66. Plus One 、 88. Merge Sorted Array

This commit is contained in:
huihut 2018-02-11 22:17:45 +08:00
parent d3cd7c0645
commit 81139710a9
6 changed files with 41 additions and 1 deletions

View File

@ -4,6 +4,7 @@
"type_traits": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"iosfwd": "cpp"
}
}

View File

@ -10,6 +10,7 @@ public:
return (findKth(nums1, 0, nums2, 0, len / 2) + findKth(nums1, 0, nums2, 0, len / 2 + 1)) / 2;
}
// find kth number of two sorted array
double findKth(vector<int>& nums1, int i1, vector<int>& nums2, int i2, int k) {
if (i1 >= nums1.size()) {
return nums2[i2 + k - 1];

View File

@ -0,0 +1,15 @@
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int carry = 1;
for(int i = digits.size() - 1; i >= 0; --i) {
int add = digits[i] + carry;
digits[i] = add % 10;
carry = add / 10;
}
if(carry > 0) {
digits.insert(digits.begin(), carry);
}
return digits;
}
};

View File

@ -0,0 +1,20 @@
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int i1 = m - 1, i2 = n - 1;
for(int i = m + n - 1; i >= 0; i--) {
if(i1 >= 0 && i2 < 0)
break;
if(i1 < 0 && i2 >= 0) {
nums1[i] = nums2[i2--];
}
if(i1 >= 0 && i2 >= 0) {
if(nums1[i1] > nums2[i2]) {
nums1[i] = nums1[i1--];
} else {
nums1[i] = nums2[i2--];
}
}
}
}
};

View File

@ -75,7 +75,10 @@
* [1. Two Sum](Problems/LeetcodeProblems/1-two-sum.h)
* [4. Median of Two Sorted Arrays](Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h)
* [11. Container With Most Water](Problems/LeetcodeProblems/11-container-with-most-water.h)
* [26. Remove Duplicates from Sorted Array](Problems/LeetcodeProblems/26-remove-duplicates-from-sorted-array.h)
* [53. Maximum Subarray](Problems/LeetcodeProblems/53-maximum-subarray.h)
* [66. Plus One](Problems/LeetcodeProblems/66-plus-one.h)
* [88. Merge Sorted Array](Problems/LeetcodeProblems/88-merge-sorted-array.h)
* [121. Best Time to Buy and Sell Stock](Problems/LeetcodeProblems/121-best-time-to-buy-and-sell-stock.h)
* [169. Majority Element](Problems/LeetcodeProblems/169-majority-element.h)
* [283. Move Zeroes](Problems/LeetcodeProblems/283-move-zeroes.h)