Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43b9ca1

Browse files
author
fupengfei
committed
18-01-02 COMMIT
1 parent 06fe836 commit 43b9ca1

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*First Missing Positive:Given an unsorted integer array, find the first missing positive integer.*/
2+
//交换数组元素,使得数组中第i位存放数值(i+1)。最后遍历数组,寻找第一个不符合此要求的元素,返回其下标。
3+
class Solution {
4+
public:
5+
int firstMissingPositive(vector<int>& nums) {
6+
int i = 0;
7+
int n = nums.size();
8+
while(i < n){
9+
if(nums[i] != i+1 && nums[i] >= 1 && nums[i] <= n && nums[nums[i]-1] != nums[i]){
10+
swap(nums[i], nums[nums[i]-1]);
11+
}else{
12+
i++;
13+
}
14+
}
15+
for(i = 0; i < n; ++i){
16+
if(nums[i] != i+1){
17+
return i+1;
18+
}
19+
}
20+
return n+1;
21+
}
22+
};

‎Leetcode/42. Trapping Rain Water.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*Trapping Rain Water:Given n non-negative integers representing an elevation map where the width of each bar is 1,
2+
compute how much water it is able to trap after raining.*/
3+
class Solution {
4+
public:
5+
int trap(vector<int>& height) {
6+
if(height.size() < 3) return 0;
7+
int left = 0;
8+
int right = height.size() - 1;
9+
int sum = 0;
10+
// 找到左边的第一个峰值
11+
while(left < right && height[left] <= height[left+1]) left++;
12+
// 找到右边的第一个峰值
13+
while(left < right && height[right] <= height[right-1]) right--;
14+
while(left < right){
15+
int leftVal = height[left];
16+
int rightVal = height[right];
17+
// 如果左边峰值较小,先计算左边
18+
if(leftVal < rightVal){
19+
while(left < right && leftVal >= height[++left]){
20+
sum += leftVal - height[left];
21+
}
22+
// 如果右边峰值较小,先计算右边
23+
} else {
24+
while(left < right && rightVal >= height[--right]){
25+
sum += rightVal - height[right];
26+
}
27+
}
28+
}
29+
return sum;
30+
}
31+
};

‎Leetcode/43. Multiply Strings.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*Multiply Strings:Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2.*/
2+
class Solution {
3+
public:
4+
string multiply(string num1, string num2) {
5+
static const int R = 10;
6+
7+
int n1 = num1.size();
8+
int n2 = num2.size();
9+
int i;
10+
int j;
11+
string num3 = "";
12+
int sum;
13+
int carry;
14+
int low, high;
15+
16+
carry = 0;
17+
for (i = 0; i < n1 + n2 - 1; ++i) {
18+
low = i > n2 - 1 ? i - (n2 - 1) : 0;
19+
high = i < n1 - 1 ? i : n1 - 1;
20+
sum = carry;
21+
for (j = low; j <= high; ++j) {
22+
sum += (num1[n1 - 1 - j] - '0') * (num2[n2 - 1 - (i - j)] - '0');
23+
}
24+
carry = sum / R;
25+
sum %= R;
26+
num3.push_back(sum + '0');
27+
}
28+
num3.push_back(carry + '0');
29+
while (num3.size() > 1 && num3.back() == '0') {
30+
num3.pop_back();
31+
}
32+
reverse(num3.begin(), num3.end());
33+
return num3;
34+
}
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /