目录
- 1.二分
- 2.模拟
- 3.双指针
- 4.dfs
- 5.dfs
- 6.位运算
- 7.dp
- 8.dfs
- 9. dfs
- 10.找规律
1.二分
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
if(nums.empty()) return {-1,-1};
int l=0,r=nums.size()-1;
while(l<r){
int mid=l+r>>1;
if(nums[mid]>=target) r=mid;
else l=mid+1;
}
if(nums[l]!=target) return {-1,-1};
int l1=0,r1=nums.size()-1;
while(l1<r1){
int mid=l1+r1+1>>1;
if(nums[mid]<=target) l1=mid;
else r1=mid-1;
}
return {l,r1};
}
};
2.模拟
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool st[9];
for(int i=0;i<9;i++){
memset(st,0,sizeof st);
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
int t=board[i][j]-'1';
if(st[t]) return false;
st[t]=1;
}
}
}
for(int i=0;i<9;i++){
memset(st,0,sizeof st);
for(int j=0;j<9;j++){
if(board[j][i]!='.'){
int t=board[j][i]-'1';
if(st[t]) return false;
st[t]=1;
}
}
}
for(int i=0;i<9;i+=3){
for(int j=0;j<9;j+=3){
memset(st,0,sizeof st);
for(int x=0;x<3;x++){
for(int y=0;y<3;y++){
if(board[i+x][j+y]!='.'){
int t=board[i+x][j+y]-'1';
if(st[t]) return false;
st[t]=1;
}
}
}
}
}
return true;
}
};
3.双指针
class Solution {
public:
string countAndSay(int n) {
string s="1";
for(int i=0;i<n-1;i++){
string t;
for(int j=0;j<s.size();){
int k=j+1;
while(k<s.size()&&s[k]==s[j]) k++;
t+=to_string(k-j)+s[j];
j=k;
}
s=t;
}
return s;
}
};
4.dfs
class Solution {
public:
vector<vector<int>> res;
vector<int> cand;
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
int N=candidates.size();
dfs(candidates,target,0,0);
return res;
}
void dfs(vector<int>& can
,int target,int now,int i){
if(now==target){
res.push_back(cand);
return;
}else if(now>target){
return;
}
for(;i<can.size();i++){
now+=can[i];
cand.push_back(can[i]);
dfs(can,target,now,i);
now-=can[i];
cand.pop_back();
}
}
};
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<vector<int>> combinationSum(vector<int>& c, int t) {
dfs(c,0,t);
return res;
}
void dfs(vector<int>& c,int idx,int t){
if(t==0){
res.push_back(path);
return;
}
if(idx==c.size()) return;
for(int i=0;i*c[idx]<=t;i++){
dfs(c,idx+1,t-c[idx]*i);
path.push_back(c[idx]);
}
for(int i=0;i*c[idx]<=t;i++){
path.pop_back();
}
}
};
5.dfs
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<vector<int>> combinationSum2(vector<int>& c, int t) {
sort(c.begin(),c.end());
dfs(c,0,t);
return res;
}
void dfs(vector<int>& c,int idx,int t){
if(t==0){
res.push_back(path);
return;
}
if(idx==c.size()) return;
int cnt=idx+1;
while(cnt<c.size()&&c[cnt]==c[idx]) cnt++;
int k=cnt-idx;
for(int i=0;i*c[idx]<=t&&i<=k;i++){
dfs(c,cnt,t-c[idx]*i);
path.push_back(c[idx]);
}
for(int i=0;i*c[idx]<=t&&i<=k;i++){
path.pop_back();
}
}
};
6.位运算
class Solution {
public:
string multiply(string num1, string num2) {
vector<int> A,B;
int N1=num1.size(),N2=num2.size();
for(int i=N1-1;i>=0;i--) A.push_back(num1[i]-'0');
for(int i=N2-1;i>=0;i--) B.push_back(num2[i]-'0');
vector<int> C(N1+N2);
for(int i=0;i<N1;i++)
for(int j=0;j<N2;j++)
C[i+j]+=A[i]*B[j];
for(int i=0,t=0;i<C.size();i++){
t+=C[i];
C[i]=t%10;
t/=10;
}
int k=C.size()-1;
while(k>0&&!C[k]) k--;
string res;
while(k>=0) res+=C[k--]+'0';
return res;
}
};
7.dp
class Solution {
public:
8.dfs
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<int> st;
vector<vector<int>> permute(vector<int>& nums) {
st=vector<int>(nums.size());
path=vector<int>(nums.size());
dfs(nums,0);
return res;
}
void dfs(vector<int>& nums,int cnt){
if(cnt==nums.size()){
res.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
if(!st[i]){
path[cnt]=nums[i];
st[i]=1;
dfs(nums,cnt+1);
st[i]=0;
}
}
}
};
9. dfs
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<int> st;
vector<vector<int>> permuteUnique(vector<int>& nums) {
st=vector<int>(nums.size());
path=vector<int>(nums.size());
sort(nums.begin(),nums.end());
dfs(nums,0);
return res;
}
void dfs(vector<int>& nums,int cnt){
if(cnt==nums.size()){
res.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
if(!st[i]){
if(i&&nums[i]==nums[i-1]&&!st[i-1]) continue;
path[cnt]=nums[i];
st[i]=1;
dfs(nums,cnt+1);
st[i]=0;
}
}
}
};
10.找规律
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
int N=matrix.size();
for(int i=0;i<N;i++){
for(int j=0;j<i;j++){
swap(matrix[i][j],matrix[j][i]);
}
}
for(int i=0;i<N;i++){
for(int j=0,k=N-1;j<k;j++,k--){
swap(matrix[i][j],matrix[i][k]);
}
}
}
};