Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2579c15

Browse files
committed
feat: add new leetcode problems
1 parent 21adb38 commit 2579c15

File tree

18 files changed

+4541
-2886
lines changed

18 files changed

+4541
-2886
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# [1822. 数组元素积的符号](https://leetcode-cn.com/problems/sign-of-the-product-of-an-array)
2+
3+
[English Version](/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>已知函数 <code>signFunc(x)</code> 将会根据 <code>x</code> 的正负返回特定值:</p>
10+
11+
<ul>
12+
<li>如果 <code>x</code> 是正数,返回 <code>1</code> 。</li>
13+
<li>如果 <code>x</code> 是负数,返回 <code>-1</code> 。</li>
14+
<li>如果 <code>x</code> 是等于 <code>0</code> ,返回 <code>0</code> 。</li>
15+
</ul>
16+
17+
<p>给你一个整数数组 <code>nums</code> 。令 <code>product</code> 为数组 <code>nums</code> 中所有元素值的乘积。</p>
18+
19+
<p>返回 <code>signFunc(product)</code> 。</p>
20+
21+
<p> </p>
22+
23+
<p><strong>示例 1:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>nums = [-1,-2,-3,-4,3,2,1]
27+
<strong>输出:</strong>1
28+
<strong>解释:</strong>数组中所有值的乘积是 144 ,且 signFunc(144) = 1
29+
</pre>
30+
31+
<p><strong>示例 2:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>nums = [1,5,0,2,-3]
35+
<strong>输出:</strong>0
36+
<strong>解释:</strong>数组中所有值的乘积是 0 ,且 signFunc(0) = 0
37+
</pre>
38+
39+
<p><strong>示例 3:</strong></p>
40+
41+
<pre>
42+
<strong>输入:</strong>nums = [-1,1,-1,1,-1]
43+
<strong>输出:</strong>-1
44+
<strong>解释:</strong>数组中所有值的乘积是 -1 ,且 signFunc(-1) = -1
45+
</pre>
46+
47+
<p> </p>
48+
49+
<p><strong>提示:</strong></p>
50+
51+
<ul>
52+
<li><code>1 <= nums.length <= 1000</code></li>
53+
<li><code>-100 <= nums[i] <= 100</code></li>
54+
</ul>
55+
56+
## 解法
57+
58+
<!-- 这里可写通用的实现逻辑 -->
59+
60+
<!-- tabs:start -->
61+
62+
### **Python3**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```python
67+
68+
```
69+
70+
### **Java**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```java
75+
76+
```
77+
78+
### **...**
79+
80+
```
81+
82+
```
83+
84+
<!-- tabs:end -->
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# [1822. Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array)
2+
3+
[中文文档](/solution/1800-1899/1822.Sign%20of%20the%20Product%20of%20an%20Array/README.md)
4+
5+
## Description
6+
7+
<p>There is a function <code>signFunc(x)</code> that returns:</p>
8+
9+
<ul>
10+
<li><code>1</code> if <code>x</code> is positive.</li>
11+
<li><code>-1</code> if <code>x</code> is negative.</li>
12+
<li><code>0</code> if <code>x</code> is equal to <code>0</code>.</li>
13+
</ul>
14+
15+
<p>You are given an integer array <code>nums</code>. Let <code>product</code> be the product of all values in the array <code>nums</code>.</p>
16+
17+
<p>Return <code>signFunc(product)</code>.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong>Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [-1,-2,-3,-4,3,2,1]
24+
<strong>Output:</strong> 1
25+
<strong>Explanation:</strong> The product of all values in the array is 144, and signFunc(144) = 1
26+
</pre>
27+
28+
<p><strong>Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [1,5,0,2,-3]
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> The product of all values in the array is 0, and signFunc(0) = 0
34+
</pre>
35+
36+
<p><strong>Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> nums = [-1,1,-1,1,-1]
40+
<strong>Output:</strong> -1
41+
<strong>Explanation:</strong> The product of all values in the array is -1, and signFunc(-1) = -1
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
49+
<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>
50+
</ul>
51+
52+
## Solutions
53+
54+
<!-- tabs:start -->
55+
56+
### **Python3**
57+
58+
```python
59+
60+
```
61+
62+
### **Java**
63+
64+
```java
65+
66+
```
67+
68+
### **...**
69+
70+
```
71+
72+
```
73+
74+
<!-- tabs:end -->
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [1823. 找出游戏的获胜者](https://leetcode-cn.com/problems/find-the-winner-of-the-circular-game)
2+
3+
[English Version](/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>共有 <code>n</code> 名小伙伴一起做游戏。小伙伴们围成一圈,按 <strong>顺时针顺序</strong> 从 <code>1</code> 到 <code>n</code> 编号。确切地说,从第 <code>i</code> 名小伙伴顺时针移动一位会到达第 <code>(i+1)</code> 名小伙伴的位置,其中 <code>1 <= i < n</code> ,从第 <code>n</code> 名小伙伴顺时针移动一位会回到第 <code>1</code> 名小伙伴的位置。</p>
10+
11+
<p>游戏遵循如下规则:</p>
12+
13+
<ol>
14+
<li>从第 <code>1</code> 名小伙伴所在位置 <strong>开始</strong> 。</li>
15+
<li>沿着顺时针方向数 <code>k</code> 名小伙伴,计数时需要 <strong>包含</strong> 起始时的那位小伙伴。逐个绕圈进行计数,一些小伙伴可能会被数过不止一次。</li>
16+
<li>你数到的最后一名小伙伴需要离开圈子,并视作输掉游戏。</li>
17+
<li>如果圈子中仍然有不止一名小伙伴,从刚刚输掉的小伙伴的 <strong>顺时针下一位</strong> 小伙伴 <strong>开始</strong>,回到步骤 <code>2</code> 继续执行。</li>
18+
<li>否则,圈子中最后一名小伙伴赢得游戏。</li>
19+
</ol>
20+
21+
<p>给你参与游戏的小伙伴总数 <code>n</code> ,和一个整数 <code>k</code> ,返回游戏的获胜者。</p>
22+
23+
<p> </p>
24+
25+
<p><strong>示例 1:</strong></p>
26+
27+
![](./images/ic234-q2-ex11.png)
28+
29+
<pre>
30+
<strong>输入:</strong>n = 5, k = 2
31+
<strong>输出:</strong>3
32+
<strong>解释:</strong>游戏运行步骤如下:
33+
1) 从小伙伴 1 开始。
34+
2) 顺时针数 2 名小伙伴,也就是小伙伴 1 和 2 。
35+
3) 小伙伴 2 离开圈子。下一次从小伙伴 3 开始。
36+
4) 顺时针数 2 名小伙伴,也就是小伙伴 3 和 4 。
37+
5) 小伙伴 4 离开圈子。下一次从小伙伴 5 开始。
38+
6) 顺时针数 2 名小伙伴,也就是小伙伴 5 和 1 。
39+
7) 小伙伴 1 离开圈子。下一次从小伙伴 3 开始。
40+
8) 顺时针数 2 名小伙伴,也就是小伙伴 3 和 5 。
41+
9) 小伙伴 5 离开圈子。只剩下小伙伴 3 。所以小伙伴 3 是游戏的获胜者。</pre>
42+
43+
<p><strong>示例 2:</strong></p>
44+
45+
<pre>
46+
<strong>输入:</strong>n = 6, k = 5
47+
<strong>输出:</strong>1
48+
<strong>解释:</strong>小伙伴离开圈子的顺序:5、4、6、2、3 。小伙伴 1 是游戏的获胜者。
49+
</pre>
50+
51+
<p> </p>
52+
53+
<p><strong>提示:</strong></p>
54+
55+
<ul>
56+
<li><code>1 <= k <= n <= 500</code></li>
57+
</ul>
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **...**
82+
83+
```
84+
85+
```
86+
87+
<!-- tabs:end -->
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# [1823. Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game)
2+
3+
[中文文档](/solution/1800-1899/1823.Find%20the%20Winner%20of%20the%20Circular%20Game/README.md)
4+
5+
## Description
6+
7+
<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 &lt;= i &lt; n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
8+
9+
<p>The rules of the game are as follows:</p>
10+
11+
<ol>
12+
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
13+
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
14+
<li>The last friend you counted leaves the circle and loses the game.</li>
15+
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
16+
<li>Else, the last friend in the circle wins the game.</li>
17+
</ol>
18+
19+
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
20+
21+
<p>&nbsp;</p>
22+
23+
<p><strong>Example 1:</strong></p>
24+
25+
![](./images/ic234-q2-ex11.png)
26+
27+
<pre>
28+
29+
<strong>Input:</strong> n = 5, k = 2
30+
31+
<strong>Output:</strong> 3
32+
33+
<strong>Explanation:</strong> Here are the steps of the game:
34+
35+
1) Start at friend 1.
36+
37+
2) Count 2 friends clockwise, which are friends 1 and 2.
38+
39+
3) Friend 2 leaves the circle. Next start is friend 3.
40+
41+
4) Count 2 friends clockwise, which are friends 3 and 4.
42+
43+
5) Friend 4 leaves the circle. Next start is friend 5.
44+
45+
6) Count 2 friends clockwise, which are friends 5 and 1.
46+
47+
7) Friend 1 leaves the circle. Next start is friend 3.
48+
49+
8) Count 2 friends clockwise, which are friends 3 and 5.
50+
51+
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
52+
53+
<p><strong>Example 2:</strong></p>
54+
55+
<pre>
56+
57+
<strong>Input:</strong> n = 6, k = 5
58+
59+
<strong>Output:</strong> 1
60+
61+
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
62+
63+
</pre>
64+
65+
<p>&nbsp;</p>
66+
67+
<p><strong>Constraints:</strong></p>
68+
69+
<ul>
70+
<li><code>1 &lt;= k &lt;= n &lt;= 500</code></li>
71+
</ul>
72+
73+
## Solutions
74+
75+
<!-- tabs:start -->
76+
77+
### **Python3**
78+
79+
```python
80+
81+
```
82+
83+
### **Java**
84+
85+
```java
86+
87+
```
88+
89+
### **...**
90+
91+
```
92+
93+
```
94+
95+
<!-- tabs:end -->
30.9 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /