max_int:
- # result = 0
- # if isPos:
- # return result
- # else:
- # return -1 * result
+# if flag:
+# x = "-" + x
- def reverse(self, x):
- # Note that in Python -1 / 10 = -1
- res, isPos = 0, 1
+# value = 2 ** 31
+# x = int(x)
+# if -value <= x < value:
+# return x
+# return 0
+
+ is_neg = False
if x < 0:
- isPos = -1
- x = -1 * x
- while x != 0:
- res = res * 10 + x % 10
- if res > 2147483647:
- return 0
- x /= 10
- return res * isPos
\ No newline at end of file
+ x = -x
+ is_neg = True
+
+ res = 0
+ while x > 0:
+ res *= 10
+ res += x % 10
+ x //= 10
+ if is_neg:
+ res = -res
+
+ if res < -2**31 or res > 2**31-1:
+ return 0
+ return res
+
\ No newline at end of file
diff --git a/python/523_Continuous_Subarray_Sum.py b/python/523_Continuous_Subarray_Sum.py
new file mode 100644
index 0000000..d37ceeb
--- /dev/null
+++ b/python/523_Continuous_Subarray_Sum.py
@@ -0,0 +1,20 @@
+class Solution:
+ def checkSubarraySum(self, nums: List[int], k: int) -> bool:
+ # remeainders[0] = 0 is for when x == 0
+ remainders = dict()
+ remainders[0] = 0
+ pre_sum = 0
+
+ for idx, item in enumerate(nums):
+ pre_sum += item
+ remaind = pre_sum % k
+
+ # remainder doesnt exist then it has to be init
+ # if it exists, then check the prev one has the same remainder
+ if remaind not in remainders:
+ remainders[remaind] = idx + 1
+ elif remainders[remaind] < idx:
+ return True
+
+ return False
+
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