2021-01-18 c++STL

数组

在这里插入图片描述

class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        vector<int> ans;
        for(auto i:nums)
        {
            int pos=abs(i)-1;
            if(nums[pos]>0)nums[pos]=-nums[pos];
        }
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]>0)ans.push_back(i+1);
        }
        return ans;
    }
};

point:重复元素的处理,要记得**if(nums[pos]>0)**nums[pos]=-nums[pos];

在这里插入图片描述
1.原地翻转

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n=matrix.size();
        for(int i=0;i<(n+1)/2;i++)
        {
            for(int j=0;j<n/2;j++)
            {
                int tmp=matrix[i][j];
                matrix[i][j]=matrix[n-1-j][i];
                matrix[n-1-j][i]=matrix[n-1-i][n-1-j];
                matrix[n-1-i][n-1-j]=matrix[j][n-1-i];
                matrix[j][n-1-i]=tmp;
                cout<<matrix[j][n-1-i];
            }
        }        
    }
};

在这里插入图片描述
2.若可以有新空间

在这里插入图片描述

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> hash;
        vector<int> ans;
        for(int i=0;i<nums.size();i++)
        {
            int j=nums[i];
            auto pos=hash.find(target-j);
            if(pos==hash.end())
            {
                hash[j]=i;
            }
            else{
                ans.push_back(pos->second);
                ans.push_back(i);
            }
        }
        return ans; 
    }
};

hash map

nums[i]i
keyvalue
20

在这里插入图片描述

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> hash;
        int ans=0;
        for(auto i:nums)
        {
            hash.insert(i);
        }
        while(!hash.empty()){
            int cur=*(hash.begin());
            int pre=cur-1,next=cur+1;
            hash.erase(cur);
            while(hash.count(pre))
            {
                hash.erase(pre);
                pre--;
            }
            while(hash.count(next))
            {
                hash.erase(next);
                next++;
            }
            ans=max(ans,next-pre-1);
        }
        return ans;
    }
};
nums[i]
全部放进去

然后向前向后查找,不断迭代长度
在这里插入图片描述

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> hashset;
        for(int n:nums)
        {
            if(hashset.count(n)>0)return true;
            else{
                hashset.insert(n);
            }
        }
        return false;
    }
};

1.unordered_set<int> hashset; //定义
2.hashset.insert(3); //插入key值
3.hashset.erase(2); //删除key值
4.hashset.count(2) //判断key值是否在集合中(0/1)
5.hashset.size() //集合大小
6.

for (auto it = hashset.begin(); it != hashset.end(); ++it) 
{        cout << (*it) << " ";    } 

//集合的迭代
7.hashset.clear(); //清除集合
8.hashset.empty() //判断是否为空bool

在这里插入图片描述 1. unordered_map<int, int> hashmap; //定义
2. hashmap.insert(make_pair(0, 0)); //插入(key,value)
3. hashmap[1] = 1; //插入
4. hashmap[1] //得到key对应的value
5. hashmap.erase(2);删除key
6. hashmap.count(2)判断key是否存在
7. hashmap.size() 得到大小
8.

for (auto it = hashmap.begin(); it != hashmap.end(); ++it) {
   cout << "(" << it->first << "," << it->second << ") ";
  1. hashmap.clear();
  2. hashmap.empty())

在这里插入图片描述

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> hashset;
        unordered_set<int> set;
        vector<int> ans;
        for(int i:nums1)
        {
            hashset.insert(i);
        }
        for(int j:nums2)
        {
            if(hashset.count(j))set.insert(j);
        }
        for(auto it=set.begin();it!=set.end();it++)
        {
            ans.push_back(*it);
        }
    return ans;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans;
        unordered_map<int,int> hashmap;
        for(int i:nums1)
        {
            hashmap[i]++;
        }
        for(int j:nums2)
        {
            if(hashmap[j]>0)
            {
                hashmap[j]--;
                ans.push_back(j);               
            }
        }
        return ans;
    }
};

在这里插入图片描述
运用了hashset无法插入重复数字来找循环。
有循环即会无限。

class Solution {
public:
    int getNext(int n)
    {
        int sum=0;
        int i=0;
        while(n>0)
            {
                i=n%10;
                sum+=i*i;
                n=n/10;
            }
        return sum;
    }
    bool isHappy(int n) {
        unordered_set<int> hashset;
        while(!hashset.count(n))
        {
            hashset.insert(n);
            n=getNext(n);            
            if(n==1)return true;
        }     
    return false;
    }
};

