1.dfs
class Solution {
public:
vector<string> ans;
string a[10]={
"","","abc","def",
"ghi","jkl","mno",
"pqrs","tuv","wxyz"
};
vector<string> letterCombinations(string digits) {
if(digits=="") return ans;
dfs(digits,0,"");
return ans;
}
void dfs(string& digits,int idx,string tmp){
if(idx==digits.size()) ans.push_back(tmp);
else{
for(auto c:a[digits[idx]-'0']){
dfs(digits,idx+1,tmp+c);
}
}
}
};
2.双指针
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
vector<vector<int>> res;
int N=nums.size();
for(int i=0;i<N;i++){
if(i&&nums[i]==nums[i-1]) continue;
for(int j=i+1;j<N;j++){
if(j>i+1&&nums[j]==nums[j-1]) continue;
for(int a=j+1,b=N-1;a<b;a++){
if(a>j+1&&nums[a]==nums[a-1]) continue;
while(b-1>a&&(long long)nums[i]+nums[j]+nums[a]+nums[b-1]>=target) b--;
if(((long long)nums[i]+nums[j]+nums[a]+nums[b])==target){
res.push_back({nums[i],nums[j],nums[a],nums[b]});
}
}
}
}
return res;
}
};
3.双指针
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy=NULL;
int l=1;
for(ListNode* i=head;i;i=i->next){
if(dummy!=NULL) dummy=dummy->next;
if(l==n+1){
dummy=head;
}
l++;
}
if(dummy) dummy->next=dummy->next->next;
else return head->next;
return head;
}
};
4.dfs
class Solution {
public:
vector<string> res;
vector<string> generateParenthesis(int n) {
dfs("",0,0,n);
return res;
}
void dfs(string tmp,int cnt1,int cnt2,int n){
if(cnt1==n&&cnt2==n){
res.push_back(tmp);
}
else{
if(cnt1<n) dfs(tmp+'(',cnt1+1,cnt2,n);
if(cnt1>cnt2&&cnt2<n) dfs(tmp+')',cnt1,cnt2+1,n);
}
}
};
5.dfs
class Solution {
public:
vector<string> res;
vector<string> generateParenthesis(int n) {
dfs("",0,0,n);
return res;
}
void dfs(string tmp,int cnt1,int cnt2,int n){
if(cnt1==n&&cnt2==n){
res.push_back(tmp);
}
else{
if(cnt1<n) dfs(tmp+'(',cnt1+1,cnt2,n);
if(cnt1>cnt2&&cnt2<n) dfs(tmp+')',cnt1,cnt2+1,n);
}
}
};
6.双指针
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummy=new ListNode(-1);
dummy->next=head;
for(auto p=dummy;p->next&&p->next->next;){
ListNode* a=p->next,* b=p->next->next;
p->next=b;
a->next=b->next;
b->next=a;
p=a;
}
return dummy->next;
}
};
7.位运算(快速幂)
class Solution {
public:
int divide(int dividend, int divisor) {
typedef long long LL;
vector<LL> exp;
bool is_minus=false;
if(dividend<0&&divisor>0||divisor<0&÷nd>0) is_minus=true;
LL a=abs(LL(dividend)),b=abs(LL(divisor));
for(LL i=b;i<=a;i+=i){
exp.push_back(i);
}
LL res=0;
for(int i=exp.size()-1;i>=0;i--){
if(a>=exp[i]){
res+=1ll<<i;
a-=exp[i];
}
}
if(is_minus) res=-res;
if(res<INT_MIN||res>INT_MAX) return INT_MAX;
return res;
}
};
8.找规律
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int k = nums.size() - 1;
while (k > 0 && nums[k] <= nums[k-1]) {
k--;
}
if (k > 0) {
int t = k;
while (t<nums.size() && nums[t] > nums[k-1]) {
t++;
}
swap(nums[t-1], nums[k-1]);
reverse(nums.begin() + k, nums.end());
}else{
reverse(nums.begin(), nums.end());
}
}
};
10.二分
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size()==0) return -1;
int l=0,r=nums.size()-1;
while(l<r){
int mid=l+r+1>>1;
if(nums[0]<=nums[mid]) l=mid;
else r=mid-1;
}
if(target>=nums[0]){
l=0;
}else{
l=l+1,r=nums.size()-1;
}
while(l<r){
int mid=l+r>>1;
if(nums[mid]>=target) r=mid;
else l=mid+1;
}
if(target==nums[r]) return r;
return -1;
}
};
|