Skip to content

[pull] main from doocs:main #155

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

Merged
merged 3 commits into from
Jul 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ func letterCombinations(digits string) []string {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t: string[] = [];
for (const a of ans) {
for (const b of s) {
Expand Down Expand Up @@ -218,13 +218,13 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t = [];
for (const a of ans) {
for (const b of s) {
Expand Down Expand Up @@ -392,7 +392,7 @@ func letterCombinations(digits string) (ans []string) {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
Expand All @@ -403,7 +403,7 @@ function letterCombinations(digits: string): string[] {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down Expand Up @@ -453,7 +453,7 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
Expand All @@ -464,7 +464,7 @@ var letterCombinations = function (digits) {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ func letterCombinations(digits string) []string {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t: string[] = [];
for (const a of ans) {
for (const b of s) {
Expand Down Expand Up @@ -214,13 +214,13 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t = [];
for (const a of ans) {
for (const b of s) {
Expand Down Expand Up @@ -388,7 +388,7 @@ func letterCombinations(digits string) (ans []string) {

```ts
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
Expand All @@ -399,7 +399,7 @@ function letterCombinations(digits: string): string[] {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down Expand Up @@ -449,7 +449,7 @@ impl Solution {
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
Expand All @@ -460,7 +460,7 @@ var letterCombinations = function (digits) {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t = [];
for (const a of ans) {
for (const b of s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [''];
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
for (const i of digits) {
const s = d[parseInt(i) - 2];
const s = d[+i - 2];
const t: string[] = [];
for (const a of ans) {
for (const b of s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @return {string[]}
*/
var letterCombinations = function (digits) {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans = [];
Expand All @@ -14,7 +14,7 @@ var letterCombinations = function (digits) {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function letterCombinations(digits: string): string[] {
if (digits.length == 0) {
if (digits.length === 0) {
return [];
}
const ans: string[] = [];
Expand All @@ -10,7 +10,7 @@ function letterCombinations(digits: string): string[] {
ans.push(t.join(''));
return;
}
const s = d[parseInt(digits[i]) - 2];
const s = d[+digits[i] - 2];
for (const c of s) {
t.push(c);
dfs(i + 1);
Expand Down
6 changes: 3 additions & 3 deletions solution/0700-0799/0724.Find Pivot Index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ tags:

### 方法一:前缀和

我们定义变量 $left$ 表示数组 `nums` 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 `nums` 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。
我们定义变量 $left$ 表示数组 $\textit{nums}$ 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 $\textit{nums}$ 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。

遍历数组 `nums`,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时如果 $left=right$,说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x$,继续遍历下一个数字。
遍历数组 $\textit{nums}$,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时如果 $left=right$,说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x$,继续遍历下一个数字。

遍历结束,如果没有找到中间位置,返回 $-1$。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。

相似题目:

Expand Down
15 changes: 14 additions & 1 deletion solution/0700-0799/0724.Find Pivot Index/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,20 @@ Right sum = nums[1] + nums[2] = 1 + -1 = 0

<!-- solution:start -->

### Solution 1
### Solution 1: Prefix Sum

We define a variable $left$ to represent the sum of elements to the left of index $i$ in the array $\textit{nums}$, and a variable $right$ to represent the sum of elements to the right of index $i$ in the array $\textit{nums}$. Initially, $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$.

We traverse the array $\textit{nums}$. For the current number $x$ being traversed, we update $right = right - x$. At this point, if $left = right$, it indicates that the current index $i$ is the middle position, and we can return it directly. Otherwise, we update $left = left + x$ and continue to traverse the next number.

If the middle position is not found by the end of the traversal, return $-1$.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $\textit{nums}$.

Similar Problems:

- [1991. Find the Middle Index in Array](https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1991.Find%20the%20Middle%20Index%20in%20Array/README_EN.md)
- [2574. Left and Right Sum Differences](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md)

<!-- tabs:start -->

Expand Down
19 changes: 19 additions & 0 deletions solution/3100-3199/3100.Water Bottles II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ function maxBottlesDrunk(numBottles: number, numExchange: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_bottles_drunk(mut num_bottles: i32, mut num_exchange: i32) -> i32 {
let mut ans = num_bottles;

while num_bottles >= num_exchange {
num_bottles -= num_exchange;
num_exchange += 1;
ans += 1;
num_bottles += 1;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
19 changes: 19 additions & 0 deletions solution/3100-3199/3100.Water Bottles II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ function maxBottlesDrunk(numBottles: number, numExchange: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn max_bottles_drunk(mut num_bottles: i32, mut num_exchange: i32) -> i32 {
let mut ans = num_bottles;

while num_bottles >= num_exchange {
num_bottles -= num_exchange;
num_exchange += 1;
ans += 1;
num_bottles += 1;
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
14 changes: 14 additions & 0 deletions solution/3100-3199/3100.Water Bottles II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
impl Solution {
pub fn max_bottles_drunk(mut num_bottles: i32, mut num_exchange: i32) -> i32 {
let mut ans = num_bottles;

while num_bottles >= num_exchange {
num_bottles -= num_exchange;
num_exchange += 1;
ans += 1;
num_bottles += 1;
}

ans
}
}
Loading