mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
66. Plus One 、 88. Merge Sorted Array
This commit is contained in:
parent
d3cd7c0645
commit
81139710a9
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -4,6 +4,7 @@
|
|||
"type_traits": "cpp",
|
||||
"xstring": "cpp",
|
||||
"xtree": "cpp",
|
||||
"xutility": "cpp"
|
||||
"xutility": "cpp",
|
||||
"iosfwd": "cpp"
|
||||
}
|
||||
}
|
|
@ -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];
|
||||
|
|
15
Problems/LeetcodeProblems/66-plus-one.h
Normal file
15
Problems/LeetcodeProblems/66-plus-one.h
Normal 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;
|
||||
}
|
||||
};
|
20
Problems/LeetcodeProblems/88-merge-sorted-array.h
Normal file
20
Problems/LeetcodeProblems/88-merge-sorted-array.h
Normal 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--];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user