首页 > 其他分享 >LeetCode(剑指 Offer)- 61. 扑克牌中的顺子

LeetCode(剑指 Offer)- 61. 扑克牌中的顺子

时间:2022-10-06 22:01:32浏览次数:83  
标签:false nums int ++ 61 num return 顺子 LeetCode


题目链接:​点击打开链接​

题目大意:

解题思路

LeetCode(剑指 Offer)- 61. 扑克牌中的顺子_Java C++

相关企业

  • 字节跳动

AC 代码

  • Java
// 解决方案(1)
class Solution {
public boolean isStraight(int[] nums) {
Set<Integer> repeat = new HashSet<>();
int max = 0, min = 14;
for(int num : nums) {
if(num == 0) continue; // 跳过大小王
max = Math.max(max, num); // 最大牌
min = Math.min(min, num); // 最小牌
if(repeat.contains(num)) return false; // 若有重复,提前返回 false
repeat.add(num); // 添加此牌至 Set
}
return max - min < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
}
}

// 解决方案(2)
class Solution {
public boolean isStraight(int[] nums) {
int joker = 0;
Arrays.sort(nums); // 数组排序
for(int i = 0; i < 4; i++) {
if(nums[i] == 0) joker++; // 统计大小王数量
else if(nums[i] == nums[i + 1]) return false; // 若有重复,提前返回 false
}
return nums[4] - nums[joker] < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
}
}

// 解决方案(3)
class Solution {
public boolean isStraight(int[] nums) {
// 计数排序思想
int[] count = new int[14];
for (int i = 0; i < nums.length; i++) {
count[nums[i]]++;
if (count[nums[i]] > 1 && nums[i] != 0) {
return false;
}
}

// 查找左右区间
boolean flagL = true, flagR = false;
int idxL = -1, idxR = -1;
for (int i = 1; i < count.length; i++) {
if (flagL && count[i] == 1) {
idxL = i;
flagL = false;
flagR = true;
} else if (flagR && count[i] == 1) {
idxR = i;
}
}

// 计算缺少坑位数
int needCnt = 0;
for (int i = idxL; i <= idxR; i++) {
if (count[i] == 0) {
needCnt++;
}
}

return needCnt <= count[0];
}
}
  • C++
// 解决方案(1)
class Solution {
public:
bool isStraight(vector<int>& nums) {
unordered_set<int> repeat;
int ma = 0, mi = 14;
for(int num : nums) {
if(num == 0) continue; // 跳过大小王
ma = max(ma, num); // 最大牌
mi = min(mi, num); // 最小牌
if(repeat.find(num) != repeat.end()) return false; // 若有重复,提前返回 false
repeat.insert(num); // 添加此牌至 Set
}
return ma - mi < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
}
};

// 解决方案(2)
class Solution {
public:
bool isStraight(vector<int>& nums) {
int joker = 0;
sort(nums.begin(), nums.end()); // 数组排序
for(int i = 0; i < 4; i++) {
if(nums[i] == 0) joker++; // 统计大小王数量
else if(nums[i] == nums[i + 1]) return false; // 若有重复,提前返回 false
}
return nums[4] - nums[joker] < 5; // 最大牌 - 最小牌 < 5 则可构成顺子
}
};

标签:false,nums,int,++,61,num,return,顺子,LeetCode
From: https://blog.51cto.com/u_10980859/5734070

相关文章

  • leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal 根据前
    一、题目大意给定两个整数数组,preorder和postorder,其中preorder是一个具有无重复值的二叉树的前序遍历,postorder是同一棵树的后序遍历,重构并返回二叉树。如果存......
  • [LeetCode] 1317. Convert Integer to the Sum of Two No-Zero Integers 将整数转换为
    No-Zerointeger isapositiveintegerthat doesnotcontainany 0 initsdecimalrepresentation.Givenaninteger n,return alistoftwointegers [A,......
  • LeetCode 792. Number of Matching Subsequences
    原题链接在这里:https://leetcode.com/problems/number-of-matching-subsequences/题目:Givenastring s andanarrayofstrings words,return thenumberof wor......
  • P7619 [COCI2011-2012#2] RASPORED
    此题解使用平衡树解决。1、原始情况首先,我们考虑未修改的情况。设\(L_i\)为吃饭时间,\(a_i\)为制作所需时间。对于\(n\)个居民,假设我们不对做披萨的顺序进行修改,即按......
  • LeetCode 1419. Minimum Number of Frogs Croaking
    原题链接在这里:https://leetcode.com/problems/minimum-number-of-frogs-croaking/题目:Youaregiventhestring croakOfFrogs,whichrepresentsacombinationofth......
  • leetcode 6 Z字形变化 C/C++ 找规律解法 / 用容器的解法
    找规律,除了一行和两行需要特殊处理之外,其他的规律是一样的。/*class Solution {public:    string convert(string s, int numRows) {       ......
  • LeetCode 15 - 并查集
    所谓并查集,是指支持集合的合并和查询操作的数据结构。合并:将两个集合合并为一个。查询:查询某个元素属于哪个集合,通常是返回集合内的一个代表元素(例如二叉树的所有结点可......
  • P7361 「JZOI-1」拜神 (字符串)
    题意:给一个串,\(Q\)次询问区间\([l,r]\)中至少出现两次的子串的最大长度。写LCT是什么东东以下做法很经典:先求出SA以及height数组,然后按height从大到小每次加......
  • 【教奶奶学SQL】(task6)秋招秘籍A(leetcode刷题)
    学习总结(1)敲黑板:练习第六题再看看,还欠最后1题困难题。(2)​​​猴子题解电子书​​文章目录​​学习总结​​​​练习一:各部门工资最高的员工(leetcode184难度:中等)​​​​......
  • LeetCode 12 - 贪心
    455.分发饼干对每个孩子i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干j,都有一个尺寸s[j] 。如果s[j] >=g[i],我们可以将这个饼干j......