Skip to content

Commit 5ca79a4

Browse files
authored
add 5, 47, 187; update 46, 65, 67, 289
1 parent 6d106ce commit 5ca79a4

8 files changed

+165
-20
lines changed

0005-longest-palindromic-substring.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
5. Longest Palindromic Substring
3+
4+
Submitted: March 29, 2025
5+
6+
Runtime: 9869 ms (beats 5.01%)
7+
17.43 MB (beats 99.57%)
8+
"""
9+
10+
class Solution:
11+
def longestPalindrome(self, s: str) -> str:
12+
# https://stackoverflow.com/a/53798151
13+
return max((s[i:j+1] for i in range (len(s)) for j in range(i,len(s)) if s[i:j+1] == s[i:j+1][::-1]), key=str.__len__)

0046-permutations.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
"""
22
46. Permutations
33
4-
Submitted: January 23, 2025
4+
Submitted: March 28, 2025
55
66
Runtime: 0 ms (beats 100.00%)
7-
Memory: 17.71 MB (beats 60.66%)
7+
Memory: 18.02 MB (beats 32.93%)
88
"""
99

1010
class Solution:
1111
def permute(self, nums: List[int]) -> List[List[int]]:
12-
return list(permutations(nums))
12+
if len(nums) < 2:
13+
return [nums]
14+
elif len(nums) == 2:
15+
return [nums, list(reversed(nums))]
16+
return list(
17+
itertools.chain.from_iterable(
18+
[[nums[i]] + v for v in self.permute(nums[:i] + nums[i+1:])]
19+
for i in range(len(nums))
20+
)
21+
)

0047-permutations-ii.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
47. Permutations II
3+
4+
Submitted: March 28, 2025
5+
6+
Runtime: 239 ms (beats 12.60%)
7+
Memory: 23.88 MB (beats 5.08%)
8+
"""
9+
10+
class Solution:
11+
def permute(self, nums: List[int]) -> List[List[int]]:
12+
if len(nums) < 2:
13+
return [nums]
14+
elif len(nums) == 2:
15+
return [nums, list(reversed(nums))]
16+
return list(
17+
itertools.chain.from_iterable(
18+
[[nums[i]] + v for v in self.permute(nums[:i] + nums[i+1:])]
19+
for i in range(len(nums))
20+
)
21+
)
22+
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
23+
return list(set(tuple(x) for x in self.permute(nums)))

0065-valid-number.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,17 @@
55
I used the regular expression shown below:
66

77
```regex
8-
(\+|-)?(\d+\.?|(\d+)?\.\d+)((e|E)(\+|-)?\d+)?
8+
[\+-]?(?:\d+\.?|(\d+)?\.\d+)(?:[Ee][\+-]?\d+)?
99
```
1010

1111
### Breakdown of Regex
1212

13-
`(\+|-)?` - an optional plus (`+`) or minus (`-`) sign
14-
`(\d+\.?|(\d+)?\.\d+)` - the digits, matches one of either:
13+
`[\+-]?` - an optional plus (`+`) or minus (`-`) sign
14+
`(?:\d+\.?|(\d+)?\.\d+)` - the digits, matches one of either:
1515
- `\d+\.?` - at least one numeric digit and optionally a following decimal dot (e.g. `123`, `123.`)
1616
- `(\d+)?\.\d+` - a decimal dot and at least one numeric digit, optionally preceded by digits (e.g. `0.33`, `.33`)
17-
`((e|E)(\+|-)?\d+)?` - the exponent:
18-
- `(e|E)` matches either uppercase or lowercase `e`
19-
- `(\+|-)` - see above
17+
`(?:[eE][\+-]?\d+)?` - the exponent:
18+
- `[eE]` matches either uppercase or lowercase `e`
19+
- `[\+-]?` - see above
2020
- `\d+` - at least one numeric digit
2121
- the `?` at the end makes it optional
22-
23-
## Code
24-
25-
```python3
26-
class Solution:
27-
def isNumber(self, s: str) -> bool:
28-
return bool(re.fullmatch(r'(\+|-)?(\d+\.?|(\d+)?\.\d+)((e|E)(\+|-)?\d+)?', s))
29-
```

