当前位置:网站首页>[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 .
边栏推荐
- 快手重点整治搬运、洗稿等方式的养号行为,自媒体平台如何净化内容生态
- 保护性拷贝&无状态
- The method of tracking the real-time market of London Silver
- 【commons-lang3专题】001-StringUtils 专题
- SQL Server 只有数据库文件,没有日志文件,恢复数据时报1813错误的解决方案
- 第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
- Some considerations about ThreadPool
- Wechat campus bathroom reservation applet graduation design finished product (5) assignment
- DDD领域驱动设计如何进行工程化落地
- Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
猜你喜欢

Data warehouse construction - ads floor

SystemVerilog-连接和复制运算符

mysql存储过程 实现创建一张表(复制原表的结构新建的表)

ZABBIX deployment and monitoring

Irregular clipping of NC data with CDO

Summary of preprocessing methods for time series data

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板

Talk about the cross end technical scheme

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template

Anomaly detection and unsupervised learning (2)
随机推荐
SystemVerilog-连接和复制运算符
Meeting notification & meeting feedback & feedback details function of meeting OA project
Station B "crashed" from beginning to end 2021.07.13 we collapsed like this (Reprint)
UE4 调试常用的打印信息方法
【树莓派】widows电脑如何与树莓派连接
COPU陆首群教授应邀在开放原子全球开源峰会上做主旨演讲
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“
Isolation level of MySQL, possible problems (dirty reading, unrepeatable reading, phantom reading) and their solutions
将行内元素转换为块元素的方法
将Word中的表格以图片形式复制到微信发送
【无标题】
Error reporting: Rong Lianyun sends SMS verification code message 500
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]
Download the latest version of visual studio code and connect to the server remotely (very detailed)
Several methods of multi-threaded sequential operation can be asked casually in the interview
芯驰科技发布G9系列最新旗舰产品,配备6个1.8Ghz主频的A55核心
Wechat campus bathroom reservation applet graduation design finished product (5) assignment
Main thread and daemon thread
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
Tips for API interface optimization