码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Merge Sorted Array

时间:2015-03-21 07:32:13      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

Note:

Since it merged from end, so 

1. tmp1, tmp2 = INT_MIN

2. tmp1 > tmp2

 1 class Solution {
 2 public:
 3     void merge(int A[], int m, int B[], int n) {
 4         int total = m+n-1, indexA = m-1, indexB = n-1;
 5         while (indexA >= 0 || indexB >= 0) {
 6             int tmp1 = INT_MIN, tmp2 = INT_MIN;
 7             if (indexA >= 0) {
 8                 tmp1 = A[indexA];
 9             }
10             if (indexB >= 0) {
11                 tmp2 = B[indexB];
12             }
13             if (tmp1 > tmp2) {
14                 A[total--] = A[indexA--];
15             } else {
16                 A[total--] = B[indexB--];
17             }
18         }
19     }
20 };

 

LeetCode – Refresh – Merge Sorted Array

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4355093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!