Day 16 胜利属于达瓦里氏!
1. 剑指 Offer 45. 把数组排成最小的数
思路: 本质上是一种排序方法的再构造 注意怎样来定义这个排序的规则 同时了解排序规则的证明 离散数学中的反对称性 和 传递性
class Solution {
public:
static bool cmp(int a , int b){
auto sa = to_string(a), sb = to_string(b);
return sa + sb < sb + sa;
}
string minNumber(vector<int>& nums) {
string res = "";
sort(nums.begin(), nums.end(), cmp);
for(auto x : nums){
res += to_string(x);
}
return res;
}
};
2.剑指 Offer 61. 扑克牌中的顺子
思路 :核心的想法 保证除去大小王的为0的扑克 数组是有序的并且无重复 同时还要考虑到一个点 最大值减去最小值的差值必须小于5 这样才能满足条件
class Solution {
public:
bool isStraight(vector<int>& nums) {
set<int> st;
int _min = 14;
int _max = 0;
for(auto x : nums){
if(x == 0) continue;
_max = max(_max, x);
_min = min(_min, x);
if(st.contains(x)) return false;
st.insert(x);
}
return _max - _min < 5;
}
};