Skip to content

Commit cd46406

Browse files
authored
feat: add solutions to lc problems: No.1017~1027 (doocs#2674)
1 parent acaedfe commit cd46406

File tree

14 files changed

+283
-201
lines changed

14 files changed

+283
-201
lines changed

solution/1000-1099/1017.Convert to Base -2/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,56 @@ function baseNeg2(n: number): string {
171171
}
172172
```
173173

174+
```rust
175+
impl Solution {
176+
pub fn base_neg2(n: i32) -> String {
177+
if n == 0 {
178+
return "0".to_string();
179+
}
180+
let mut k = 1;
181+
let mut ans = String::new();
182+
let mut num = n;
183+
while num != 0 {
184+
if num % 2 != 0 {
185+
ans.push('1');
186+
num -= k;
187+
} else {
188+
ans.push('0');
189+
}
190+
k *= -1;
191+
num /= 2;
192+
}
193+
ans.chars().rev().collect::<String>()
194+
}
195+
}
196+
```
197+
198+
```cs
199+
public class Solution {
200+
public string BaseNeg2(int n) {
201+
if (n == 0) {
202+
return "0";
203+
}
204+
int k = 1;
205+
StringBuilder ans = new StringBuilder();
206+
int num = n;
207+
while (num != 0) {
208+
if (num % 2 != 0) {
209+
ans.Append('1');
210+
num -= k;
211+
} else {
212+
ans.Append('0');
213+
}
214+
k *= -1;
215+
num /= 2;
216+
}
217+
char[] cs = ans.ToString().ToCharArray();
218+
Array.Reverse(cs);
219+
return new string(cs);
220+
}
221+
}
222+
```
223+
174224
<!-- tabs:end -->
175225

176226
<!-- end -->

solution/1000-1099/1017.Convert to Base -2/README_EN.md

+50
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,56 @@ function baseNeg2(n: number): string {
157157
}
158158
```
159159

160+
```rust
161+
impl Solution {
162+
pub fn base_neg2(n: i32) -> String {
163+
if n == 0 {
164+
return "0".to_string();
165+
}
166+
let mut k = 1;
167+
let mut ans = String::new();
168+
let mut num = n;
169+
while num != 0 {
170+
if num % 2 != 0 {
171+
ans.push('1');
172+
num -= k;
173+
} else {
174+
ans.push('0');
175+
}
176+
k *= -1;
177+
num /= 2;
178+
}
179+
ans.chars().rev().collect::<String>()
180+
}
181+
}
182+
```
183+
184+
```cs
185+
public class Solution {
186+
public string BaseNeg2(int n) {
187+
if (n == 0) {
188+
return "0";
189+
}
190+
int k = 1;
191+
StringBuilder ans = new StringBuilder();
192+
int num = n;
193+
while (num != 0) {
194+
if (num % 2 != 0) {
195+
ans.Append('1');
196+
num -= k;
197+
} else {
198+
ans.Append('0');
199+
}
200+
k *= -1;
201+
num /= 2;
202+
}
203+
char[] cs = ans.ToString().ToCharArray();
204+
Array.Reverse(cs);
205+
return new string(cs);
206+
}
207+
}
208+
```
209+
160210
<!-- tabs:end -->
161211

162212
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public string BaseNeg2(int n) {
3+
if (n == 0) {
4+
return "0";
5+
}
6+
int k = 1;
7+
StringBuilder ans = new StringBuilder();
8+
int num = n;
9+
while (num != 0) {
10+
if (num % 2 != 0) {
11+
ans.Append('1');
12+
num -= k;
13+
} else {
14+
ans.Append('0');
15+
}
16+
k *= -1;
17+
num /= 2;
18+
}
19+
char[] cs = ans.ToString().ToCharArray();
20+
Array.Reverse(cs);
21+
return new string(cs);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn base_neg2(n: i32) -> String {
3+
if n == 0 {
4+
return "0".to_string();
5+
}
6+
let mut k = 1;
7+
let mut ans = String::new();
8+
let mut num = n;
9+
while num != 0 {
10+
if num % 2 != 0 {
11+
ans.push('1');
12+
num -= k;
13+
} else {
14+
ans.push('0');
15+
}
16+
k *= -1;
17+
num /= 2;
18+
}
19+
ans.chars().rev().collect::<String>()
20+
}
21+
}

solution/1000-1099/1019.Next Greater Node In Linked List/README.md

+21-68
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ function nextLargerNodes(head: ListNode | null): number[] {
203203
}
204204
const stk: number[] = [];
205205
const n = nums.length;
206-
const ans: number[] = new Array(n).fill(0);
206+
const ans: number[] = Array(n).fill(0);
207207
for (let i = n - 1; ~i; --i) {
208-
while (stk.length && stk[stk.length - 1] <= nums[i]) {
208+
while (stk.length && stk.at(-1)! <= nums[i]) {
209209
stk.pop();
210210
}
211-
ans[i] = stk.length ? stk[stk.length - 1] : 0;
211+
ans[i] = stk.length ? stk.at(-1)! : 0;
212212
stk.push(nums[i]);
213213
}
214214
return ans;
@@ -232,32 +232,29 @@ function nextLargerNodes(head: ListNode | null): number[] {
232232
// }
233233
// }
234234
// }
235-
struct Item {
236-
index: usize,
237-
val: i32,
238-
}
239-
235+
use std::collections::VecDeque;
240236
impl Solution {
241237
pub fn next_larger_nodes(head: Option<Box<ListNode>>) -> Vec<i32> {
242-
let mut res = Vec::new();
243-
let mut stack: Vec<Item> = Vec::new();
244-
let mut cur = &head;
245-
for i in 0..usize::MAX {
246-
if cur.is_none() {
247-
break;
238+
let mut nums = Vec::new();
239+
let mut current = &head;
240+
while let Some(node) = current {
241+
nums.push(node.val);
242+
current = &node.next;
243+
}
244+
245+
let mut stk = VecDeque::new();
246+
let n = nums.len();
247+
let mut ans = vec![0; n];
248+
for i in (0..n).rev() {
249+
while !stk.is_empty() && stk.back().copied().unwrap() <= nums[i] {
250+
stk.pop_back();
248251
}
249-
res.push(0);
250-
let node = cur.as_ref().unwrap();
251-
while !stack.is_empty() && stack.last().unwrap().val < node.val {
252-
res[stack.pop().unwrap().index] = node.val;
252+
if let Some(&top) = stk.back() {
253+
ans[i] = top;
253254
}
254-
stack.push(Item {
255-
index: i,
256-
val: node.val,
257-
});
258-
cur = &node.next;
255+
stk.push_back(nums[i]);
259256
}
260-
res
257+
ans
261258
}
262259
}
263260
```
@@ -296,48 +293,4 @@ var nextLargerNodes = function (head) {
296293

297294
<!-- tabs:end -->
298295

299-
### 方法二
300-
301-
<!-- tabs:start -->
302-
303-
```ts
304-
/**
305-
* Definition for singly-linked list.
306-
* class ListNode {
307-
* val: number
308-
* next: ListNode | null
309-
* constructor(val?: number, next?: ListNode | null) {
310-
* this.val = (val===undefined ? 0 : val)
311-
* this.next = (next===undefined ? null : next)
312-
* }
313-
* }
314-
*/
315-
316-
interface Item {
317-
index: number;
318-
val: number;
319-
}
320-
321-
function nextLargerNodes(head: ListNode | null): number[] {
322-
const res: number[] = [];
323-
const stack: Item[] = [];
324-
let cur = head;
325-
for (let i = 0; cur != null; i++) {
326-
res.push(0);
327-
const { val, next } = cur;
328-
while (stack.length !== 0 && stack[stack.length - 1].val < val) {
329-
res[stack.pop().index] = val;
330-
}
331-
stack.push({
332-
val,
333-
index: i,
334-
});
335-
cur = next;
336-
}
337-
return res;
338-
}
339-
```
340-
341-
<!-- tabs:end -->
342-
343296
<!-- end -->

0 commit comments

Comments
 (0)