Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Python/101.SymmetricTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def isSymmetric(self, root: TreeNode) -> bool:
level.append(node.val if node else None)
length -= 1
if node:
queue.append(node.left)
queue.append(node.right)
queue.extend((node.left, node.right))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.isSymmetric refactored with the following changes:

i = 0
j = len(level) - 1
while i <= j:
Expand Down
12 changes: 8 additions & 4 deletions Python/150.EvaluateReversePolishNotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ def evalRPN(self, tokens: List[str]) -> int:
else:
y = stack.pop()
x = stack.pop()
if token == '+': stack.append(x + y)
if token == '-': stack.append(x - y)
if token == '*': stack.append(x * y)
if token == '/': stack.append(int(x / y))
if token == '*':
stack.append(x * y)
elif token == '+':
stack.append(x + y)
elif token == '-':
stack.append(x - y)
elif token == '/':
stack.append(int(x / y))
Comment on lines -15 to +22
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.evalRPN refactored with the following changes:

  • Simplify conditional into switch-like form (switch)

return stack[0]
7 changes: 3 additions & 4 deletions Python/496_nextgreaterelement.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Solution:
def check(self, number, nums1, nums2):
index = nums2.index(number)
for _ in range(index, len(nums2)):
if(nums2[_]>number):
return nums2[_]
return -1
return next(
(nums2[_] for _ in range(index, len(nums2)) if (nums2[_] > number)), -1
)
Comment on lines -4 to +6
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.check refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)


