Skip to content

aaa #2078

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

Closed
wants to merge 7 commits into from
Closed

aaa #2078

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 @@ -15,24 +15,24 @@
<pre>
<strong>输入: </strong>s = "abcabcbb"
<strong>输出: </strong>3
<strong>解释:</strong> 因为无重复字符的最长子串是 <code>"abc",所以其</code>长度为 3。
<strong>解释:</strong> 因为无重复字符的最长子串是 "abc",所以其长度为 3。
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入: </strong>s = "bbbbb"
<strong>输出: </strong>1
<strong>解释: </strong>因为无重复字符的最长子串是 <code>"b"</code>,所以其长度为 1。
<strong>解释: </strong>因为无重复字符的最长子串是 "b",所以其长度为 1。
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入: </strong>s = "pwwkew"
<strong>输出: </strong>3
<strong>解释: </strong>因为无重复字符的最长子串是&nbsp;<code>"wke"</code>,所以其长度为 3。
&nbsp; 请注意,你的答案必须是 <strong>子串 </strong>的长度,<code>"pwke"</code>&nbsp;是一个<em>子序列,</em>不是子串。
<strong>解释: </strong>因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 <strong>子串 </strong>的长度,"pwke" 是一个<em>子序列,</em>不是子串。
</pre>

<p>&nbsp;</p>
Expand Down
4 changes: 2 additions & 2 deletions solution/0000-0099/0006.Zigzag Conversion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func convert(s string, numRows int) string {
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
let arr = new Array(numRows);
const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
Expand All @@ -225,7 +225,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
for (let item of arr) {
for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
Expand Down
4 changes: 2 additions & 2 deletions solution/0000-0099/0006.Zigzag Conversion/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func convert(s string, numRows int) string {
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
let arr = new Array(numRows);
const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
Expand All @@ -219,7 +219,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
for (let item of arr) {
for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
Expand Down
4 changes: 2 additions & 2 deletions solution/0000-0099/0006.Zigzag Conversion/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
var convert = function (s, numRows) {
if (numRows == 1) return s;
let arr = new Array(numRows);
const arr = new Array(numRows);
for (let i = 0; i < numRows; i++) arr[i] = [];
let mi = 0,
isDown = true;
Expand All @@ -19,7 +19,7 @@ var convert = function (s, numRows) {
else mi--;
}
let ans = [];
for (let item of arr) {
for (const item of arr) {
ans = ans.concat(item);
}
return ans.join('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [<code>4,5,6,7,0,1,2]</code>, target = 0
<strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 0
<strong>输出:</strong>4
</pre>

<p><strong>示例&nbsp;2:</strong></p>

<pre>
<strong>输入:</strong>nums = [<code>4,5,6,7,0,1,2]</code>, target = 3
<strong>输入:</strong>nums = [4,5,6,7,0,1,2], target = 3
<strong>输出:</strong>-1</pre>

<p><strong>示例 3:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = [<code>5,7,7,8,8,10]</code>, target = 8
<strong>输入:</strong>nums = [5,7,7,8,8,10], target = 8
<strong>输出:</strong>[3,4]</pre>

<p><strong>示例&nbsp;2:</strong></p>

<pre>
<strong>输入:</strong>nums = [<code>5,7,7,8,8,10]</code>, target = 6
<strong>输入:</strong>nums = [5,7,7,8,8,10], target = 6
<strong>输出:</strong>[-1,-1]</pre>

<p><strong>示例 3:</strong></p>
Expand Down
123 changes: 122 additions & 1 deletion solution/1000-1099/1040.Moving Stones Until Consecutive II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,143 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:排序 + 分类讨论 + 双指针**

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `stones` 的长度。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
stones.sort()
mi = n = len(stones)
mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1)
i = 0
for j, x in enumerate(stones):
while x - stones[i] + 1 > n:
i += 1
if j - i + 1 == n - 1 and x - stones[i] == n - 2:
mi = min(mi, 2)
else:
mi = min(mi, n - (j - i + 1))
return [mi, mx]
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int[] numMovesStonesII(int[] stones) {
Arrays.sort(stones);
int n = stones.length;
int mi = n;
int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
for (int i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
mi = Math.min(mi, 2);
} else {
mi = Math.min(mi, n - (j - i + 1));
}
}
return new int[] {mi, mx};
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> numMovesStonesII(vector<int>& stones) {
sort(stones.begin(), stones.end());
int n = stones.size();
int mi = n;
int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
for (int i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
mi = min(mi, 2);
} else {
mi = min(mi, n - (j - i + 1));
}
}
return {mi, mx};
}
};
```

### **Go**

```go
func numMovesStonesII(stones []int) []int {
sort.Ints(stones)
n := len(stones)
mi := n
mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1)
i := 0
for j, x := range stones {
for x-stones[i]+1 > n {
i++
}
if j-i+1 == n-1 && stones[j]-stones[i] == n-2 {
mi = min(mi, 2)
} else {
mi = min(mi, n-(j-i+1))
}
}
return []int{mi, mx}
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function numMovesStonesII(stones: number[]): number[] {
stones.sort((a, b) => a - b);
const n = stones.length;
let mi = n;
const mx =
Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) -
(n - 1);
for (let i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
mi = Math.min(mi, 2);
} else {
mi = Math.min(mi, n - (j - i + 1));
}
}
return [mi, mx];
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,130 @@ Notice we cannot move 10 -&gt; 2 to finish the game, because that would be an il
### **Python3**

```python

class Solution:
def numMovesStonesII(self, stones: List[int]) -> List[int]:
stones.sort()
mi = n = len(stones)
mx = max(stones[-1] - stones[1] + 1, stones[-2] - stones[0] + 1) - (n - 1)
i = 0
for j, x in enumerate(stones):
while x - stones[i] + 1 > n:
i += 1
if j - i + 1 == n - 1 and x - stones[i] == n - 2:
mi = min(mi, 2)
else:
mi = min(mi, n - (j - i + 1))
return [mi, mx]
```

### **Java**

```java
class Solution {
public int[] numMovesStonesII(int[] stones) {
Arrays.sort(stones);
int n = stones.length;
int mi = n;
int mx = Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
for (int i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
mi = Math.min(mi, 2);
} else {
mi = Math.min(mi, n - (j - i + 1));
}
}
return new int[] {mi, mx};
}
}
```

### **C++**

```cpp
class Solution {
public:
vector<int> numMovesStonesII(vector<int>& stones) {
sort(stones.begin(), stones.end());
int n = stones.size();
int mi = n;
int mx = max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) - (n - 1);
for (int i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) {
mi = min(mi, 2);
} else {
mi = min(mi, n - (j - i + 1));
}
}
return {mi, mx};
}
};
```

### **Go**

```go
func numMovesStonesII(stones []int) []int {
sort.Ints(stones)
n := len(stones)
mi := n
mx := max(stones[n-1]-stones[1]+1, stones[n-2]-stones[0]+1) - (n - 1)
i := 0
for j, x := range stones {
for x-stones[i]+1 > n {
i++
}
if j-i+1 == n-1 && stones[j]-stones[i] == n-2 {
mi = min(mi, 2)
} else {
mi = min(mi, n-(j-i+1))
}
}
return []int{mi, mx}
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
```

### **TypeScript**

```ts
function numMovesStonesII(stones: number[]): number[] {
stones.sort((a, b) => a - b);
const n = stones.length;
let mi = n;
const mx =
Math.max(stones[n - 1] - stones[1] + 1, stones[n - 2] - stones[0] + 1) -
(n - 1);
for (let i = 0, j = 0; j < n; ++j) {
while (stones[j] - stones[i] + 1 > n) {
++i;
}
if (j - i + 1 === n - 1 && stones[j] - stones[i] === n - 2) {
mi = Math.min(mi, 2);
} else {
mi = Math.min(mi, n - (j - i + 1));
}
}
return [mi, mx];
}
```

### **...**
Expand Down
Loading