Skip to content

Commit 2fe9a1b

Browse files
committed
feat: add solutions to lc problems: No.2119,2177
1 parent 59ce33e commit 2fe9a1b

File tree

10 files changed

+168
-5
lines changed

10 files changed

+168
-5
lines changed

solution/2100-2199/2119.A Number After a Double Reversal/README.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### 方法一
65+
### 方法一:数学
66+
67+
如果数字是 $0$,或者数字的个位不是 $0$,那么反转两次后的数字一定和原数字相等。
68+
69+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
6670

6771
<!-- tabs:start -->
6872

@@ -103,6 +107,36 @@ func isSameAfterReversals(num int) bool {
103107
}
104108
```
105109

110+
#### TypeScript
111+
112+
```ts
113+
function isSameAfterReversals(num: number): boolean {
114+
return num === 0 || num % 10 !== 0;
115+
}
116+
```
117+
118+
#### Rust
119+
120+
```rust
121+
impl Solution {
122+
pub fn is_same_after_reversals(num: i32) -> bool {
123+
num == 0 || num % 10 != 0
124+
}
125+
}
126+
```
127+
128+
#### JavaScript
129+
130+
```js
131+
/**
132+
* @param {number} num
133+
* @return {boolean}
134+
*/
135+
var isSameAfterReversals = function (num) {
136+
return num === 0 || num % 10 !== 0;
137+
};
138+
```
139+
106140
<!-- tabs:end -->
107141

108142
<!-- solution:end -->

solution/2100-2199/2119.A Number After a Double Reversal/README_EN.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Mathematics
68+
69+
If the number is $0$, or the last digit of the number is not $0$, then the number after reversing twice will be the same as the original number.
70+
71+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
6872

6973
<!-- tabs:start -->
7074

@@ -105,6 +109,36 @@ func isSameAfterReversals(num int) bool {
105109
}
106110
```
107111

112+
#### TypeScript
113+
114+
```ts
115+
function isSameAfterReversals(num: number): boolean {
116+
return num === 0 || num % 10 !== 0;
117+
}
118+
```
119+
120+
#### Rust
121+
122+
```rust
123+
impl Solution {
124+
pub fn is_same_after_reversals(num: i32) -> bool {
125+
num == 0 || num % 10 != 0
126+
}
127+
}
128+
```
129+
130+
#### JavaScript
131+
132+
```js
133+
/**
134+
* @param {number} num
135+
* @return {boolean}
136+
*/
137+
var isSameAfterReversals = function (num) {
138+
return num === 0 || num % 10 !== 0;
139+
};
140+
```
141+
108142
<!-- tabs:end -->
109143

