当前位置:网站首页>[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 .
边栏推荐
- DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
- seleniumwire获取百度指数
- Anomaly detection and unsupervised learning (1)
- 靠云业务独撑收入增长大梁,微软仍然被高估?
- QT静态编译程序(Mingw编译)
- 小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- Necessary interview skills for Android (including interview questions and learning materials)
- Rk3399 9.0 driver add powser button
- Huawei releases harmonyos 3.0, taking another step towards "Internet of all things"
猜你喜欢

【无标题】

【愚公系列】2022年07月 Go教学课程 020-Go容器之数组

B-tree~

【Web开发】Flask框架基础知识

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“

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

Talk about the cross end technical scheme

ZABBIX deployment and monitoring

Data warehouse construction - ads floor
随机推荐
【树莓派】widows电脑如何与树莓派连接
Interview shock 69: is TCP reliable? Why?
SystemVerilog-连接和复制运算符
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
【unity】将unity编辑c#配置为vscode
[untitled]
Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Data warehouse construction - DWT floor
SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案
Several methods of multi-threaded sequential operation can be asked casually in the interview
What are the methods to track the real-time market of London Silver?
Cookie和Session
Error reporting: Rong Lianyun sends SMS verification code message 500
异步模式之工作线程
Breadth first search (BFS) and its matlab code
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
Method of converting inline elements to block elements
华为发布HarmonyOS 3.0,向“万物互联”再迈一步
seleniumwire获取百度指数