当前位置:网站首页>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)边栏推荐
- How to change the IP address of ECS? What are the precautions for changing the IP address
- 问题:sql创建存储过程
- 解析后人类时代类人机器人的优越性
- Bi-sql - Select
- DP summary of ACM in recent two weeks
- event
- Deep learning common optimizer summary
- What is an evpn switch?
- Customer disaster recovery case - a MySQL database migration scheme
- 集成阿里云短信服务以及报签名不合法的原因
猜你喜欢

SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义

Doctor application | Hong Kong University of science and Technology (Guangzhou) Mr. Liu Hao recruits the full award doctor / Master in data mining

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole

An interface testing software that supports offline document sharing in the Intranet

Training course of mixed accuracy from simple to deep

解析90后创客教育的主观积极性

SAP mts/ato/mto/eto topic 8: ATO mode 2 d+ empty mode strategy 85

Idea creates a servlet and accesses the 404 message

2022年二级造价工程师备考攻略,你准备好了吗?

SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization
随机推荐
Jimureport building block report - what problems does the layout design solve?
An interface testing software that supports offline document sharing in the Intranet
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
What are the advantages of ECS? Is ECS better than VM?
Application and related research of Worthington elastase
Worthington木瓜蛋白酶化学性质和特异性
Introduction to C language custom types (structure, enumeration, union, bit segment)
Lldb common commands
Qiming cloud sharing: tips on esp32c3 simple IO and serial port
Abnova peptide design and synthesis solutions
数据库解答建标,按要求回答
Getattribute return value is null
Weibo International Edition changed its name to Weibo light sharing Edition
Spirit breath development log (16)
External network access SVN server (external network access SVN server deployed on the cloud)
After purchasing Tencent ECs, how to solve packet loss in Internet access?
Zhang Xiaodan, chief architect of Alibaba cloud hybrid cloud: evolution and development of government enterprise hybrid cloud technology architecture
Collagenase -- four types of crude collagenase from Worthington
ribbon
SAP MTS/ATO/MTO/ETO专题之十:ETO模式 Q+空模式 未估价库存 策略自定义