Skip to content

Commit 2a5957a

Browse files
authored
Merge branch 'doocs:main' into main
2 parents ab59941 + 8c4ea1f commit 2a5957a

File tree

204 files changed

+39690
-31045
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

204 files changed

+39690
-31045
lines changed

images/starcharts.svg

Lines changed: 30737 additions & 30637 deletions
Loading

solution/0000-0099/0008.String to Integer (atoi)/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,36 @@ class Solution {
386386
}
387387
```
388388

389+
#### C
390+
391+
```c
392+
int myAtoi(char* s) {
393+
int i = 0;
394+
395+
while (s[i] == ' ') {
396+
i++;
397+
}
398+
399+
int sign = 1;
400+
if (s[i] == '-' || s[i] == '+') {
401+
sign = (s[i] == '-') ? -1 : 1;
402+
i++;
403+
}
404+
405+
int res = 0;
406+
while (isdigit(s[i])) {
407+
int digit = s[i] - '0';
408+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
409+
return sign == 1 ? INT_MAX : INT_MIN;
410+
}
411+
res = res * 10 + digit;
412+
i++;
413+
}
414+
415+
return res * sign;
416+
}
417+
```
418+
389419
<!-- tabs:end -->
390420
391421
<!-- solution:end -->

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,36 @@ class Solution {
374374
}
375375
```
376376

377+
#### C
378+
379+
```c
380+
int myAtoi(char* s) {
381+
int i = 0;
382+
383+
while (s[i] == ' ') {
384+
i++;
385+
}
386+
387+
int sign = 1;
388+
if (s[i] == '-' || s[i] == '+') {
389+
sign = (s[i] == '-') ? -1 : 1;
390+
i++;
391+
}
392+
393+
int res = 0;
394+
while (isdigit(s[i])) {
395+
int digit = s[i] - '0';
396+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
397+
return sign == 1 ? INT_MAX : INT_MIN;
398+
}
399+
res = res * 10 + digit;
400+
i++;
401+
}
402+
403+
return res * sign;
404+
}
405+
```
406+
377407
<!-- tabs:end -->
378408
379409
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int myAtoi(char* s) {
2+
int i = 0;
3+
4+
while (s[i] == ' ') {
5+
i++;
6+
}
7+
8+
int sign = 1;
9+
if (s[i] == '-' || s[i] == '+') {
10+
sign = (s[i] == '-') ? -1 : 1;
11+
i++;
12+
}
13+
14+
int res = 0;
15+
while (isdigit(s[i])) {
16+
int digit = s[i] - '0';
17+
if (res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > INT_MAX % 10)) {
18+
return sign == 1 ? INT_MAX : INT_MIN;
19+
}
20+
res = res * 10 + digit;
21+
i++;
22+
}
23+
24+
return res * sign;
25+
}

solution/0000-0099/0009.Palindrome Number/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ class Solution {
248248
}
249249
```
250250

251+
#### C
252+
253+
```c
254+
bool isPalindrome(int x) {
255+
if (x < 0 || (x != 0 && x % 10 == 0)) {
256+
return false;
257+
}
258+
259+
int y = 0;
260+
while (y < x) {
261+
y = y * 10 + x % 10;
262+
x /= 10;
263+
}
264+
265+
return (x == y || x == y / 10);
266+
}
267+
```
268+
251269
<!-- tabs:end -->
252270
253271
<!-- solution:end -->

solution/0000-0099/0009.Palindrome Number/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,24 @@ class Solution {
240240
}
241241
```
242242

243+
#### C
244+
245+
```c
246+
bool isPalindrome(int x) {
247+
if (x < 0 || (x != 0 && x % 10 == 0)) {
248+
return false;
249+
}
250+
251+
int y = 0;
252+
while (y < x) {
253+
y = y * 10 + x % 10;
254+
x /= 10;
255+
}
256+
257+
return (x == y || x == y / 10);
258+
}
259+
```
260+
243261
<!-- tabs:end -->
244262
245263
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
bool isPalindrome(int x) {
2+
if (x < 0 || (x != 0 && x % 10 == 0)) {
3+
return false;
4+
}
5+
6+
int y = 0;
7+
while (y < x) {
8+
y = y * 10 + x % 10;
9+
x /= 10;
10+
}
11+
12+
return (x == y || x == y / 10);
13+
}

solution/0000-0099/0010.Regular Expression Matching/README.md

