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 ...
随机推荐
- 小白学 Python 爬虫(11):urllib 基础使用(一)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- Java 从入门到进阶之路(十)
之前的文章我们介绍了一下 Java 中的引用型数组类型,接下来我们再来看一下 Java 中的继承. 继承的概念 继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类. 继承就是子类继 ...
- linux 系统自动定制运行 crontab
在UNIX下怎样实现和Windows下“计划任务”一样的功能 $crontab -e 编辑脚本 $crontab -l 察看脚本 用$crontab -e 编辑脚本,加入下列行 :分 小时 星期 ...
- Leecode_98_Validate_Binary_Search_Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 用Cocoapods并结合Github管理,导入,更新,下载自己的SDK
转自:http://www.jianshu.com/p/129d3c315ea6
- hdu1710 Binary Tree Traversals
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 题意:给前序.中序求后序,多组 前序:根左右 中序:左右根 分析:因为前序(根左右)最先出现的总 ...
- spring security 权限安全认证框架-入门(一)
spring security 概述: Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架.它是保护基于spring的应用程序的实际标准. Spring Security ...
- 最简单易懂的linux系统基础优化
第10章 linux的系统优化 10.1 系统的版本,内核查看 10.1.1 系统版本的查看 [root@oldboy6666 ~]# cat /etc/redhat-release CentOS L ...
- Zabbix与ELK整合实现对日志数据的实时监控
4.2.zabbix平台配置日志告警 一. ELK与zabbix有什么关系? ELK大家应该比较熟悉了,zabbix应该也不陌生,那么将ELK和zabbix放到一起的话,可能大家就有疑问了?这两个放到 ...
- Linux IO多路复用之epoll网络编程
前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网络读取一 ...