Skip to content

Commit f7d0f71

Browse files
committed
add 8, 148, 290, 374, 3714; update 217
1 parent 2385d42 commit f7d0f71

6 files changed

+138
-9
lines changed

0008-string-to-integer-atoi.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
8. String to Integer (atoi)
3+
4+
Submitted: February 10, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 9.09 MB (beats 82.35%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int myAtoi(string s) {
13+
if (s.size() == 0) return 0;
14+
long long res = 0;
15+
auto it = s.cbegin();
16+
while (*it == ' ') ++it;
17+
bool negative = false;
18+
if (*it == '-') {
19+
negative = true;
20+
++it;
21+
} else if (*it == '+') {
22+
++it;
23+
}
24+
while (it != s.cend() && 48 <= (int) *it && (int) *it <= 57) {
25+
res *= 10;
26+
if (res > 2147483648 && negative) return -2147483648;
27+
if (res > 2147483647 && !negative) return 2147483647;
28+
res += ((int) *it) - 48;
29+
++it;
30+
}
31+
if (res > 2147483647) return negative ? -2147483648 : 2147483647;
32+
if (negative) return -res;
33+
return res;
34+
}
35+
};

0148-sort-list.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
148. Sort List
3+
4+
Submitted: February 10, 2025
5+
6+
Runtime: 7 ms (beats 96.37%)
7+
Memory: 58.09 MB (beats 75.27%)
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode() : val(0), next(nullptr) {}
16+
* ListNode(int x) : val(x), next(nullptr) {}
17+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
ListNode* sortList(ListNode* head) {
23+
vector<int> v;
24+
for (ListNode* p = head; p != nullptr; p = p->next) v.push_back(p->val);
25+
sort(v.begin(), v.end());
26+
auto it = v.cbegin();
27+
for (ListNode* p = head; p != nullptr && it != v.cend(); p = p->next, ++it) p->val = *it;
28+
return head;
29+
}
30+
};

0217-contains-duplicate.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
"""
22
217. Contains Duplicate
33
4-
Submitted: October 4, 2024
5-
Runtime: 414 ms (beats 5.53%)
6-
Memory: 35.63 MB (beats 8.45)
4+
Submitted: February 7, 2025
5+
Runtime: 18 ms (beats 20.84%)
6+
Memory: 31.66 MB (beats 30.18%)
77
"""
88

99
class Solution:
1010
def containsDuplicate(self, nums: List[int]) -> bool:
11-
map = {}
12-
for i in range(len(nums)):
13-
if nums[i] in map:
14-
return True
15-
map[nums[i]] = i
16-
return False
11+
return len(set(nums)) != len(nums)

0290-word-pattern.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
290. Word Pattern
3+
4+
Submitted: February 7, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.72 MB (beats 54.52%)
8+
"""
9+
10+
class Solution:
11+
def wordPattern(self, pattern: str, s: str) -> bool:
12+
words: list[str] = s.split(' ')
13+
if len(pattern) != len(words): return False
14+
# bidirectional hash map
15+
d1 = {}
16+
d2 = {}
17+
for (letter, word) in zip(pattern, words):
18+
if letter in d1:
19+
if d1[letter] != word: return False
20+
elif word in d2:
21+
if d2[word] != letter: return False
22+
d1[letter] = word
23+
d2[word] = letter
24+
return True

0374-guess-number-higher-or-lower.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
374. Guess Number Higher or Lower
3+
4+
Submitted: February 7, 2025
5+
*/
6+
7+
/**
8+
* Forward declaration of guess API.
9+
* @param num your guess
10+
* @return -1 if num is higher than the picked number
11+
* 1 if num is lower than the picked number
12+
* otherwise return 0
13+
* int guess(int num);
14+
*/
15+
16+
class Solution {
17+
public:
18+
int guessNumber(int n) {
19+
unsigned min = 1, max = n;
20+
unsigned mid;
21+
unsigned char guess_res;
22+
while (min <= max) {
23+
mid = (max + min) / 2;
24+
guess_res = guess(mid);
25+
if (guess_res == 0) return mid;
26+
else if (guess_res == 1) min = mid + 1;
27+
else max = mid - 1;
28+
}
29+
return -1;
30+
}
31+
};

3174-clear-digits.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
3174. Clear Digits
3+
4+
Submitted: February 10, 2025
5+
6+
Runtime: 3 ms (beats 29.98%)
7+
Memory: 17.56 MB (beats 88.57%)
8+
"""
9+
10+
class Solution:
11+
def clearDigits(self, s: str) -> str:
12+
while (pos := next((i for i in range(len(s)) if s[i].isdigit()), None)) is not None:
13+
s = s[:pos-1] + s[pos+1:]
14+
return s

0 commit comments

Comments
 (0)