Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.1017~1027 #2674

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions solution/1000-1099/1017.Convert to Base -2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,56 @@ function baseNeg2(n: number): string {
}
```

```rust
impl Solution {
pub fn base_neg2(n: i32) -> String {
if n == 0 {
return "0".to_string();
}
let mut k = 1;
let mut ans = String::new();
let mut num = n;
while num != 0 {
if num % 2 != 0 {
ans.push('1');
num -= k;
} else {
ans.push('0');
}
k *= -1;
num /= 2;
}
ans.chars().rev().collect::<String>()
}
}
```

```cs
public class Solution {
public string BaseNeg2(int n) {
if (n == 0) {
return "0";
}
int k = 1;
StringBuilder ans = new StringBuilder();
int num = n;
while (num != 0) {
if (num % 2 != 0) {
ans.Append('1');
num -= k;
} else {
ans.Append('0');
}
k *= -1;
num /= 2;
}
char[] cs = ans.ToString().ToCharArray();
Array.Reverse(cs);
return new string(cs);
}
}
```

<!-- tabs:end -->

<!-- end -->
50 changes: 50 additions & 0 deletions solution/1000-1099/1017.Convert to Base -2/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,56 @@ function baseNeg2(n: number): string {
}
```

```rust
impl Solution {
pub fn base_neg2(n: i32) -> String {
if n == 0 {
return "0".to_string();
}
let mut k = 1;
let mut ans = String::new();
let mut num = n;
while num != 0 {
if num % 2 != 0 {
ans.push('1');
num -= k;
} else {
ans.push('0');
}
k *= -1;
num /= 2;
}
ans.chars().rev().collect::<String>()
}
}
```

```cs
public class Solution {
public string BaseNeg2(int n) {
if (n == 0) {
return "0";
}
int k = 1;
StringBuilder ans = new StringBuilder();
int num = n;
while (num != 0) {
if (num % 2 != 0) {
ans.Append('1');
num -= k;
} else {
ans.Append('0');
}
k *= -1;
num /= 2;
}
char[] cs = ans.ToString().ToCharArray();
Array.Reverse(cs);
return new string(cs);
}
}
```

<!-- tabs:end -->

<!-- end -->
23 changes: 23 additions & 0 deletions solution/1000-1099/1017.Convert to Base -2/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Solution {
public string BaseNeg2(int n) {
if (n == 0) {
return "0";
}
int k = 1;
StringBuilder ans = new StringBuilder();
int num = n;
while (num != 0) {
if (num % 2 != 0) {
ans.Append('1');
num -= k;
} else {
ans.Append('0');
}
k *= -1;
num /= 2;
}
char[] cs = ans.ToString().ToCharArray();
Array.Reverse(cs);
return new string(cs);
}
}
21 changes: 21 additions & 0 deletions solution/1000-1099/1017.Convert to Base -2/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
impl Solution {
pub fn base_neg2(n: i32) -> String {
if n == 0 {
return "0".to_string();
}
let mut k = 1;
let mut ans = String::new();
let mut num = n;
while num != 0 {
if num % 2 != 0 {
ans.push('1');
num -= k;
} else {
ans.push('0');
}
k *= -1;
num /= 2;
}
ans.chars().rev().collect::<String>()
}
}
89 changes: 21 additions & 68 deletions solution/1000-1099/1019.Next Greater Node In Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ function nextLargerNodes(head: ListNode | null): number[] {
}
const stk: number[] = [];
const n = nums.length;
const ans: number[] = new Array(n).fill(0);
const ans: number[] = Array(n).fill(0);
for (let i = n - 1; ~i; --i) {
while (stk.length && stk[stk.length - 1] <= nums[i]) {
while (stk.length && stk.at(-1)! <= nums[i]) {
stk.pop();
}
ans[i] = stk.length ? stk[stk.length - 1] : 0;
ans[i] = stk.length ? stk.at(-1)! : 0;
stk.push(nums[i]);
}
return ans;
Expand All @@ -232,32 +232,29 @@ function nextLargerNodes(head: ListNode | null): number[] {
// }
// }
// }
struct Item {
index: usize,
val: i32,
}

use std::collections::VecDeque;
impl Solution {
pub fn next_larger_nodes(head: Option<Box<ListNode>>) -> Vec<i32> {
let mut res = Vec::new();
let mut stack: Vec<Item> = Vec::new();
let mut cur = &head;
for i in 0..usize::MAX {
if cur.is_none() {
break;
let mut nums = Vec::new();
let mut current = &head;
while let Some(node) = current {
nums.push(node.val);
current = &node.next;
}

let mut stk = VecDeque::new();
let n = nums.len();
let mut ans = vec![0; n];
for i in (0..n).rev() {
while !stk.is_empty() && stk.back().copied().unwrap() <= nums[i] {
stk.pop_back();
}
res.push(0);
let node = cur.as_ref().unwrap();
while !stack.is_empty() && stack.last().unwrap().val < node.val {
res[stack.pop().unwrap().index] = node.val;
if let Some(&top) = stk.back() {
ans[i] = top;
}
stack.push(Item {
index: i,
val: node.val,
});
cur = &node.next;
stk.push_back(nums[i]);
}
res
ans
}
}
```
Expand Down Expand Up @@ -296,48 +293,4 @@ var nextLargerNodes = function (head) {

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/

interface Item {
index: number;
val: number;
}

function nextLargerNodes(head: ListNode | null): number[] {
const res: number[] = [];
const stack: Item[] = [];
let cur = head;
for (let i = 0; cur != null; i++) {
res.push(0);
const { val, next } = cur;
while (stack.length !== 0 && stack[stack.length - 1].val < val) {
res[stack.pop().index] = val;
}
stack.push({
val,
index: i,
});
cur = next;
}
return res;
}
```

<!-- tabs:end -->

<!-- end -->
Loading
Loading