def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
return [self.check(x, nums1, nums2) for x in nums1]
2 changes: 1 addition & 1 deletion Python/50.Powxn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class Solution:
def myPow(self, x: float, n: int) -> float:
if not n: return 1
power = self.myPow(x, int(n/2))
power = self.myPow(x, n // 2)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.myPow refactored with the following changes:

if n % 2 == 0:
return power * power
else:
Expand Down
5 changes: 1 addition & 4 deletions Python/5_LongestPalindromicSubstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ def longestPalindrome(self, s: str) -> str:
left,right = i,i
ls = ""
while 0<=left<size and 0<=right<size and s[left]==s[right]:
if left==right:
ls = s[left]
else:
ls = s[left] + ls + s[right]
ls = s[left] if left==right else s[left] + ls + s[right]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.longestPalindrome refactored with the following changes:

left-=1
right+=1
#Even Palindrome
Expand Down
17 changes: 7 additions & 10 deletions Python/994_Rotting_Oranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.orangesRotting refactored with the following changes:

count = 0
rotten = []
for i in range(n):
Expand All @@ -15,13 +15,13 @@ def orangesRotting(self, grid: List[List[int]]) -> int:
count+=1
elif grid[i][j] == 1:
count+=1


if count == 0:
return 0

cycles = 0

while rotten:
count-=len(rotten)
newRotten = []
Expand All @@ -32,11 +32,8 @@ def orangesRotting(self, grid: List[List[int]]) -> int:
self.destroy(grid,i,j-1,n,m,newRotten)
rotten = newRotten
cycles+=1

if count>0:
return -1
else:
return cycles-1

return -1 if count>0 else cycles-1

def destroy(self, grid, i, j, n, m, rotten):
if 0<=i<n and 0<=j<m:
Expand Down
17 changes: 10 additions & 7 deletions Python/SmallestDifference.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
-10^6 <= arr[i] <= 10^6
"""

def smallestDifference(arr1 : list, arr2 : list) :
arr1.sort();arr2.sort()
i = 0;j = 0
def smallestDifference(arr1 : list, arr2 : list):
arr1.sort()
arr2.sort()
i = 0
j = 0

smallest = float("inf")
current=float("inf")
smallestPair = []

smallest = float("inf");current=float("inf")
smallestPair = list()

while i < len(arr1) and j < len(arr2) :
fnum = arr1[i];snum = arr2[j]

Comment on lines -41 to +53
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function smallestDifference refactored with the following changes:

if fnum < snum :
current = snum - fnum
i += 1
Expand Down
6 changes: 3 additions & 3 deletions Python/ThreeNumbersSum.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
Output: []
"""

def sum_of_three(arr, target=0) :
result = list()
def sum_of_three(arr, target=0):
result = []
arr.sort()

Comment on lines -32 to +35
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function sum_of_three refactored with the following changes:

for i in range(len(arr)-2) :
left = i+1;right=len(arr)-1

Expand Down
8 changes: 4 additions & 4 deletions Python/baseK.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def sumBase(n: int, k: int) :
def sumBase(n: int, k: int):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function sumBase refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

"""
Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.
After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.
Expand All @@ -9,9 +9,9 @@ def sumBase(n: int, k: int) :
return value : int
"""
summation=0
while n >= k :
while n >= k:

summation = summation + n%k
n=n//k
print(n)
n //= k
print(n)
return (summation + n)
7 changes: 4 additions & 3 deletions Python/contains-duplicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
count = {}
for n in nums :
if count.get(n) != None :
for n in nums:
if count.get(n) is None:
count[n] = 1
else:
return True
count[n] = 1
Comment on lines -9 to -12
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.containsDuplicate refactored with the following changes:

return False
4 changes: 1 addition & 3 deletions Python/detect-capital.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ def detectCapitalUse(self, word: str) -> bool:
num = int(str(num),2)

#Checking if it matches any of the valid number
if num==(1<<(size-1)) or num==(0<<(size-1)) or num==((1<<size)-1):
return True
return False
return num in [1<<(size-1), 0<<(size-1), (1<<size)-1]
10 changes: 2 additions & 8 deletions Python/jumpGame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ def canJump(nums):
while ptr2 >= 0:
if nums[ptr2] >= ptr1 - ptr2:
ptr1 = ptr2
ptr2 -= 1
else :
ptr2 -= 1

if ptr1 == 0:
return True
else:
return False
ptr2 -= 1
return ptr1 == 0
Comment on lines -25 to +26
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function canJump refactored with the following changes:


print(canJump[3,2,1,0,4])
4 changes: 2 additions & 2 deletions Python/longest-valid-parentheses.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def longestValidParentheses(self, s):
ans=0
stack=[-1]
for i in range(len(s)):
if(s[i]=='('):
if (s[i]=='('):
stack.append(i)
else:
stack.pop()
if(len(stack)==0):
if not stack:
Comment on lines -12 to +16
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.longestValidParentheses refactored with the following changes:

stack.append(i)
else:
ans=max(ans,i-stack[-1])
Expand Down
2 changes: 1 addition & 1 deletion Python/rotate-array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def rotate(self, nums: List[int], k: int) -> None:
Do not return anything, modify nums in-place instead.
"""
lenz = len(nums)
k = k % lenz
k %= lenz
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.rotate refactored with the following changes:

  • Replace assignment with augmented assignment (aug-assign)

if k == 0:
return nums
nums += nums[:-k]
Expand Down
14 changes: 6 additions & 8 deletions Python/search-in-rotated-sorted-array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ def search(self, nums: List[int], target: int) -> int:
mid = (low+high)//2
if nums[mid]==target:
return mid
# if left half is sorted

# if left half is sorted
if nums[low]<=nums[mid]:
if nums[low]<=target<nums[mid]:
high = mid-1
else:
low = mid+1

# if right half is sorted

elif nums[mid]<target<=nums[high]:
low = mid+1
else:
if nums[mid]<target<=nums[high]:
low = mid+1
else:
high = mid-1
high = mid-1
Comment on lines -18 to +29
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.search refactored with the following changes:

This removes the following comments ( why? ):

# if right half is sorted

return -1


Expand Down
10 changes: 5 additions & 5 deletions Python/single-number.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution:
def singleNumber(self, nums: List[int]) -> int:
d = dict()
for i in range(len(nums)) :
if nums[i] not in d :
d[nums[i]] = 1
d = {}
for num in nums:
if num not in d:
d[num] = 1
else:
d[nums[i]] += 1
d[num] += 1
Comment on lines -3 to +8
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function singleNumber refactored with the following changes:

for key, value in d.items() :
if value == 1 :
return key
6 changes: 3 additions & 3 deletions Python/wildcard-matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def isMatch(self, s: str, p: str) -> bool:
m,n = len(p),len(s)
if len(p) - p.count("*") > len(s):
return False
dp = [[False]*(n+1) for i in range(m+1)]

dp = [[False]*(n+1) for _ in range(m+1)]
Comment on lines -51 to +52
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Solution.isMatch refactored with the following changes:

for i in range(m+1):
for j in range(n+1):
if j==0 and i==0:
Expand All @@ -58,7 +58,7 @@ def isMatch(self, s: str, p: str) -> bool:
dp[i][j] = False
elif j==0:
dp[i][j] = (p[i-1]=="*" and dp[i-1][j])
elif p[i-1] == s[j-1] or p[i-1]=='?':
elif p[i - 1] in [s[j - 1], '?']:
dp[i][j] = dp[i-1][j-1]
elif p[i-1]=='*':
dp[i][j] = dp[i-1][j] or dp[i][j-1]
Expand Down