LeetCode 88

Merge Sorted Array

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 and nums2
are m and n respectively.

 1 /*************************************************************************
 2     > File Name: LeetCode088.c
 3     > Author: Juntaran
 4     > Mail: [email protected]
 5     > Created Time: Tue 17 May 2016 15:28:11 PM CST
 6  ************************************************************************/
 7
 8 /*************************************************************************
 9
10     Merge Sorted Array
11
12     Given two sorted integer arrays nums1 and nums2,
13     merge nums2 into nums1 as one sorted array.
14
15     Note:
16     You may assume that nums1 has enough space
17     (size that is greater or equal to m + n)
18     to hold additional elements from nums2.
19     The number of elements initialized in nums1 and nums2
20     are m and n respectively.
21
22  ************************************************************************/
23
24  #include <stdio.h>
25
26 void merge(int* nums1, int m, int* nums2, int n)
27 {
28     int i = m - 1;
29     int j = n - 1;
30
31     while( i>=0 && j>=0 )
32     {
33         if( nums1[i] > nums2[j] )
34         {
35             nums1[i+j+1] = nums1[i];
36             i --;
37         }
38         else
39         {
40             nums1[i+j+1] = nums2[j];
41             j --;
42         }
43     }
44     while( j >= 0 )
45     {
46         nums1[i+j+1] = nums2[j];
47         j --;
48     }
49 }
时间: 2024-11-10 03:54:14

LeetCode 88的相关文章

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array 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 n

LeetCode 88. Merge Sorted Array(合并有序数组)

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 initi

LeetCode(88)题解-- Merge Sorted Array

https://leetcode.com/problems/merge-sorted-array/ 题目: 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 additi

[leetcode] 88. 合并两个有序数组

88. 合并两个有序数组 水题,没有在原数组上做,偷了个懒 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] ans = new int[m + n]; int i = 0, j = 0; int top = 0; while (i < m && j < n) { if (nums1[i] < nums2[j]) { ans[top++] = nums1[i

LeetCode 88:Merge Sorted Array

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 init

leetcode || 88、Merge Sorted Array

problem: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A an

leetcode[88] Gray Code

题目:格雷码. 格雷码是从0开始且之后两个相邻码之间只有一个符号不相同,例如000,100,101,111三个相邻之间只有一个二进制不同. 现在给定一个数字n,然后给出格雷码所对应的数字.例如: For example, given n = 2, return [0,1,3,2]. Its gray code sequence is: 00 - 0 01 - 1 11 - 3 10 - 2 左边一列是格雷码,右边一列是对应的需要输出的十进制. 我们来观察一下格雷码的规律.如果是1那么是 0 1

[LeetCode]88. Swap Nodes in Pairs链表成对逆序

Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, onl

Java [Leetcode 88]CMerge Sorted Array

题目描述: 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