leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)
Convert Binary Number in a Linked List to Integer这道题在leetcode上面算作是“easy”,然而小生我还是不会做,于是根据大佬的回答来整理一下思路以便日后复习。
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/
1.原题:
Given head which 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.
翻译:Head是一个单链表的引用,每个链表的元素都是1或者0,链表的数字们组成起来代表一个二进制数字(比如说 [1,0,1]代表二进制的101)。你需要返回这个二进制所代表的十进制数字。
输入输出:
Input: head = [1,0,1]
Output: 5
这是单链表的定义:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
2.解题思路:
首先作为算法题,大多数语言都可以用,作者这里习惯,用的是C++。
这道题考验的主要是答题者对编程语言中二进制的了解。
a.解题所需要的知识:
二进制的101代表十进制的5,这是因为 4 + 1 = 5。这个不用说了吧。
而我们要注意的就是 “<<”,这个不是我们熟知的C++输出符,而是指往左移,后面的数字是指左移1一位。假设ret是3,ret <<= 1 之后就会ret = 6。
|= 意思为:按位或后赋值,也就是给对应的二进制的数字的位置赋值,假设ret是7,ret |= 4之后ret仍然是7,因为7是0111,4是0100,注意这4这个位置已经有1了所以已经赋值了,因此结果不变。
head->val就是指链表里面的val。关于链表的定义可以参考:https://blog.csdn.net/slandarer/article/details/91863177
b.解题思路:
解题思路见注释,非常通俗易懂。
参考答案:
class Solution {
public:
int getDecimalValue(ListNode* head) {
int ret = 0; //设置ret为0。
while(head) //在head都不为NULL的情况下继续循环
{
ret <<= 1;
//我们会把ret左移,所以说如果之前已经有一位1了.
//那就会被推到更高的位置,比如说之前为0001,那么现在就是0010
ret |= head->val;
//如果head->val是1,就给这一层赋值1,如果是0,就维持不变。
//例子:比如说之前我们已经得到了0010,现在如果为1,就是0011;如果是0,就是0010不变。
head = head->next;
//指向下一个链表元素。
}
return ret;
}
};
leetcode菜鸡斗智斗勇系列(1)---把一个链表中的二进制数字转换为一个整型数(int)的更多相关文章
- leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点
1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...
- leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字
1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...
- leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段
1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...
- leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法
1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping
1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...
- leetcode菜鸡斗智斗勇系列(9)--- Range Sum of BST
1.原题: https://leetcode.com/problems/range-sum-of-bst/ Given the root node of a binary search tree, r ...
- leetcode菜鸡斗智斗勇系列(8)--- Find N Unique Integers Sum up to Zero
1.原题: https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ Given an integer n, retur ...
- leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字
1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...
随机推荐
- 纵论WebAssembly,JS在性能逆境下召唤强援
webassembly的作用 webassembly是一种底层的二进制数据格式和一套可以操作这种数据的JS接口的统称.我们可以认为webassembly的范畴里包含两部分 wasm: 一种体积小.加载 ...
- Git实战指南----跟着haibiscuit学Git(第二篇)
笔名: haibiscuit 博客园: https://www.cnblogs.com/haibiscuit/ Git地址: https://github.com/haibiscuit?tab=re ...
- FileReader.result
FileReader.result 该属性返回文件的内容.此属性仅在读取操作完成后才有效,并且数据的格式取决于用于启动读取操作的方法.FileReader]**result** 句法 var file ...
- react中将svg做成icon组件在其他模块调用
开发前端页面,经常会有很多共用的图标icon,那么我们把它单独做成组件,以便后期重复调用! 首先在components 的icons文件夹下创建BaseIcon.js文件. 我们需要先在命令行安装gl ...
- Linux集群介绍、keepalived介绍及配置高可用集群
7月3日任务 18.1 集群介绍18.2 keepalived介绍18.3/18.4/18.5 用keepalived配置高可用集群扩展heartbeat和keepalived比较http://blo ...
- TC297B - 外设头文件解析(以IO为例)
打开例程,目录树下的Includes中包含了各个片上资源对应的头文件,这些头文件定义了相应外设的寄存器地址(寄存器是内置于各个 IP 外设中,是一种用于配置外设功能的存储器,就是一种内存,并且有相对应 ...
- 接口访问报错:The valid characters are defined in RFC 7230 and RFC 3986
写了个接口,在测试访问的时候,需要传json串,但是后台报错了 The valid characters are defined in RFC 7230 and RFC 3986 当前使用的tomca ...
- 简单理解http协议的特性
http协议一种数据传输的规范,像我们的在发送数据的时候,我们无法保证发送与接收的类型是一致的,它就保证了我们传输的同一个类型数据. 特性: 1.灵活:我们不管传输什么数据,图片,文件,文字,都可以进 ...
- Java基础面试相关
面试相关的问题(下) 四 Linux高级_ 1.Linux机器 变慢,怎么查看? (1)整机的性能 主要查看的是CPU和内存 先查看整机的top,使用命令 top 虚拟机 使用1可以查看哪个核被占用过 ...
- CF372C Watching Fireworks is Fun(单调队列优化DP)
A festival will be held in a town's main street. There are n sections in the main street. The sectio ...