0065-valid-number.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""
22
65. Valid Number
33
4-
Submitted: January 23, 2025
4+
Submitted: April 15, 2025
55
66
Runtime: 0 ms (beats 100.00%)
7-
Memory: 17.60 MB (beats 49.99%)
7+
Memory: 17.66 MB (beats 88.76%)
88
"""
99

1010
class Solution:
1111
def isNumber(self, s: str) -> bool:
12-
return bool(re.fullmatch(r'(\+|-)?(\d+\.?|(\d+)?\.\d+)((e|E)(\+|-)?\d+)?', s))
12+
return bool(re.fullmatch(r'[\+-]?(?:\d+\.?|(\d+)?\.\d+)(?:[Ee][\+-]?\d+)?', s))

0067-add-binary.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
67. Add Binary
3+
4+
Runtime: 0 ms (beats 100.00%)
5+
Memory: 17.76 MB (beats 73.35%)
6+
"""
7+
8+
class Solution:
9+
def addBinary(self, a: str, b: str) -> str:
10+
return bin(int(a, 2) + int(b, 2))[2:]

0187-repeated-dna-sequences.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
187. Repeated DNA Sequences
3+
4+
Submitted: April 16, 2025
5+
6+
Runtime: 39 ms (beats 15.79%)
7+
Memory: 29.16 MB (beats 48.93%)
8+
"""
9+
10+
class Solution:
11+
def findRepeatedDnaSequences(self, s: str) -> List[str]:
12+
if len(s) <= 10:
13+
return []
14+
def _():
15+
dq = deque(s[:10])
16+
for i in s[10:]:
17+
yield ''.join(dq)
18+
dq.popleft()
19+
dq.append(i)
20+
yield ''.join(dq)
21+
return [
22+
k for k,v in Counter(_()).items()
23+
if v >= 2
24+
]

0289-game-of-life.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
289. Game of Life
3+
4+
Submitted: April 3, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 9.25 MB (beats 6.02%)
8+
*/
9+
10+
typedef struct pair {
11+
int x;
12+
int y;
13+
} pair;
14+
15+
inline unsigned char** init2D(pair size) {
16+
int n = size.x, m = size.y;
17+
unsigned char** mat = malloc((sizeof(unsigned char*) * n) + (sizeof(unsigned char) * n * m));
18+
unsigned char* data = (unsigned char*)(mat + n);
19+
for (size_t i = 0; i < n; ++i) {
20+
mat[i] = data + (m * i);
21+
}
22+
return mat;
23+
}
24+
25+
inline unsigned char** copy2D(int** matrix, pair size) {
26+
unsigned char** copy = init2D(size);
27+
int n = size.x, m = size.y;
28+
for (size_t i = 0; i < n; ++i) {
29+
for (size_t j = 0; j < m; ++j) {
30+
copy[i][j] = matrix[i][j];
31+
}
32+
}
33+
return copy;
34+
}
35+
36+
#define isValidIndex(n, m, i, j) ((0 <= i && i < n) && (0 <= j && j < m))
37+
#define validOrZero(a, n, m, i, j) (isValidIndex(n, m, i, j) ? a[i][j] : 0)
38+
unsigned char sumOfNeighbors(unsigned char** matrix, pair size, pair index) {
39+
const int n = size.x, m = size.y;
40+
const int i = index.x, j = index.y;
41+
return validOrZero(matrix, n, m, i - 1, j - 1) +
42+
validOrZero(matrix, n, m, i - 1, j) +
43+
validOrZero(matrix, n, m, i - 1, j + 1) +
44+
validOrZero(matrix, n, m, i, j - 1) +
45+
validOrZero(matrix, n, m, i, j + 1) +
46+
validOrZero(matrix, n, m, i + 1, j - 1) +
47+
validOrZero(matrix, n, m, i + 1, j) +
48+
validOrZero(matrix, n, m, i + 1, j + 1);
49+
}
50+
51+
void gameOfLife(int** board, int boardSize, int* boardColSize) {
52+
pair size = { boardSize, boardColSize[0] };
53+
int n = size.x, m = size.y;
54+
unsigned char** copy = copy2D(board, size);
55+
for (size_t i = 0; i < n; ++i) {
56+
for (size_t j = 0; j < m; ++j) {
57+
int cell = board[i][j];
58+
pair index = { i, j };
59+
unsigned char neighbors = sumOfNeighbors(copy, size, index);
60+
if (cell) {
61+
if (neighbors < 2) {
62+
board[i][j] = 0;
63+
} else if (neighbors > 3) {
64+
board[i][j] = 0;
65+
}
66+
} else {
67+
if (neighbors == 3) {
68+
board[i][j] = 1;
69+
}
70+
}
71+
}
72+
}
73+
free(copy);
74+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy