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 0b01a3d

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 7c2d3db commit 0b01a3d

File tree

3 files changed

+43
-71
lines changed

3 files changed

+43
-71
lines changed

‎041_first_missing_positive/missing_positive.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ static inline void swap(int *a, int *b)
1010

1111
static int firstMissingPositive(int* nums, int numsSize)
1212
{
13-
if (numsSize <1) {
13+
if (numsSize ==0) {
1414
return 1;
1515
}
1616

1717
int i = 0;
1818
while (i < numsSize) {
1919
/* nums[i] should be i+1 and nums[nums[i] - 1] should be nums[i] */
2020
if (nums[i] != i + 1 && nums[i] > 0 && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) {
21-
/* nums[nums[i] - 1] <- nums[i] */
21+
/* let nums[nums[i] - 1] = nums[i] */
2222
swap(nums + i, nums + nums[i] - 1);
2323
} else {
2424
i++;
2525
}
2626
}
2727

2828
for (i = 0; i < numsSize; i++) {
29-
if (nums[i] != i + 1) break;
29+
if (nums[i] != i + 1) {
30+
break;
31+
}
3032
}
33+
3134
return i + 1;
3235
}
3336

‎042_trapping_rain_water/trap_water.c‎

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,33 @@
22
#include <stdlib.h>
33

44

5-
static inline int max(int a, int b)
6-
{
7-
return a > b ? a : b;
8-
}
9-
10-
static inline int min(int a, int b)
11-
{
12-
return a < b ? a : b;
13-
}
14-
155
static int trap(int* height, int heightSize)
166
{
17-
if (heightSize < 1) {
18-
return 0;
19-
}
20-
21-
int i;
22-
int *lh = malloc(heightSize * sizeof(int));
23-
int *rh = malloc(heightSize * sizeof(int));
24-
25-
/* restore the max height in the left side of [i] (included) */
26-
lh[0] = height[0];
27-
for (i = 1; i < heightSize; i++) {
28-
lh[i] = max(height[i], lh[i - 1]);
29-
}
30-
31-
/* restore the max height in the right side of [i] (included) */
32-
rh[heightSize - 1] = height[heightSize - 1];
33-
for (i = heightSize - 2; i >= 0; i--) {
34-
rh[i] = max(height[i], rh[i + 1]);
35-
}
36-
37-
int capacity = 0;
38-
for (i = 0; i < heightSize; i++) {
39-
capacity += min(lh[i], rh[i]) - height[i];
7+
/* In fact if we find the relative higher bar position and then the
8+
* water level of the position would be determined by the opposite side.
9+
*/
10+
int res = 0;
11+
int l = 0, lmax = 0;
12+
int r = heightSize - 1, rmax = 0;
13+
while (l < r) {
14+
if (height[l] < height[r]) {
15+
if (height[l] > lmax) {
16+
lmax = height[l];
17+
} else {
18+
res += lmax - height[l];
19+
}
20+
l++;
21+
} else {
22+
if (height[r] > rmax) {
23+
rmax = height[r];
24+
} else {
25+
res += rmax - height[r];
26+
}
27+
r--;
28+
}
4029
}
4130

42-
return capacity;
31+
return res;
4332
}
4433

4534
int main(int argc, char **argv)

‎043_multiply_strings/multiply_strings.c‎

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,38 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5-
static void reverse(char *s, int len)
6-
{
7-
int low = 0;
8-
int high = len - 1;
9-
while (low < high) {
10-
char c = s[low];
11-
s[low] = s[high];
12-
s[high] = c;
13-
low++;
14-
high--;
15-
}
16-
}
175

186
static char* multiply(char* num1, char* num2)
197
{
208
if (*num1 == '0円') {
219
return num1;
2210
}
11+
2312
if (*num2 == '0円') {
2413
return num2;
2514
}
2615

2716
int i, j;
28-
char *result = malloc(110 + 110);
29-
memset(result, '0', 220);
3017
int len1 = strlen(num1);
3118
int len2 = strlen(num2);
32-
reverse(num1, len1);
33-
reverse(num2, len2);
34-
for (i = 0; i < len1; i++) {
19+
char *result = malloc(len1 + len2 + 1);
20+
memset(result, '0', len1 + len2 + 1);
21+
result[len1 + len2] = '0円';
22+
23+
for (i = len2 - 1; i >= 0; i--) {
3524
int carry = 0;
36-
for (j = 0; j <len2; j++) {
37-
carry += (num1[i] - '0') * (num2[j] - '0') + (result[i + j] - '0');
38-
result[i + j] = carry % 10 + '0';
25+
for (j = len1-1; j >= 0; j--) {
26+
carry += (num2[i] - '0') * (num1[j] - '0') + (result[i + j+1] - '0');
27+
result[i + j+1] = carry % 10 + '0';
3928
carry /= 10;
4029
}
41-
if (carry != 0) {
42-
result[len2 + i] = carry + '0';
43-
}
44-
}
45-
int len = 220;
46-
while (--len >= 0) {
47-
if (result[len] > '0') {
48-
result[++len] = '0円';
49-
break;
50-
}
30+
result[i + j + 1] = carry + '0';
5131
}
52-
if (len==-1) {
53-
len=1;
54-
result[len] ='0円';
32+
33+
while (result[0] =='0'&&result[1] !='0円') {
34+
result++;
5535
}
56-
reverse(result, len);
36+
5737
return result;
5838
}
5939

0 commit comments

Comments
(0)

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