Skip to content

Commit 279abd6

Browse files
committed
Adding Complex Solution for Problem - 1290 - Convert Binary Number To Integer
1 parent 9ff5edd commit 279abd6

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Diff for: Convert_Binary_Number_To_Int/ComplexSolution.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 1290
6+
## Problem Name: Convert Binary Number in a Linked List to Integer
7+
##===================================
8+
#
9+
#Given head which is a reference node to a singly-linked list.
10+
#The value of each node in the linked list is either 0 or 1.
11+
#The linked list holds the binary representation of a number.
12+
#
13+
#Return the decimal value of the number in the linked list.
14+
#
15+
#Example 1:
16+
#
17+
#Input: head = [1,0,1]
18+
#Output: 5
19+
#Explanation: (101) in base 2 = (5) in base 10
20+
#
21+
#Example 2:
22+
#
23+
#Input: head = [0]
24+
#Output: 0
25+
#
26+
#Example 3:
27+
#
28+
#Input: head = [1]
29+
#Output: 1
30+
#
31+
#Example 4:
32+
#
33+
#Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
34+
#Output: 18880
35+
#
36+
#Example 5:
37+
#
38+
#Input: head = [0,0]
39+
#Output: 0
40+
class Solution:
41+
def getDecimalValue(self, head):
42+
tmp = head
43+
count = 0
44+
while tmp:
45+
count += 1
46+
tmp = tmp.next
47+
tmpList = []
48+
for i in range(count - 1, -1, -1):
49+
j = 2**i
50+
tmpList.append(j)
51+
tmp = head
52+
emptyList = []
53+
while tmp:
54+
val = tmp.val
55+
emptyList.append(val)
56+
tmp = tmp.next
57+
for i in range(len(emptyList)):
58+
for j in range(len(tmpList)):
59+
ab = [emptyList[i]*tmpList[i] for i in range(len(emptyList))]
60+
sum = 0
61+
for i in range(len(ab)):
62+
sum = sum + ab[i]
63+
return sum

0 commit comments

Comments
 (0)