Skip to content

Commit a180654

Browse files
committed
Change link of 1290 and 1342. Fix code style
1 parent bab8add commit a180654

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
222222
| 1089 | [Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1089_Duplicate_Zeros.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1089_Duplicate_Zeros.java) | 2 Pass, store last position and final move steps, O(n) and O(1) |
223223
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1108_Defanging_an_IP_Address.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1108_Defanging_an_IP_Address.java) | String manipulate (split, replace and join), O(n) and O(n) |
224224
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1260_Shift_2D_Grid.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1260_Shift_2D_Grid.java) | Final position of each element can be computed according to k, m and n, e.g., k == mn, then don't move, O(mn) and O(mn) |
225-
| 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Python](https://github.com/srivastavaayu/leetcode-1/blob/master/python/1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py) | Take 2 to the power digit position from right (starting from 0) and multiply it with the digit |
225+
| 1290 | [Convert Binary Number in a Linked List to Integer](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py) | Take 2 to the power digit position from right (starting from 0) and multiply it with the digit |
226226
| 1337 | [The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1337_The_K_Weakest_Rows_in_a_Matrix.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1337_The_K_Weakest_Rows_in_a_Matrix.java) | Check by row, from left to right, until encount first zero, O(mn) and O(1) |
227-
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Python](https://github.com/srivastavaayu/leetcode-1/blob/master/python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py) | If number is divisible by 2, divide the number by 2, else subtract 1 from the number, and output the number of steps |
227+
| 1342 | [Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py) | If number is divisible by 2, divide the number by 2, else subtract 1 from the number, and output the number of steps, O(logn) and O(1) |
228228
| 1365 | [How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1365_How_Many_Numbers_Are_Smaller_Than_the_Current_Number.java) | 1. Sort and get position in sorted nums, O(nlogn) and O(n)<br>2. Fill count into 0-100, O(n) and O(1) |
229229
| 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1480_Running_Sum_of_1d_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1480_Running_Sum_of_1d_Array.java) | 1. Go through the array, O(n) and O(1)<br>2. Accumulate API |
230230

python/1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@
3333

3434
class Solution:
3535
def getDecimalValue(self, head: ListNode) -> int:
36-
binary_numbers_list=[]
36+
binary_numbers_list = []
3737
binary_numbers_list.append(head.val)
38-
while(head.next!=None):
39-
head=head.next
38+
while(head.next is not None):
39+
head = head.next
4040
binary_numbers_list.append(head.val)
41-
answer=0
42-
power=0
43-
for digit in range(len(binary_numbers_list)-1,-1,-1):
44-
if(binary_numbers_list[digit]>0):
45-
answer+=((2**power)*binary_numbers_list[digit])
46-
power+=1
41+
answer = 0
42+
power = 0
43+
# from len(binary_numbers_list) - 1 -> 0
44+
for digit in range(len(binary_numbers_list) - 1, -1, -1):
45+
if(binary_numbers_list[digit] > 0):
46+
answer += ((2 ** power) * binary_numbers_list[digit])
47+
power += 1
4748
return answer

python/1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@
3232

3333
class Solution:
3434
def numberOfSteps (self, num: int) -> int:
35-
steps=0
36-
while(num>0):
37-
if(num%2==0):
38-
num=num/2
39-
steps+=1
35+
steps = 0
36+
while(num > 0):
37+
if(num % 2 == 0):
38+
num = num / 2
39+
steps + =1
4040
else:
41-
num=num-1
42-
steps+=1
41+
num = num - 1
42+
steps += 1
4343
return steps
44-
44+
45+
# def numberOfSteps (self, num: int) -> int:
46+
# ans = 0
47+
# # check the number of 1
48+
# while num:
49+
# ans += (num & 1) + 1
50+
# num >>= 1
51+
# return ans - 1
52+
53+
# def numberOfSteps (self, num: int) -> int:
54+
# ones, zeros = self.bitCount(num)
55+
# if ones == 0:
56+
# return 0
57+
# else:
58+
# return 2 * ones + zeros - 1

0 commit comments

Comments
 (0)