当前位置:网站首页>[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 .
边栏推荐
- 散列表 ~
- B-tree~
- Some considerations about ThreadPool
- The method of tracking the real-time market of London Silver
- 【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
- 【无标题】
- Error reporting: the network preview shows {xxx:['this field is required']}
- Surfacecontrol and surfaceflinger communication
- Data warehouse construction - ads floor
- “index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
猜你喜欢

如何给女友讲明白JS的bind模拟实现

Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution

selenium对接代理与seleniumwire访问开发者工具NetWork

Charles -- 从0-1教你如何使用抓包工具

Anomaly detection and unsupervised learning (1)

DDD领域驱动设计如何进行工程化落地

Consumer unit 消费单元

Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers

Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?

NFT 项目的 7 种市场营销策略
随机推荐
Download the latest version of visual studio code and connect to the server remotely (very detailed)
自制 | 纯手工自制一个16位RISC架构CPU
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
Selenium wire obtains Baidu Index
【刷题笔记】从链表中删去总和值为零的连续节点
【无标题】
DRF - deserialization of serializer, fields and parameters, local and global hooks, use of modelserializer
【commons-lang3专题】003- RandomStringUtils 专题
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
主线程与守护线程
[Commons lang3 topic] 005- objectutils topic
深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
【commons-lang3专题】002- RandomUtils 专题
MATLAB02:结构化编程和函数定义「建议收藏」
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
Armeabi-v7a architecture (sv7a)
状态压缩dp-蒙德里安的梦想
浅谈一下跨端技术方案
追踪伦敦银实时行情的方法