110144
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} num
3+
* @return {boolean}
4+
*/
5+
var isSameAfterReversals = function (num) {
6+
return num === 0 || num % 10 !== 0;
7+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn is_same_after_reversals(num: i32) -> bool {
3+
num == 0 || num % 10 != 0
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function isSameAfterReversals(num: number): boolean {
2+
return num === 0 || num % 10 !== 0;
3+
}

solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tags:
5454

5555
### 方法一:数学
5656

57-
假设三个连续的整数分别为 $x-1$, $x$, $x+1$,则它们的和为 $3x$,因此 $num$ 必须是 $3$ 的倍数。如果 $num$ 不是 $3$ 的倍数,则无法表示成三个连续整数的和,返回空数组。否则,令 $x = \frac{num}{3}$,则 $x-1$, $x$, $x+1$ 就是三个连续整数,它们的和为 $num$。
57+
我们假设三个连续的整数分别为 $x-1$, $x$, $x+1$,则它们的和为 $3x$,因此 $\textit{num}$ 必须是 $3$ 的倍数。如果 $\textit{num}$ 不是 $3$ 的倍数,则无法表示成三个连续整数的和,返回空数组。否则,令 $x = \frac{\textit{num}}{3}$,则 $x-1$, $x$, $x+1$ 就是三个连续整数,它们的和为 $\textit{num}$。
5858

5959
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
6060

@@ -122,6 +122,36 @@ function sumOfThree(num: number): number[] {
122122
}
123123
```
124124

125+
#### Rust
126+
127+
```rust
128+
impl Solution {
129+
pub fn sum_of_three(num: i64) -> Vec<i64> {
130+
if num % 3 != 0 {
131+
return Vec::new();
132+
}
133+
let x = num / 3;
134+
vec![x - 1, x, x + 1]
135+
}
136+
}
137+
```
138+
139+
#### JavaScript
140+
141+
```js
142+
/**
143+
* @param {number} num
144+
* @return {number[]}
145+
*/
146+
var sumOfThree = function (num) {
147+
if (num % 3) {
148+
return [];
149+
}
150+
const x = Math.floor(num / 3);
151+
return [x - 1, x, x + 1];
152+
};
153+
```
154+
125155
<!-- tabs:end -->
126156

127157
<!-- solution:end -->

solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md

+31-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ tags:
5454

5555
### Solution 1: Mathematics
5656

57-
Assume the three consecutive integers are $x-1$, $x$, and $x+1$. Their sum is $3x$, so $num$ must be a multiple of $3$. If $num$ is not a multiple of $3$, it cannot be expressed as the sum of three consecutive integers, and we return an empty array. Otherwise, let $x = \frac{num}{3}$, then $x-1$, $x$, and $x+1$ are the three consecutive integers whose sum is $num$.
57+
Assume the three consecutive integers are $x-1$, $x$, and $x+1$. Their sum is $3x$, so $\textit{num}$ must be a multiple of $3$. If $\textit{num}$ is not a multiple of $3$, it cannot be represented as the sum of three consecutive integers, and we return an empty array. Otherwise, let $x = \frac{\textit{num}}{3}$, then $x-1$, $x$, and $x+1$ are the three consecutive integers whose sum is $\textit{num}$.
5858

5959
The time complexity is $O(1)$, and the space complexity is $O(1)$.
6060

@@ -122,6 +122,36 @@ function sumOfThree(num: number): number[] {
122122
}
123123
```
124124

125+
#### Rust
126+
127+
```rust
128+
impl Solution {
129+
pub fn sum_of_three(num: i64) -> Vec<i64> {
130+
if num % 3 != 0 {
131+
return Vec::new();
132+
}
133+
let x = num / 3;
134+
vec![x - 1, x, x + 1]
135+
}
136+
}
137+
```
138+
139+
#### JavaScript
140+
141+
```js
142+
/**
143+
* @param {number} num
144+
* @return {number[]}
145+
*/
146+
var sumOfThree = function (num) {
147+
if (num % 3) {
148+
return [];
149+
}
150+
const x = Math.floor(num / 3);
151+
return [x - 1, x, x + 1];
152+
};
153+
```
154+
125155
<!-- tabs:end -->
126156

127157
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number} num
3+
* @return {number[]}
4+
*/
5+
var sumOfThree = function (num) {
6+
if (num % 3) {
7+
return [];
8+
}
9+
const x = Math.floor(num / 3);
10+
return [x - 1, x, x + 1];
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn sum_of_three(num: i64) -> Vec<i64> {
3+
if num % 3 != 0 {
4+
return Vec::new();
5+
}
6+
let x = num / 3;
7+
vec![x - 1, x, x + 1]
8+
}
9+
}

solution/2600-2699/2674.Split a Circular Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tags:
4646
<p><strong>提示:</strong></p>
4747

4848
<ul>
49-
<li><code>list</code> 中的节点数范围为 <code>[2, 105]</code></li>
49+
<li><code>list</code> 中的节点数范围为 <code>[2, 10<sup>5</sup>]</code></li>
5050
<li><code>0 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
5151
<li><code>LastNode.next = FirstNode</code> ,其中 <code>LastNode</code> 是链表的最后一个节点,<code>FirstNode</code> 是第一个节点。</li>
5252
</ul>

0 commit comments

Comments
 (0)