【leetcode】1290. Convert Binary Number in a Linked List to Integer
题目如下:
Given
headwhich is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.Return the decimal value of the number in the linked list.
Example 1:
Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10Example 2:
Input: head = [0]
Output: 0Example 3:
Input: head = [1]
Output: 1Example 4:
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880Example 5:
Input: head = [0,0]
Output: 0Constraints:
- The Linked List is not empty.
- Number of nodes will not exceed
30.- Each node's value is either
0or1.
解题思路:送分题,遍历一遍链表,把每个节点的值拼接成字符串,最后做进制转换。
代码如下:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def getDecimalValue(self, head):
"""
:type head: ListNode
:rtype: int
"""
binary = ''
while head != None:
binary += str(head.val)
head = head.next
return int(binary,2)
【leetcode】1290. Convert Binary Number in a Linked List to Integer的更多相关文章
- [Algorithm] 1290. Convert Binary Number in a Linked List to Integer
Given head which is a reference node to a singly-linked list. The value of each node in the linked l ...
- LeetCode 1290. Convert Binary Number in a Linked List to Integer
题目 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN ...
- 【LeetCode】405. Convert a Number to Hexadecimal 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
随机推荐
- Netty源码剖析-业务处理
参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! ----主线:worker thread 触发pipeline.fi ...
- LC 94. Binary Tree Inorder Traversal
问题描述 Given a binary tree, return the inorder traversal of its nodes' values. (左 - 根 - 右) Example: In ...
- xss level11
Level11 (1) (2)毫无头绪,查看PHP源代码发现,是从头文件的referer获取的输入. (3)用Burp抓包,修改头文件如下: (4)再点击Proxy界面的forward,回到浏览器页面 ...
- kubernetes 健康检查和初始化容器
Pod-hook:postStart:1.$ $ vim preStart-hook.yaml---apiVersion: v1kind: Podmetadata: name: hook-demo1 ...
- 针对Web的信息搜集
信息收集(Information Gathering),信息收集是指通过各种方式获取所需要的信息,在整个渗透测试环节中,信息搜集是整个渗透过程中最为重要的一环,信息搜集可占据整个渗透测试80%左右的工 ...
- seaborn:一个更强大的画图工具
数据加载: 1. Countplot 和之前的pandas绘图相比,使用countplot可以自动计算每类的数量. 2. KDE Plot KDE,是"kernel density esti ...
- Python下载安装及验证
目录: 一.Python介绍 二.python安装及验证 一.Python介绍 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个 ...
- element之input输入搜索联想框
1. 模板代码 <el-autocomplete :minlength="2" v-model="searchName" :fetch-suggestio ...
- 第二章、Django以及数据库的配置
目录 第二章.Django以及数据库的配置 一.小白必会三板斧 二.静态文件配置 三.form表单 action和method参数可以写的形式 四.request对象及方法 五.django连接数据库 ...
- 智能指针原理及实现(2)unique_ptr
只允许基础指针的一个所有者. 可以移到新所有者(具有移动语义),但不会复制或共享(即我们无法得到指向同一个对象的两个unique_ptr). 替换已弃用的 auto_ptr. 相较于 boost::s ...
