217:存在重复元素
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> s;
for(int x:nums){
if(s.find(x)!=s.end()){
return true;
}
s.insert(x);
}
return false;
}
};
go语言的比较迷惑,只能暂时理解为map[int]struct{}{}是定义了一个set,然后之后的set[v] = struct{}{}是将v插入哈希表。 if _, has := set[v]; has { return true},这一句我的理解是两句都是if语句,并列写省一个if。
func containsDuplicate(nums []int) bool {
set := map[int]struct{}{}
for _, v := range nums {
if _, has := set[v]; has {
return true
}
set[v] = struct{}{}
}
return false
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/contains-duplicate/solution/cun-zai-zhong-fu-yuan-su-by-leetcode-sol-iedd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
389:找不同
vector<int>a,b(n,0)的意思就是 创建了一个 int 类型的空的vector容器a,和一个 int 类型n个元素,且值均为0的vecotr容器b。
class Solution {
public:
char findTheDifference(string s, string t) {
vector<int> a(26,0);
for(char x:s){
//指针++
a[x-'a']++;
}
for(char y:t){
a[y-'a']--;
if(a[y-'a']<0){
return y;
}
}
//注意‘’中间有空格
return ' ';
}
};
func findTheDifference(s, t string) byte {
cnt := [26]int{}
for _, ch := range s {
cnt[ch-'a']++
}
for i := 0; ; i++ {
ch := t[i]
cnt[ch-'a']--
if cnt[ch-'a'] < 0 {
return ch
}
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/find-the-difference/solution/zhao-bu-tong-by-leetcode-solution-mtqf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
496.下一个最大元素(先贴过来,慢慢看)
vector<int> nextGreaterElement(vector<int>& nums) {
vector<int> ans(nums.size()); // 存放答案的数组
stack<int> s;
for (int i = nums.size() - 1; i >= 0; i--) { // 倒着往栈里放
while (!s.empty() && s.top() <= nums[i]) { // 判定个子高矮
s.pop(); // 矮个起开,反正也被挡着了。。。
}
ans[i] = s.empty() ? -1 : s.top(); // 这个元素身后的第一个高个
s.push(nums[i]); // 进队,接受之后的身高判定吧!
}
return ans;
}
作者:labuladong
链接:https://leetcode-cn.com/problems/next-greater-element-i/solution/dan-diao-zhan-jie-jue-next-greater-number-yi-lei-w/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int>s;//临时栈
unordered_map<int,int>num_map; //哈希表
int size = nums1.size();
vector<int>vec_num;//存储下一个最大值的vector
for(int i = 0;i < nums2.size();++i)
{
while(!s.empty() && nums2[i]>s.top())
{
//num_map.insert(s.pop(),nums2[i]);
num_map[s.top()] = nums2[i];
s.pop();
}
s.push(nums2[i]);
}
while(!s.empty())
{
num_map[s.top()] = -1;
s.pop();
}
for(int i = 0;i<size;++i)
{
vec_num.push_back(num_map[nums1[i]]);
}
return vec_num;
}
};
func nextGreaterElement(nums1 []int, nums2 []int) []int {
m := make(map[int]int)
var stack []int
for _, v := range nums2 {
for len(stack) != 0 && v > stack[len(stack)-1] {
// 发现有更大的数字,给其下一个更大数字赋值
m[stack[len(stack)-1]] = v
stack = stack[:len(stack)-1]
}
stack = append(stack, v)
}
for k, v := range nums1 {
if value, ok := m[v]; ok {
nums1[k] = value
} else {
nums1[k] = -1
}
}
return nums1
}
作者:bloodborne
链接:https://leetcode-cn.com/problems/next-greater-element-i/solution/golangdan-diao-zhan-fa-by-bloodborne/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。