目录
- 1.二分
- 2.模拟
- 3.双指针
- 4.dfs
- 5.dfs
- 6.位运算
- 7.dp
- 8.dfs
- 9. dfs
- 10.找规律
1.二分
![【力扣打卡--day3】_第1张图片](http://img.e-com-net.com/image/info8/f0f6a3a25ab84008a2a3ae207ddd55c9.jpg)
![【力扣打卡--day3】_第2张图片](http://img.e-com-net.com/image/info8/ddc65059fa6948dfae30e3daad850e52.jpg)
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.模拟
![【力扣打卡--day3】_第3张图片](http://img.e-com-net.com/image/info8/201beace8f5f4829bcac50fae56f177e.jpg)
![【力扣打卡--day3】_第4张图片](http://img.e-com-net.com/image/info8/fe30d57f22e64fe685bf3896f9976ca9.jpg)
![【力扣打卡--day3】_第5张图片](http://img.e-com-net.com/image/info8/d1972a1711ad4a809c64d4ce8a278ac7.jpg)
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.双指针
![【力扣打卡--day3】_第6张图片](http://img.e-com-net.com/image/info8/fccc0138af064bc7bd44f6d644f03691.jpg)
![【力扣打卡--day3】_第7张图片](http://img.e-com-net.com/image/info8/064557f8fac24237b7095d83132ac398.jpg)
![在这里插入图片描述](http://img.e-com-net.com/image/info8/9869129e721e4140aa9836a841c2330f.jpg)
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
![【力扣打卡--day3】_第8张图片](http://img.e-com-net.com/image/info8/e39ddb803e6c4ac5b0953d17694c562f.jpg)
![【力扣打卡--day3】_第9张图片](http://img.e-com-net.com/image/info8/8928c62fc8474347ac901d0eb33d5123.jpg)
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
![【力扣打卡--day3】_第10张图片](http://img.e-com-net.com/image/info8/227139281928483f8a558c0c8fa6435b.jpg)
![【力扣打卡--day3】_第11张图片](http://img.e-com-net.com/image/info8/57559094f48c4235bb5b738fc0d7e3b1.jpg)
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.位运算
![【力扣打卡--day3】_第12张图片](http://img.e-com-net.com/image/info8/26c8784f8ac943cd8bf7b7d9909c8166.jpg)
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
![【力扣打卡--day3】_第13张图片](http://img.e-com-net.com/image/info8/0e54eab6bda14c7fbe73843b6520e1ea.jpg)
class Solution {
public:
8.dfs
![【力扣打卡--day3】_第14张图片](http://img.e-com-net.com/image/info8/fae263461ac945ce82dfd917c0ee49fd.jpg)
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
![【力扣打卡--day3】_第15张图片](http://img.e-com-net.com/image/info8/31eea4ebe7b44571af836dd3fa40fe79.jpg)
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.找规律
![【力扣打卡--day3】_第16张图片](http://img.e-com-net.com/image/info8/97b44a985b09460abb323fd07f21457e.jpg)
![【力扣打卡--day3】_第17张图片](http://img.e-com-net.com/image/info8/3196cc0ed07f4e638be1d62eb314e3b2.jpg)
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]);
}
}
}
};