当前位置:网站首页>LeetCode 1290. Binary linked list to integer
LeetCode 1290. Binary linked list to integer
2022-06-24 04:51:00 【freesan44】
subject
Give you a single chain table reference node head. The value of each node in the list is not 0 Namely 1. It is known that this list is a binary representation of an integer number .
Please go back to the Decimal value .
Example 1: Input :head = [1,0,1] Output :5 explain : Binary number (101) Convert to decimal (5) Example 2: Input :head = [0] Output :0 Example 3: Input :head = [1] Output :1 Example 4: Input :head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0] Output :18880 Example 5: Input :head = [0,0] Output :0
Tips :
Link list is not empty .
The total number of nodes in the list does not exceed 30.
The value of each node is not 0 Namely 1.
Their thinking
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
resStr = ""
while head != None:
resStr += str(head.val)
head = head.next
# Binary to decimal int( Binary value ,2)
# print(resStr)
return int(resStr, 2)
if __name__ == '__main__':
# # Sample linked list , Pay attention to copying !
# #L1 1->2->3->4->5
# l1 = ListNode(1,ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))
# #L2 1->3->4
# l2 = ListNode(1, ListNode(3, ListNode(4)))
# list1 = [1, 2, 3, 3, 2, 1]
# list1 = [1, 0, 1]
list1 = [1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0]
nodeL1 = ListNode()
headL1 = nodeL1
while len(list1) >0:
node = ListNode(list1.pop(0))
nodeL1.next = node
nodeL1 = node
headL1 = headL1.next
tempHeadL1 = headL1
while(tempHeadL1.next != None):
# print(tempHeadL1.val)
tempHeadL1 = tempHeadL1.next
ret = Solution().getDecimalValue(headL1)
print(ret)边栏推荐
- Facebook internal announcement: instant messaging will be re integrated
- What is the principle of Ping? How does the server disable Ping?
- I have an agreement with IOT
- Loss and optimization of linear regression, machine learning to predict house prices
- Abnova多肽设计和合成解决方案
- SAP MTS/ATO/MTO/ETO专题之七:ATO模式1 M+M模式策略用82(6892)
- 少儿编程教育在特定场景中的普及作用
- Worthington胰蛋白酶的物化性质及特异性
- mysql中表的命名
- Abnova膜蛋白脂蛋白体解决方案
猜你喜欢

Training course of mixed accuracy from simple to deep

Loss and optimization of linear regression, machine learning to predict house prices
Advanced authentication of uni app [Day12]

Abnova peptide design and synthesis solutions

线性回归的损失和优化,机器学习预测房价

Weibo International Edition changed its name to Weibo light sharing Edition

Introduction to C language custom types (structure, enumeration, union, bit segment)

SAP MTS/ATO/MTO/ETO专题之七:ATO模式1 M+M模式策略用82(6892)

Apipost interface assertion details

少儿编程课程改革后的培养方式
随机推荐
Popularization of children's programming education in specific scenarios
How to enlarge the ECS page? How to select ECS instance specifications?
An interface testing software that supports offline document sharing in the Intranet
What is the new generation cloud computing architecture cipu of Alibaba cloud?
Abnova膜蛋白脂蛋白体解决方案
event
Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs
How to install software on ECs is it expensive to rent ECS
Integration of Alibaba cloud SMS services and reasons for illegal message signing
Abnova荧光原位杂交(FISH)探针解决方案
Collagenase -- four types of crude collagenase from Worthington
Let children learn the application essence of steam Education
Specificity and correlation of Worthington deoxyribonuclease I
Beauty of script │ VBS introduction interactive practice
Bi-sql insert into
oracle数据库提示无操作权限的问题
问题:sql创建存储过程
让孩子们学习Steam 教育的应用精髓
Replication of variables in golang concurrency
查找GBase 8c数据库当前索引?