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)的更多相关文章

  1. leetcode菜鸡斗智斗勇系列(7)--- 用最小的时间访问所有的节点

    1.原题: https://leetcode.com/problems/minimum-time-visiting-all-points/ On a plane there are n points ...

  2. leetcode菜鸡斗智斗勇系列(2)--- 把一个ipv4地址转换成一串数字

    1.原题: https://leetcode.com/problems/defanging-an-ip-address/ 这道题本身很简单, Given a valid (IPv4) IP addre ...

  3. leetcode菜鸡斗智斗勇系列(6)--- 检查一个string里面有几个对称的字段

    1.原题: https://leetcode.com/problems/split-a-string-in-balanced-strings/ Split a String in Balanced S ...

  4. leetcode菜鸡斗智斗勇系列(4)--- 单一数字的乘积和总合的减法

    1.原题: https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ Given an i ...

  5. leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石

    1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...

  6. leetcode菜鸡斗智斗勇系列(10)--- Decrypt String from Alphabet to Integer Mapping

    1.原题: https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/submissions/ Giv ...

  7. 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 ...

  8. 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 ...

  9. leetcode菜鸡斗智斗勇系列(5)--- 寻找拥有偶数数位的数字

    1.原题: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/ Given an array nums of ...

随机推荐

  1. Java并发编程:Callable、Future和FutureTask【转】

    原文链接:http://www.cnblogs.com/dolphin0520/p/3949310.html 创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这 ...

  2. 版本控制神器——git的基本使用

    git基础命令 安装git windows的话,直接下载安装即可 Linux Ubuntu安装,apt-get install git Linux Centos安装,yum install git 配 ...

  3. 【Android - 自定义View】之自定义View实现“刮刮卡”效果

    首先来介绍一下这个自定义View: (1)这个自定义View的名字叫做 GuaguakaView ,继承自View类: (2)这个View实现了很多电商项目中的“刮刮卡”的效果,即用户可以刮开覆盖层, ...

  4. Spring Boot SpringApplication启动类(一)

    目录 目录 前言 1.起源 2.SpringApplication 准备阶段 2.1.推断 Web 应用类型 2.2.加载应用上下文初始器 ApplicationContextInitializer ...

  5. Java 基于Spire.Cloud.Excel 将Excel转为PDF

    Spire.Cloud.Excel Sdk 提供GeneralApi接口和WorkbookApi接口,支持将本地Excel和云端Excel文档转换为ODS, PDF, XPS, PCL, PS等格式. ...

  6. webpack入门指南(基于webpack v4.41.2)

    2019年12月5日初稿,目前webpack已经更新到v4.41.2,本文正是基于该版本,在windows8.1操作系统下进行的demo编译,适用于想入门webpack的前端开发人员. webpack ...

  7. MySQL必知必会(Select, Order by子句)

    SELECT prod_name FROM products ORDER BY prod_name; SELECT prod_id, prod_price, prod_name FROM produc ...

  8. Mac 中环境变量的配置

    1. 基本了解 1.1. 查看当前path 在讲解Mac环境变量配置之前,大家可以打开dos框,输入 echo $PATH 查看当前的path. 本机结果: /usr/local/bin:/usr/l ...

  9. UVA-11987

    I hope you know the beautiful Union-Find structure. In this problem, you're to implement somethingsi ...

  10. 【C语言】%f,%lf,%3.1f

    在输出时应注意变量类型,使用如%3.1时会默认四舍五入.