数组
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 |
---|---|
key | value |
2 | 0 |
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 << ") ";
hashmap.clear();
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=3j+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;
}
};
刚开始四次循环,调到三次再到两次。