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 f0818df

Browse files
committed
1 parent c7e85e5 commit f0818df

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

‎lcof/面试题61. 扑克牌中的顺子/README.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,53 @@
2424
- 数组的数取值为 `[0, 13]`
2525
## 解法
2626
<!-- 这里可写通用的实现逻辑 -->
27+
用数组 t 记录是否存在重复的数,存在则直接返回 false。
2728

29+
遍历数组,忽略大小王(0),求出数组的最大、最小值。若差值超过 4,则无法构成顺子,例如:`5,6,(0),8,10`
2830

2931
### Python3
3032
<!-- 这里可写当前语言的特殊实现逻辑 -->
3133

3234
```python
35+
class Solution:
36+
def isStraight(self, nums: List[int]) -> bool:
37+
t = [False for _ in range(14)]
38+
max_val, min_val = 0, 14
39+
for num in nums:
40+
if num == 0:
41+
continue
42+
if t[num]:
43+
return False
44+
t[num] = True
45+
max_val = max(max_val, num)
46+
min_val = min(min_val, num)
47+
return max_val - min_val <= 4
48+
3349

3450
```
3551

3652
### Java
3753
<!-- 这里可写当前语言的特殊实现逻辑 -->
3854

3955
```java
40-
56+
class Solution {
57+
public boolean isStraight(int[] nums) {
58+
boolean[] t = new boolean[14];
59+
int maxVal = Integer.MIN_VALUE, minVal = Integer.MAX_VALUE;
60+
for (int num : nums) {
61+
if (num == 0) {
62+
continue;
63+
}
64+
if (t[num]) {
65+
return false;
66+
}
67+
t[num] = true;
68+
maxVal = Math.max(maxVal, num);
69+
minVal = Math.min(minVal, num);
70+
}
71+
return maxVal - minVal <= 4;
72+
}
73+
}
4174
```
4275

4376
### ...
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean isStraight(int[] nums) {
3+
boolean[] t = new boolean[14];
4+
int maxVal = Integer.MIN_VALUE, minVal = Integer.MAX_VALUE;
5+
for (int num : nums) {
6+
if (num == 0) {
7+
continue;
8+
}
9+
if (t[num]) {
10+
return false;
11+
}
12+
t[num] = true;
13+
maxVal = Math.max(maxVal, num);
14+
minVal = Math.min(minVal, num);
15+
}
16+
return maxVal - minVal <= 4;
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def isStraight(self, nums: List[int]) -> bool:
3+
t = [False for _ in range(14)]
4+
max_val, min_val = 0, 14
5+
for num in nums:
6+
if num == 0:
7+
continue
8+
if t[num]:
9+
return False
10+
t[num] = True
11+
max_val = max(max_val, num)
12+
min_val = min(min_val, num)
13+
return max_val - min_val <= 4
14+

0 commit comments

Comments
(0)

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