leetCode練習(88)
題目:Merge Sorted Array
難度:easy
問題描述:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
You may assume that nums1 has enough space (size that is greater or equal to
m + n) to hold additional elements from nums2. The number of elements initialized in
nums1
解題思路:
將有序陣列B插入有序陣列A中,預設A的空間足夠。很容易的一道題,注意從末端插入,這樣就可以不需要移動還未遍歷到的數了。
具體程式碼如下:
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int p1=m-1; int p2=n-1; int i; if(n==0){ return; } if(m==0){ for(i=0;i<n;i++){ nums1[i]=nums2[i]; } return; } for(i=n+m-1;i>=0&&p1>=0&&p2>=0;i--){ if(nums1[p1]>nums2[p2]){ nums1[i]=nums1[p1]; p1--; }else{ nums1[i]=nums2[p2]; p2--; } } if(p1<0){ while(p2>=0){ nums1[i--]=nums2[p2--]; } return; } } }
相關推薦
leetCode練習(88)
題目:Merge Sorted Array 難度:easy 問題描述: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y
leetCode練習(98)
題目:Validate Binary Search Tree 難度:medium 問題描述: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is
leetCode練習(140)
題目:Single Number難度:hard問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to
leetCode練習(153)、(154)
題目:Find Minimum in Rotated Sorted Array難度:medium問題描述:Suppose an array sorted in ascending order is rotated at some pivot unknown to you be
leetCode練習(61)
題目:Rotate List 難度:medium 問題描述: Given a list, rotate the list to the right by k places, where k is n
leetCode練習(121)
題目:Best Time to Buy and Sell Stock 難度:easy 問題描述: Say you have an array for which the ith element is the price of a given stock on day i.
leetCode練習(147)
題目:Insertion Sort List難度:medium問題描述:使用插入排序對List進行排序求解思路:從左到右,依次將node插入到左邊已經排好的list中程式碼如下:public stati
leetCode練習(1)
題目:gas station難度:MEDIUM問題描述:There are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car
leetCode練習(139)
題目:Single Number難度:medium問題描述:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s ca
leetcode練習(2)
Container With Most Water 題意:在二維座標系中,(i, ai) 表示 從 (i, 0) 到 (i, ai) 的一條線段,任意兩條這樣的線段和 x 軸組成一個木桶,找出能夠盛水最多的木桶,返回其容積。 程式碼: int l = 0 ; int
leetCode練習(47)
題目:Permutations II 難度:medium 問題描述: Given a collection of numbers that might contain duplicates, return all possible unique permutations.
leetCode練習(172)
題目:Factorial Trailing Zeroes難度:EASY問題描述:Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3 Output: 0 Explan
leetCode練習(128)
題目:Longest Consecutive Sequence 難度:hard 問題描述: Given an unsorted array of integers, find the length of the longest consecutive elements se
leetCode練習(43)
題目:Multiply Strings 難度:dedium 問題描述: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th
Leetcode代碼練習(二)
sts compare bre 那種 character span 一次 給定 spa 首先,沒有第二題,沒有第二題的原因是,JavaScript中根本就沒有那種數據結構,盡管我在playground裏面調試出了正確的結果,但是也許是因為數據結構問題,最終沒能讓我通過。 所
【LeetCode演算法練習(C++)】Count and Say
題目: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 2
二、Linear Regression 練習(轉載)
hold off int 必須 html 移動 根據 which win from 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/15/2961660.html 前言 本文是多元線性回歸的練習,這裏練習的
六、regularized logisitic regssion練習(轉載)
method open lose hold on update import 等高線 algo 2個 轉載鏈接:http://www.cnblogs.com/tornadomeet/archive/2013/03/17/2964858.html 在上一講Deep learn
日常編程練習(三)
style tar code 依次 span 二叉 div 前序遍歷 cin 一、樹的遍歷算法 樹的創建 struct BinaryTreeNode { int val; BinaryTreeNode* left; BinaryTreeNode*
課堂練習(團隊)
公開 壓力 自己的 般的 責任 開發人員 做出 勞動力 了解 1.團隊如何做決定? a)獨裁:領導說了算。 優點:領導比較冷靜、客觀,以整體利益為主;避免了團隊間的明面上的糾紛,團隊能有效地服從、執行。 缺點:領導可能不知道團隊的能力,不了解團隊的做事方法,不能做出正確的決