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 79d218a

Browse files
update 144, 1929, add 7, 53, 69, 242, 412, 1749, 1929, 2667
1 parent 9b19c39 commit 79d218a

12 files changed

+225
-20
lines changed

‎0007-reverse-integer.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
7. Reverse Integer
3+
4+
Submitted: February 25, 2025
5+
6+
Runtime: 32 ms (beats 90.57%)
7+
Memory: 17.83 MB (beats 37.07%)
8+
"""
9+
10+
class Solution:
11+
def reverse(self, x: int) -> int:
12+
res = -int((str(x)[1:][::-1])) if x < 0 else int(str(x)[::-1])
13+
return res if -2147483648 <= res <= 2147483647 else 0

‎0053-maximum-subarray.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
53. Maximum Subarray
3+
4+
Submitted: February 26, 2025
5+
6+
Runtime: 56 ms (beats 63.17%)
7+
Memory: 32.92 MB (beats 7.45%)
8+
"""
9+
10+
class Solution:
11+
def maxSubArray(self, nums: List[int]) -> int:
12+
res = float('-inf')
13+
c = 0
14+
for n in nums:
15+
# c = max(n, c + n)
16+
c = (c + n) if c > 0 else n
17+
res = max(res, c)
18+
return res

‎0069-sqrtx.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
69. Sqrt(x)
3+
4+
Submitted: February 23, 2025
5+
6+
Runtime: 0 ms (beats 100.00%
7+
Memory: 8.53 MB (beats 33.16%)
8+
*/
9+
10+
// fast inverse square root
11+
double Q_rsqrt( double number )
12+
{
13+
long i;
14+
double x2, y;
15+
const double threehalfs = 1.5;
16+
17+
x2 = number * 0.5;
18+
y = number;
19+
i = * ( long * ) &y;
20+
i = 0x5fe6eb50c7b537a9 - ( i >> 1 );
21+
y = * ( double * ) &i;
22+
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
23+
y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
24+
y = y * ( threehalfs - ( x2 * y * y ) ); // added in 3rd iteration so we can pass leetcode testcases
25+
return y;
26+
}
27+
28+
int mySqrt(int x) {
29+
return 1 / Q_rsqrt(x);
30+
}

‎0069-sqrtx.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
69. Sqrt(x)
3+
4+
Submitted: February 23, 2025
5+
6+
Runtime: 0 ms (beats 100.00%
7+
Memory: 8.58 MB (beats 51.10%)
8+
*/
9+
10+
// fast inverse square root
11+
double Q_rsqrt( double number )
12+
{
13+
long i;
14+
double x2, y;
15+
const double threehalfs = 1.5;
16+
17+
x2 = number * 0.5;
18+
y = number;
19+
i = * ( long * ) &y;
20+
i = 0x5fe6eb50c7b537a9 - ( i >> 1 );
21+
y = * ( double * ) &i;
22+
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
23+
y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
24+
y = y * ( threehalfs - ( x2 * y * y ) ); // added in 3rd iteration so we can pass leetcode testcases
25+
return y;
26+
}
27+
28+
class Solution {
29+
public:
30+
int mySqrt(int x) {
31+
return 1.0 / Q_rsqrt(x);
32+
}
33+
};
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
144. Binary Tree Preorder Traversal
33
4-
Submitted: March 2, 2023
4+
Submitted: February 13, 2025
55
6-
Runtime: 41 ms (beats 100.00%)
7-
Memory: 13.84 MB (beats 100.00%)
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.68 MB (beats 83.66%)
88
"""
99

1010
# Definition for a binary tree node.
@@ -13,14 +13,8 @@
1313
# self.val = val
1414
# self.left = left
1515
# self.right = right
16+
1617
class Solution:
1718
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
18-
b=[]
19-
def t(root):
20-
if root is None:
21-
return
22-
b.append(root.val)
23-
t(root.left)
24-
t(root.right)
25-
t(root)
26-
return b
19+
if not root: return []
20+
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)

‎0242-valid-anagram.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
242. Valid Anagram
3+
4+
Submitted: February 25, 2025
5+
6+
Runtime: 7 ms (beats 89.87%)
7+
Memory: 18.05 MB (beats 32.84%)
8+
"""
9+
10+
class Solution:
11+
def isAnagram(self, s: str, t: str) -> bool:
12+
return Counter(s) == Counter(t)

‎0412-fizz-buzz.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
412. Fizz Buzz
3+
4+
Submitted: February 26, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 18.44 MB (beats 26.16%)
8+
"""
9+
10+
class Solution:
11+
def fizzBuzz(self, n: int) -> List[str]:
12+
return [
13+
"FizzBuzz" if i % 3 == 0 and i % 5 == 0 else
14+
"Fizz" if i % 3 == 0 else
15+
"Buzz" if i % 5 == 0 else
16+
str(i)
17+
for i in range(1, n + 1)
18+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
1749. Maximum Absolute Sum of Any Subarray
3+
4+
Submitted: February 26, 2025
5+
6+
Runtime: 60 ms (beats 62.45%)
7+
Memory: 28.43 MB (beats 62.31%)
8+
"""
9+
10+
class Solution:
11+
12+
def maxAbsoluteSum(self, nums: List[int]) -> int:
13+
return max(abs(self.maxSum(nums)), abs(self.minSum(nums)))
14+
15+
def maxSum(self, nums):
16+
res = float('-inf')
17+
c = 0
18+
for n in nums:
19+
# c = max(n, c + n)
20+
c = (c + n) if c > 0 else n
21+
res = max(res, c)
22+
return res
23+
24+
def minSum(self, nums):
25+
res = float('inf')
26+
c = 0
27+
for n in nums:
28+
# c = max(n, c + n)
29+
c = (c + n) if c < 0 else n
30+
res = min(res, c)
31+
return res
32+
33+

‎1929-concatenation-of-array.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
1929. Concatenation of Array
3+
4+
Submitted: February 25, 2025
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 14.39 MB (beats 60.45%)
8+
*/
9+
10+
/**
11+
* Note: The returned array must be malloced, assume caller calls free().
12+
*/
13+
int* getConcatenation(int* nums, int numsSize, int* returnSize) {
14+
nums = realloc(nums, numsSize * sizeof(int) * 2);
15+
*returnSize = numsSize * 2;
16+
for (int i = 0; i < numsSize; ++i) nums[i + numsSize] = nums[i];
17+
return nums;
18+
}

‎1929-concatenation-of-array.cpp‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/*
22
1929. Concatenation of Array
33
4-
Submitted: October 7, 2024
4+
Submitted: February 25, 2025
55
6-
Runtime: 8 ms (beats 8.35%)
7-
Memory: 15.68 MB (beats 16.24%)
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 17.18 MB (beats 17.74%)
88
*/
99

1010
class Solution {
1111
public:
12-
vector<int> getConcatenation(vector<int>& nums) {
13-
int length = nums.size();
14-
for (int i = 0; i < length; ++i) {
15-
nums.push_back(nums.at(i));
16-
}
12+
vector<int>& getConcatenation(vector<int>& nums) {
13+
size_t n = nums.size();
14+
nums.reserve(n * 2);
15+
for (int i = 0; i < n; ++i) nums.push_back(nums[i]);
1716
return nums;
1817
}
1918
};

0 commit comments

Comments
(0)

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