Skip to content

Commit

Permalink
66. Plus One 、 88. Merge Sorted Array
Browse files Browse the repository at this point in the history
  • Loading branch information
huihut committed Feb 11, 2018
1 parent d3cd7c0 commit 8113971
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type_traits": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"xutility": "cpp"
"xutility": "cpp",
"iosfwd": "cpp"
}
}
Empty file.
1 change: 1 addition & 0 deletions Problems/LeetcodeProblems/4-median-of-two-sorted-arrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Solution {
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];
Expand Down
15 changes: 15 additions & 0 deletions Problems/LeetcodeProblems/66-plus-one.h
Original file line number Diff line number Diff line change
@@ -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;
}
};
20 changes: 20 additions & 0 deletions Problems/LeetcodeProblems/88-merge-sorted-array.h
Original file line number Diff line number Diff line change
@@ -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--];
}
}
}
}
};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8113971

Please sign in to comment.