当前位置:网站首页>[notes for question brushing] binary linked list to integer
[notes for question brushing] binary linked list to integer
2022-07-29 01:00:00 【Qihai】
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.source : Power button (LeetCode)
link :https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer
solution :
First of all, this single linked list stores binary numbers . If you want to take out every bit of binary, you need to traverse the linked list .
Ordinary thinking is to traverse it first and record how many bits there are , Then each round is stored in an array , Then use the number of bits of each bit (2^(n - 1)) Multiply by this number and add to get . But this is not only inefficient , And the array is easy to cross the boundary and this n Poorly controlled factors .
that , We can use decimal numbers in turn to think about : For example, in an array, each subscript stores an integer of one bit (0~9), such as int a[] = {1, 2, 3, 9, 2} So let's think about how to make it into a number, that is 12392 Well ?
Multiplicative multiplication The role of the . For example, the first time I got 1, So first come in and put the first 0 multiply 10, And then add , That is to say, increase the original to one decimal , Become one higher than the number that comes in now , The low position is 0 了 , You can directly add , And then, in turn , You can find that the problem can be solved by using one traversal perfectly .
Binary isomorphism , Only binary is full two into one , So multiply by 2 that will do .
The code is as follows :
int getDecimalValue(struct ListNode* head){
int num = 0;
struct ListNode* temp = head;
while(temp)
{
num = num * 2 + temp->val;
temp = temp->next;
}
return num;
}however , The first thing we should think of is that it is a binary number , What is most relevant to binary numbers is bit operation , So how to say that there is no bit operation !?
You can follow the above steps , Move the original number one bit to the left each time , There will be one more on the far right 0, Then press the bit or 1 Namely 1 Otherwise, it would be 0 that will do .
// Binary system -- An operator
int getDecimalValue(struct ListNode* head){
int num = 0;
struct ListNode* temp = head;
while(temp)
{
num <<= 1;
num |= temp->val;
temp = temp->next;
}
return num;
}Think clearly about the logic of each step .
边栏推荐
- JWT token related configuration (global configuration identity authentication rewrites authenticate method)
- CUDA related
- [untitled]
- Breadth first search (BFS) and its matlab code
- 【Web开发】Flask框架基础知识
- Definition of double linked list~
- Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
- 第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
- SystemVerilog-连接和复制运算符
- rk3399 9.0驱动添加Powser按键
猜你喜欢

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】

The method of tracking the real-time market of London Silver

Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping

B- 树 ~

Seven marketing strategies of NFT project
![[web development] basic knowledge of flask framework](/img/79/5ece84552c82e98f5e15fac1ee3335.png)
[web development] basic knowledge of flask framework

自制 | 纯手工自制一个16位RISC架构CPU

关于ThreadPool的一些注意事项

selenium对接代理与seleniumwire访问开发者工具NetWork
![[untitled]](/img/28/db3b2e1985dc9acf41cdf2004ea0d5.png)
[untitled]
随机推荐
mysql存储过程 实现创建一张表(复制原表的结构新建的表)
深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
[Yugong series] go teaching course in July 2022, an array of 020 go containers
如何在WordPress中创建一个自定义404错误页面
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
The method of tracking the real-time market of London Silver
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
SystemVerilog-连接和复制运算符
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
如何给女友讲明白JS的bind模拟实现
Educational Codeforces Round 132 (Rated for Div. 2)【A~C】
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
armeabi-v7a架构(sv7a)
Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology
[Commons lang3 topic] 004- numberutils topic
浅谈一下跨端技术方案
追踪伦敦银实时行情的方法有哪些?
Error reporting: Rong Lianyun sends SMS verification code message 500