Skip to content

Commit bab8add

Browse files
Add 1290_Convert_Binary_Number_in_a_Linked_List_to_Integer.py and 1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py (qiyuangong#8)
* 1290_Convert_Binary_Number_In_A_Linked_List_To_Integer.py * 1342_Number_of_Steps_to_Reduce_a_Number_to_Zero.py Contributed by @srivastavaayu
1 parent a2c3238 commit bab8add

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +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 |
225226
| 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 |
226228
| 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) |
227229
| 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 |
228230

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Given head which is a reference node to a singly-linked list.
3+
The value of each node in the linked list is either 0 or 1.
4+
The linked list holds the binary representation of a number.
5+
Return the decimal value of the number in the linked list.
6+
Example 1:
7+
Input: head = [1,0,1]
8+
Output: 5
9+
Explanation: (101) in base 2 = (5) in base 10
10+
Example 2:
11+
Input: head = [0]
12+
Output: 0
13+
Example 3:
14+
Input: head = [1]
15+
Output: 1
16+
Example 4:
17+
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
18+
Output: 18880
19+
Example 5:
20+
Input: head = [0,0]
21+
Output: 0
22+
Constraints:
23+
The Linked List is not empty.
24+
Number of nodes will not exceed 30.
25+
Each node's value is either 0 or 1.
26+
'''
27+
28+
# Definition for singly-linked list.
29+
# class ListNode:
30+
# def __init__(self, x):
31+
# self.val = x
32+
# self.next = None
33+
34+
class Solution:
35+
def getDecimalValue(self, head: ListNode) -> int:
36+
binary_numbers_list=[]
37+
binary_numbers_list.append(head.val)
38+
while(head.next!=None):
39+
head=head.next
40+
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
47+
return answer
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given a non-negative integer num, return the number of steps to reduce it to zero.
3+
If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1 from it.
4+
5+
Example 1:
6+
Input: num = 14
7+
Output: 6
8+
Explanation:
9+
Step 1) 14 is even; divide by 2 and obtain 7.
10+
Step 2) 7 is odd; subtract 1 and obtain 6.
11+
Step 3) 6 is even; divide by 2 and obtain 3.
12+
Step 4) 3 is odd; subtract 1 and obtain 2.
13+
Step 5) 2 is even; divide by 2 and obtain 1.
14+
Step 6) 1 is odd; subtract 1 and obtain 0.
15+
16+
Example 2:
17+
Input: num = 8
18+
Output: 4
19+
Explanation:
20+
Step 1) 8 is even; divide by 2 and obtain 4.
21+
Step 2) 4 is even; divide by 2 and obtain 2.
22+
Step 3) 2 is even; divide by 2 and obtain 1.
23+
Step 4) 1 is odd; subtract 1 and obtain 0.
24+
25+
Example 3:
26+
Input: num = 123
27+
Output: 12
28+
29+
Constraints:
30+
0 <= num <= 10^6
31+
'''
32+
33+
class Solution:
34+
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
40+
else:
41+
num=num-1
42+
steps+=1
43+
return steps
44+

0 commit comments

Comments
 (0)