在这里插入图片描述
主要是不同字母不能映射到同一字符上,如ado-prr是不可以的。所以需要两个map。

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char,char> hashmaps;
        unordered_map<char,char> hashmapt;
        for(int i=0;i<s.length();i++)
        {
            if(hashmaps.count(s[i])==0)hashmaps[s[i]]=t[i];
            if(hashmapt.count(t[i])==0)hashmapt[t[i]]=s[i];
            if(hashmaps.count(s[i])==1&&hashmaps[s[i]]!=t[i]){
                return false;
            }
            if(hashmapt.count(t[i])==1&&hashmapt[t[i]]!=s[i]){
                return false;
            }
        }
    return true;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
        int miner=100000;
        unordered_map<string,int> hashmap;
        vector<string> ans;
        for(int i=0;i<list1.size();i++)
        {
            for(int j=0;j<list2.size();j++)
            {
                if(list1[i]==list2[j])
                {
                    int sum=i+j;
                    hashmap[list1[i]]=sum;
                }
            }
        }
        for(auto it=hashmap.begin();it!=hashmap.end();it++)
        {
            
            if(it->second<miner){
                miner=it->second;
                ans.clear();
                ans.push_back(it->first);
                cout<<miner<<endl;
            }
            else if(it->second==miner)
            {
                ans.push_back(it->first);
            }
        }
        return ans;
    }
};

在这里插入图片描述

class Solution {
public:
    int firstUniqChar(string s) {
        unordered_map<char,int> hashmap;
        for(char t:s)
        {
            if(hashmap.count(t)==1)hashmap[t]+=1;
            else{
                hashmap[t]=1;
            }
        }
        for(int i=0;i<s.size();i++)
        {
            if(hashmap[s[i]]==1)return i;
        }
    return -1;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> ans;
        unordered_map<string,vector<string>> hashmap;
        for(string s:strs)
        {
            string tmp=s;
            sort(tmp.begin(),tmp.end());
            hashmap[tmp].push_back(s);
        }
        for(auto it=hashmap.begin();it!=hashmap.end();it++)
        {
            ans.push_back(it->second);
        }
        return ans;
    }
};
string(sorted)vector<string
aet[“ate”,“eat”,“tea”]
ant[“nat”,“tan”]
abt[“bat”]

point:排序!!sort
在这里插入图片描述
方法一:对行、列、格子分别遍历。
point:[k/3,k%3】遍历了9个小格子。
int ix=3i+k/3;
int jy=3
j+k%3;

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        unordered_set<char> hashset;
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                if(board[i][j]!='.')
                {
                    if(hashset.count(board[i][j]))return false;
                    hashset.insert(board[i][j]);
                }
            }
            hashset.clear();
        }
        
        for(int j=0;j<9;j++)
        {
            for(int i=0;i<9;i++)
            {
                if(board[i][j]!='.')
                {
                    if(hashset.count(board[i][j]))return false;
                    hashset.insert(board[i][j]);
                }
            }
            hashset.clear();
        }
       for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                for(int k=0;k<9;k++)
                {
                    int ix=3*i+k/3;
                    int jy=3*j+k%3;
                    if(board[ix][jy]!='.')
                    {
                        if(hashset.count(board[ix][jy]))return false;
                        hashset.insert(board[ix][jy]);
                    }
                }
                hashset.clear();
            }            
        }        
        return true;
    }
};


方法二:

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int x[9][9]={0};
        int y[9][9]={0};
        int g[9][9]={0};
        for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                if(board[i][j]!='.')
                {
                    int k=board[i][j]-'1';
                    if(++x[k][i]>1||++y[k][j]>1||++g[k][(i/3)*3+j/3]>1)return false;
                }
                
            }
        }
        return true;
    }
};

对于x[9][9]:即对1-9每个数分别对于每行他有几个
g[9][9],格子:j/3即分别对应0,1,2。往下应该345即加上3,+(i/3)*3

在这里插入图片描述

class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        int ans=0;
        unordered_map<int,int> hashmap;
        for(int l:D)
        {
            for(int k:C)
            {
                hashmap[l+k]++;
            }
        }
        for(int i=0;i<A.size();i++)
        {
            for(int j=0;j<B.size();j++)
            {
                int ll=A[i] + B[j];
                if(hashmap.count(-ll))
                {                    
                    ans+=hashmap[-ll];
                }
            }
        }
        return ans;
    }
};

刚开始四次循环,调到三次再到两次。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值