Lines changed: 117 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,85 @@ public class Solution {
331331
}
332332
```
333333

334+
#### C
335+
336+
```c
337+
#define MAX_LEN 1000
338+
339+
char *ss, *pp;
340+
int m, n;
341+
int f[MAX_LEN + 1][MAX_LEN + 1];
342+
343+
bool dfs(int i, int j) {
344+
if (j >= n) {
345+
return i == m;
346+
}
347+
if (f[i][j] != 0) {
348+
return f[i][j] == 1;
349+
}
350+
int res = -1;
351+
if (j + 1 < n && pp[j + 1] == '*') {
352+
if (dfs(i, j + 2) || (i < m && (ss[i] == pp[j] || pp[j] == '.') && dfs(i + 1, j))) {
353+
res = 1;
354+
}
355+
} else if (i < m && (ss[i] == pp[j] || pp[j] == '.') && dfs(i + 1, j + 1)) {
356+
res = 1;
357+
}
358+
f[i][j] = res;
359+
return res == 1;
360+
}
361+
362+
bool isMatch(char* s, char* p) {
363+
ss = s;
364+
pp = p;
365+
m = strlen(s);
366+
n = strlen(p);
367+
memset(f, 0, sizeof(f));
368+
return dfs(0, 0);
369+
}
370+
```
371+
372+
#### PHP
373+
374+
```php
375+
class Solution {
376+
/**
377+
* @param String $s
378+
* @param String $p
379+
* @return Boolean
380+
*/
381+
function isMatch($s, $p) {
382+
$m = strlen($s);
383+
$n = strlen($p);
384+
$f = array_fill(0, $m + 1, array_fill(0, $n + 1, 0));
385+
386+
$dfs = function ($i, $j) use (&$s, &$p, $m, $n, &$f, &$dfs) {
387+
if ($j >= $n) {
388+
return $i == $m;
389+
}
390+
if ($f[$i][$j] != 0) {
391+
return $f[$i][$j] == 1;
392+
}
393+
$res = -1;
394+
if ($j + 1 < $n && $p[$j + 1] == '*') {
395+
if (
396+
$dfs($i, $j + 2) ||
397+
($i < $m && ($s[$i] == $p[$j] || $p[$j] == '.') && $dfs($i + 1, $j))
398+
) {
399+
$res = 1;
400+
}
401+
} elseif ($i < $m && ($s[$i] == $p[$j] || $p[$j] == '.') && $dfs($i + 1, $j + 1)) {
402+
$res = 1;
403+
}
404+
$f[$i][$j] = $res;
405+
return $res == 1;
406+
};
407+
408+
return $dfs(0, 0);
409+
}
410+
}
411+
```
412+
334413
<!-- tabs:end -->
335414

336415
<!-- solution:end -->
@@ -541,44 +620,60 @@ public class Solution {
541620
```php
542621
class Solution {
543622
/**
544-
* @param string $s
545-
* @param string $p
546-
* @return boolean
623+
* @param String $s
624+
* @param String $p
625+
* @return Boolean
547626
*/
548-
549627
function isMatch($s, $p) {
550628
$m = strlen($s);
551629
$n = strlen($p);
552630

553-
$dp = array_fill(0, $m + 1, array_fill(0, $n + 1, false));
554-
$dp[0][0] = true;
555-
556-
for ($j = 1; $j <= $n; $j++) {
557-
if ($p[$j - 1] == '*') {
558-
$dp[0][$j] = $dp[0][$j - 2];
559-
}
560-
}
631+
$f = array_fill(0, $m + 1, array_fill(0, $n + 1, false));
632+
$f[0][0] = true;
561633

562-
for ($i = 1; $i <= $m; $i++) {
634+
for ($i = 0; $i <= $m; $i++) {
563635
for ($j = 1; $j <= $n; $j++) {
564-
if ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1]) {
565-
$dp[$i][$j] = $dp[$i - 1][$j - 1];
566-
} elseif ($p[$j - 1] == '*') {
567-
$dp[$i][$j] = $dp[$i][$j - 2];
568-
if ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1]) {
569-
$dp[$i][$j] = $dp[$i][$j] || $dp[$i - 1][$j];
636+
if ($p[$j - 1] == '*') {
637+
$f[$i][$j] = $f[$i][$j - 2];
638+
if ($i > 0 && ($p[$j - 2] == '.' || $p[$j - 2] == $s[$i - 1])) {
639+
$f[$i][$j] = $f[$i][$j] || $f[$i - 1][$j];
570640
}
571-
} else {
572-
$dp[$i][$j] = false;
641+
} elseif ($i > 0 && ($p[$j - 1] == '.' || $p[$j - 1] == $s[$i - 1])) {
642+
$f[$i][$j] = $f[$i - 1][$j - 1];
573643
}
574644
}
575645
}
576646

577-
return $dp[$m][$n];
647+
return $f[$m][$n];
578648
}
579649
}
580650
```
581651

652+
#### C
653+
654+
```c
655+
bool isMatch(char* s, char* p) {
656+
int m = strlen(s), n = strlen(p);
657+
bool f[m + 1][n + 1];
658+
memset(f, 0, sizeof(f));
659+
f[0][0] = true;
660+
661+
for (int i = 0; i <= m; ++i) {
662+
for (int j = 1; j <= n; ++j) {
663+
if (p[j - 1] == '*') {
664+
f[i][j] = f[i][j - 2];
665+
if (i > 0 && (p[j - 2] == '.' || p[j - 2] == s[i - 1])) {
666+
f[i][j] = f[i][j] || f[i - 1][j];
667+
}
668+
} else if (i > 0 && (p[j - 1] == '.' || p[j - 1] == s[i - 1])) {
669+
f[i][j] = f[i - 1][j - 1];
670+
}
671+
}
672+
}
673+
return f[m][n];
674+
}
675+
```
676+
582677
<!-- tabs:end -->
583678
584679
<!-- solution:end -->

0 commit comments

Comments
 (0)