Skip to content

Commit 32eb954

Browse files
authored
update: 添加问题“1290.二进制链表转整数”的代码和题解(#1027)
* fix: 昨晚消失的commit Signed-off-by: LetMeFly666 <[email protected]> * docs: en MacOS上忘记push了一个commit - 准备手动merge吧 * temp: windows+MacOS * init+rollback: true hash (#1026) c2a5f47 * 1290: AC.cpp+py+java+go (#1026) cpp - AC,100.00%,5.35% py - AC,100.00%,79.56% java - AC,100.00%,5.95% go - AC,100.00%,92.52% * update: 添加问题“1290.二进制链表转整数”的代码和题解(#1027) Signed-off-by: LetMeFly666 <[email protected]> * fix: 完美一点 (#1026) #1027 (review) --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 9eaee8d commit 32eb954

9 files changed

+309
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-14 23:37:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-14 23:42:31
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* struct ListNode {
14+
* int val;
15+
* ListNode *next;
16+
* ListNode() : val(0), next(nullptr) {}
17+
* ListNode(int x) : val(x), next(nullptr) {}
18+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
int getDecimalValue(ListNode* head) {
24+
int ans = 0;
25+
while (head) {
26+
ans = ans * 2 + head->val;
27+
head = head->next;
28+
}
29+
return ans;
30+
}
31+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-14 23:37:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-14 23:47:42
6+
*/
7+
package main
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* type ListNode struct {
12+
* Val int
13+
* Next *ListNode
14+
* }
15+
*/
16+
func getDecimalValue(head *ListNode) (ans int) {
17+
for ; head != nil; head = head.Next {
18+
ans = ans * 2 + head.Val
19+
}
20+
return
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-14 23:37:54
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-14 23:45:19
6+
*/
7+
/**
8+
* Definition for singly-linked list.
9+
* public class ListNode {
10+
* int val;
11+
* ListNode next;
12+
* ListNode() {}
13+
* ListNode(int val) { this.val = val; }
14+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
15+
* }
16+
*/
17+
class Solution {
18+
public int getDecimalValue(ListNode head) {
19+
int ans = 0;
20+
while (head != null) {
21+
ans = ans * 2 + head.val;
22+
head = head.next;
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-07-14 23:37:54
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-07-14 23:43:59
6+
'''
7+
from typing import Optional
8+
9+
# # Definition for singly-linked list.
10+
# class ListNode:
11+
# def __init__(self, val=0, next=None):
12+
# self.val = val
13+
# self.next = next
14+
15+
class Solution:
16+
def getDecimalValue(self, head: Optional[ListNode]) -> int:
17+
ans = 0
18+
while head:
19+
ans = ans * 2 + head.val
20+
head = head.next
21+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@
508508
|1282.用户分组|中等|<a href="https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/12/LeetCode%201282.%E7%94%A8%E6%88%B7%E5%88%86%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126298148" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/group-the-people-given-the-group-size-they-belong-to/solution/letmefly-1282yong-hu-fen-zu-by-tisfy-9h94/" target="_blank">LeetCode题解</a>|
509509
|1287.有序数组中出现次数超过25%的元素|简单|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/17/LeetCode%201287.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E4%B8%AD%E5%87%BA%E7%8E%B0%E6%AC%A1%E6%95%B0%E8%B6%85%E8%BF%8725%E7%9A%84%E5%85%83%E7%B4%A0" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145683090" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/element-appearing-more-than-25-in-sorted-array/solutions/3078136/letmefly-1287you-xu-shu-zu-zhong-chu-xia-wdat/" target="_blank">LeetCode题解</a>|
510510
|1289.下降路径最小和II|困难|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/08/10/LeetCode%201289.%E4%B8%8B%E9%99%8D%E8%B7%AF%E5%BE%84%E6%9C%80%E5%B0%8F%E5%92%8CII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132201281" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-falling-path-sum-ii/solutions/2381173/letmefly-1289xia-jiang-lu-jing-zui-xiao-kt8cj/" target="_blank">LeetCode题解</a>|
511+
|1290.二进制链表转整数|简单|<a href="https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/14/LeetCode%201290.%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%93%BE%E8%A1%A8%E8%BD%AC%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149342077" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/solutions/3723954/letmefly-1290er-jin-zhi-lian-biao-zhuan-adolg/" target="_blank">LeetCode题解</a>|
511512
|1295.统计位数为偶数的数字|简单|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/30/LeetCode%201295.%E7%BB%9F%E8%AE%A1%E4%BD%8D%E6%95%B0%E4%B8%BA%E5%81%B6%E6%95%B0%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147637587" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-numbers-with-even-number-of-digits/solutions/3666390/letmefly-1295tong-ji-wei-shu-wei-ou-shu-bwhfr/" target="_blank">LeetCode题解</a>|
512513
|1299.将每个元素替换为右侧最大元素|简单|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/16/LeetCode%201299.%E5%B0%86%E6%AF%8F%E4%B8%AA%E5%85%83%E7%B4%A0%E6%9B%BF%E6%8D%A2%E4%B8%BA%E5%8F%B3%E4%BE%A7%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145661909" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/replace-elements-with-greatest-element-on-right-side/solutions/3076497/letmefly-1299jiang-mei-ge-yuan-su-ti-hua-4y58/" target="_blank">LeetCode题解</a>|
513514
|1302.层数最深叶子节点的和|中等|<a href="https://leetcode.cn/problems/deepest-leaves-sum/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/17/LeetCode%201302.%E5%B1%82%E6%95%B0%E6%9C%80%E6%B7%B1%E5%8F%B6%E5%AD%90%E8%8A%82%E7%82%B9%E7%9A%84%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126377912" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/deepest-leaves-sum/solution/letmefly-1302ceng-shu-zui-shen-xie-zi-ji-eyu0/" target="_blank">LeetCode题解</a>|
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
---
2+
title: 1290.二进制链表转整数:链表+进制转换
3+
date: 2025-07-14 23:48:16
4+
tags: [题解, LeetCode, 简单, 链表, 数学, 进制, 二进制, 进制转换]
5+
categories: [题解, LeetCode]
6+
index_img: http://assets.leetcode.cn/aliyun-lc-upload/uploads/2019/12/15/graph-1.png
7+
---
8+
9+
# 【LetMeFly】1290.二进制链表转整数:链表+进制转换
10+
11+
力扣题目链接:[https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/](https://leetcode.cn/problems/convert-binary-number-in-a-linked-list-to-integer/)
12+
13+
<p>给你一个单链表的引用结点&nbsp;<code>head</code>。链表中每个结点的值不是 0 就是 1。已知此链表是一个整数数字的二进制表示形式。</p>
14+
15+
<p>请你返回该链表所表示数字的 <strong>十进制值</strong> 。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/15/graph-1.png" style="height: 108px; width: 426px;"></p>
22+
23+
<pre><strong>输入:</strong>head = [1,0,1]
24+
<strong>输出:</strong>5
25+
<strong>解释:</strong>二进制数 (101) 转化为十进制数 (5)
26+
</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre><strong>输入:</strong>head = [0]
31+
<strong>输出:</strong>0
32+
</pre>
33+
34+
<p><strong>示例 3:</strong></p>
35+
36+
<pre><strong>输入:</strong>head = [1]
37+
<strong>输出:</strong>1
38+
</pre>
39+
40+
<p><strong>示例 4:</strong></p>
41+
42+
<pre><strong>输入:</strong>head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
43+
<strong>输出:</strong>18880
44+
</pre>
45+
46+
<p><strong>示例 5:</strong></p>
47+
48+
<pre><strong>输入:</strong>head = [0,0]
49+
<strong>输出:</strong>0
50+
</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li>链表不为空。</li>
58+
<li>链表的结点总数不超过&nbsp;<code>30</code>。</li>
59+
<li>每个结点的值不是&nbsp;<code>0</code> 就是 <code>1</code>。</li>
60+
</ul>
61+
62+
63+
64+
## 解题方法:进制转换
65+
66+
二进制如何转为十进制?
67+
68+
初始值$ans = 0$,遍历二进制数字,并令$ans\times 2+当前二进制位$
69+
70+
+ 时间复杂度$O(len(list))$
71+
+ 空间复杂度$O(1)$
72+
73+
### AC代码
74+
75+
#### C++
76+
77+
```cpp
78+
/*
79+
* @Author: LetMeFly
80+
* @Date: 2025-07-14 23:37:54
81+
* @LastEditors: LetMeFly.xyz
82+
* @LastEditTime: 2025-07-14 23:42:31
83+
*/
84+
#if defined(_WIN32) || defined(__APPLE__)
85+
#include "_[1,2]toVector.h"
86+
#endif
87+
88+
/**
89+
* Definition for singly-linked list.
90+
* struct ListNode {
91+
* int val;
92+
* ListNode *next;
93+
* ListNode() : val(0), next(nullptr) {}
94+
* ListNode(int x) : val(x), next(nullptr) {}
95+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
96+
* };
97+
*/
98+
class Solution {
99+
public:
100+
int getDecimalValue(ListNode* head) {
101+
int ans = 0;
102+
while (head) {
103+
ans = ans * 2 + head->val;
104+
head = head->next;
105+
}
106+
return ans;
107+
}
108+
};
109+
```
110+
111+
#### Python
112+
113+
```python
114+
'''
115+
Author: LetMeFly
116+
Date: 2025-07-14 23:37:54
117+
LastEditors: LetMeFly.xyz
118+
LastEditTime: 2025-07-14 23:43:59
119+
'''
120+
from typing import Optional
121+
122+
# # Definition for singly-linked list.
123+
# class ListNode:
124+
# def __init__(self, val=0, next=None):
125+
# self.val = val
126+
# self.next = next
127+
128+
class Solution:
129+
def getDecimalValue(self, head: Optional[ListNode]) -> int:
130+
ans = 0
131+
while head:
132+
ans = ans * 2 + head.val
133+
head = head.next
134+
return ans
135+
```
136+
137+
#### Java
138+
139+
```java
140+
/*
141+
* @Author: LetMeFly
142+
* @Date: 2025-07-14 23:37:54
143+
* @LastEditors: LetMeFly.xyz
144+
* @LastEditTime: 2025-07-14 23:45:19
145+
*/
146+
/**
147+
* Definition for singly-linked list.
148+
* public class ListNode {
149+
* int val;
150+
* ListNode next;
151+
* ListNode() {}
152+
* ListNode(int val) { this.val = val; }
153+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
154+
* }
155+
*/
156+
class Solution {
157+
public int getDecimalValue(ListNode head) {
158+
int ans = 0;
159+
while (head != null) {
160+
ans = ans * 2 + head.val;
161+
head = head.next;
162+
}
163+
return ans;
164+
}
165+
}
166+
```
167+
168+
#### Go
169+
170+
```go
171+
/*
172+
* @Author: LetMeFly
173+
* @Date: 2025-07-14 23:37:54
174+
* @LastEditors: LetMeFly.xyz
175+
* @LastEditTime: 2025-07-14 23:47:42
176+
*/
177+
package main
178+
179+
/**
180+
* Definition for singly-linked list.
181+
* type ListNode struct {
182+
* Val int
183+
* Next *ListNode
184+
* }
185+
*/
186+
func getDecimalValue(head *ListNode) (ans int) {
187+
for ; head != nil; head = head.Next {
188+
ans = ans * 2 + head.Val
189+
}
190+
return
191+
}
192+
```
193+
194+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/149342077)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/07/14/LeetCode%201290.%E4%BA%8C%E8%BF%9B%E5%88%B6%E9%93%BE%E8%A1%A8%E8%BD%AC%E6%95%B4%E6%95%B0/)哦~
195+
>
196+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/LeetCode 3439.重新安排会议得到最多空余时间I.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: 3439.重新安排会议得到最多空余时间 I:贪心(滑动窗口)
33
date: 2025-07-13 22:23:38
44
tags: [题解, LeetCode, 中等, 贪心, 数组, 滑动窗口]
55
categories: [题解, LeetCode]
6+
index_img: https://assets.leetcode.com/uploads/2024/12/21/example0_rescheduled.png
67
---
78

89
# 【LetMeFly】3439.重新安排会议得到最多空余时间 I:贪心(滑动窗口) - 连开k场会议
@@ -82,11 +83,11 @@ categories: [题解, LeetCode]
8283

8384
### 解题思路
8485

85-
怎么尽可能出现“最大间隙”?当然是尽可能地把会放到一块开
86+
怎么尽可能出现“最大间隙”?当然是尽可能地把会议放到一块开
8687

8788
> 题外话:长痛不如短痛,一下连着把课上了然后连着放个长假挺好的。
8889
89-
但是,我们最多调整k个会议的时间。怎么调?选连续的k个挤到一块呗(把当前选中的4个放到<span title="或0">上次会议的end</span>之后紧挨着)。
90+
但是,我们最多调整k个会议的时间。怎么调?选连续的k个挤到一块呗(把当前选中的k个放到<span title="或0">上次会议的end</span>之后紧挨着)。
9091

9192
每次把连续的$k$个会议往前调后,k场会议中最后一个的结束时间和<span title="或eventTime">下一场会议的start</span>之间的间隔即为调整这k个会议的情况下所能营造的最长连续间隔。
9293

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ categories: [自用]
464464
|amiable|adj. 和蔼可亲的,亲切友好的|
465465
|plump|adj. 丰满的,丰腴的,胖乎乎的,饱满的<br/>v. (拍打坐垫等)使蓬松,猛地摔下,重重的坐下<br/>n. 突然前冲,种种坠落|
466466
|miscarriage|n. 流产|
467-
|<font color="#28bea0" title="二次复习">sullen</font>|adj. 闷闷不乐的,郁郁寡欢的,(天空/天气)阴沉的|
467+
|<font color="#28bea0" title="三次复习">sullen</font>|adj. 闷闷不乐的,郁郁寡欢的,(天空/天气)阴沉的|
468468
|||
469469
|carpenter|n. 木匠,木工<br/>v. 以木工手艺造/修(家具/器物)|
470470
|||

en.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!--
2+
* @Author: LetMeFly
3+
* @Date: 2025-07-14 23:30:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-07-14 23:58:06
6+
-->
7+
|bulletin|n. 公告,告示;新闻快报;简报|
8+
|stevedore|n. 码头工人<br/>装船卸货|
9+
|tar|n. 焦油,沥青<br/>用沥青抹,用柏油涂<br/>adj. 涂油柏油的|

0 commit comments

Comments
 (0)