5.回文串

几种方法:

暴力:枚举每一个字串,判断是否为回文串,复杂度O(n^3),暴力月莫不可取

dp:区间dp思想,O(n^2)

中心扩展:找每一个字符,然后往两边扩展,O(n^2)

manacher算法:主要是中心扩展的一个优化,链接,这篇讲的易懂

6.ZigZag Conversion

P   A   H   N
A P L S I I G
Y I R

之前用了数学方法,累死了,直接搞个每行的string存一下就行

11. Container With Most Water

Start by evaluating the widest container, using the first and the last line. All other possible containers are less wide, so to hold more water, they need to be higher. Thus, after evaluating that widest container, skip lines at both ends that don't support a higher height. Then evaluate that new container we arrived at. Repeat until there are no more possible containers left.

22.Generate Parentheses

递归

 class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<string> ans;
vector<string> ans1;
vector<string> ans2;
for(int i=;i<*n;i+=){
ans1=generateParenthesis((i-)/);
ans2=generateParenthesis((*n-i-)/);
ans1.push_back("");
ans2.push_back("");
for(int j=;j<ans1.size();j++){
for(int k=;k<ans2.size();k++){
string m='('+ans1[j]+')'+ans2[k];
if(m.length()!=*n) continue;
ans.push_back(m);
}
}
}
return ans;
}
};

23.**

一堆链表的合并 优先队列,学习了一个在class怎么用

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution { public:
struct cmp {
bool operator()(const ListNode* l, const ListNode* r) {
return l->val > r->val;
}
}; ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode *,vector<ListNode *>,cmp> q;
for(int i=;i<lists.size();i++){
if(lists[i]) q.push(lists[i]);
}
if(q.empty()) return NULL;
ListNode *ans=q.top(); q.pop();
if(ans->next) q.push(ans->next);
ListNode *aans=ans;
while(!q.empty()){
aans->next=q.top();
q.pop();
aans=aans->next;
if(aans->next) q.push(aans->next);
}
return ans; }
};

24.**

交换链表相邻元素的值

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode **ans=&head,*a; //ans是指向head的指针
ListNode *ha=NULL;
while(a=*ans){
if(a->next==NULL){
break;
}
ha=a->next;
a->next=a->next->next;
ha->next=a;
*ans = ha;
ans=&(a->next);
}
return head;
}
};

25.***

24的加强版,交换一个区间内的元素,方法很巧妙

26.Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

 class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size()==) return ;
int w=nums[],ans=;
for(int i=;i<nums.size();i++){
if(nums[i]!=w){
w=nums[i];
nums[ans]=w;
ans++;
}
}
return ans;
}
};

27.Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

 class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if(nums.size()==) return ;
int ans=;
for(int i=;i<nums.size();i++){
if(val!=nums[i]){
nums[ans++]=nums[i];
}
}
return ans;
}
};

28.KMP

 class Solution {
public:
int strStr(string haystack, string needle) { string x=needle;
string y=haystack;
int m=needle.length();
int n=haystack.length();
if(m==) return ;
int next[];
int i,j;
j=next[]=-;
i=;
while(i<m)
{
while(-!=j && x[i]!=x[j])j=next[j];
next[++i]=++j;
} int ans=;
i=j=;
while(i<n)
{
//printf("%d\n",i);
while(-!=j && y[i]!=x[j])j=next[j];
i++;j++;
if(j>=m)
{
ans=i;
break;
}
if(ans) break;
}
if(ans-m<) return -;
return ans-m;
} };

30.给定字符串s和一堆相同长度字符串words,如果这个s里有一段字符串包含了words里所有的字符串,并且没有间隔,则输出这段字符串的首位

没有时间限制真的爽,随便怎么写

 class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ans;
map<string,int> word;
int siz=words.size();
int len=s.length();
int wl=words[].length();
for(int i=;i<siz;i++){
word[words[i]]++;
}
for(int i=;i<len-siz*wl+;i++){ //需要遍历的次数
map<string,int> word2;
int j=;
for(;j<siz;j++){
string w=s.substr(i+j*wl,wl);
if(word.find(w)!=word.end()){
word2[w]++;
if(word2[w]>word[w]){
break;
}
}
else break;
}
if(j==siz) ans.push_back(i);
}
return ans;
}
};

31.生成下一个排列,用STL一行解决,爽歪歪

 class Solution {
public:
void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(),nums.end());
}
};

32.找到字符串中正确的最长括号串,用stack消除括号,并累计数字到它的栈顶,如果这个栈顶继续消除的话,就会继续累计的下一个地方

 class Solution {
public:
int longestValidParentheses(string s) {
int len=s.length();
int ans=;
stack<int> st;
vector<int> f;
f.push_back();
st.push();
for(int i=;i<len;i++){
int w;
if(s[i]=='(') w=-;
else w=;
if(w==-st.top()&&w==){ //可消除
st.pop();
int o=st.size();
o--;
f[o]=+f[o+]+f[o];
f.erase(f.begin()+o+);
if(f[o]>ans){
ans=f[o];
}
}
else{ //无法消除
st.push(w);
f.push_back();
}
}
return ans;
}
};

33.在一个重新排了一半的有序序列中,找到目标值,23333,好题!需要多做几遍 ********************

34.在一个有序序列中,找到目标值的范围     ,写了个logn的方法居然还没On的方法快。。。然后加了点剪枝

 class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int n=nums.size();
int l=,r=n-,mid,ans1=-,ans2=-;
vector<int> ans;
while(l<=r){
mid=(l+r)>>;
if(nums[mid]==target){
ans1=mid;
l=mid+;
}
else if(nums[mid]<target)l=mid+;
else r=mid-;
}
if(ans1==-){
ans.push_back(ans2);
ans.push_back(ans1);
return ans;
}
l=,r=ans1;
while(l<=r){
mid=(l+r)>>;
if(nums[mid]==target){
ans2=mid;
r=mid-;
}
else if(nums[mid]<target)l=mid+;
else r=mid-;
} ans.push_back(ans2);
ans.push_back(ans1);
return ans;
}
};

35.找到一个数或插入到有序序列中,裸二分

 class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int n=nums.size();
int l=,r=n-,mid;
while(l<=r){
mid=(l+r)>>;
if(nums[mid]<target) l=mid+;
else r=mid-;
}
return l;
}
};

36.判断一个数组是否合理,模拟即可

 class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
//检验的基准就是每9个中是否存在重复数字
//先检验横行
int vis[];
int i,j,k,u,v;
bool flag=;
for(i=;i<;i++){
for(k=;k<;k++) vis[k]=;
for(j=;j<;j++){
if(board[i][j]=='.') continue;
int w=board[i][j]-'';
if(vis[w]){
flag=;
break;
}
else vis[w]=;
}
if(!flag){
break;
}
}
if(!flag) return ; //再检验纵行
for(i=;i<;i++){
for(k=;k<;k++) vis[k]=;
for(j=;j<;j++){
if(board[j][i]=='.') continue;
int w=board[j][i]-'';
if(vis[w]){
flag=;
break;
}
else vis[w]=;
}
if(!flag){
break;
}
}
if(!flag) return ; //再检验九宫格
for(u=;u<;u++){
for(v=;v<;v++){
for(k=;k<;k++) vis[k]=;
for(i=u*;i<=u*+;i++){
for(j=v*;j<=v*+;j++){
if(board[i][j]=='.') continue;
int w=board[i][j]-'';
if(vis[w]){
flag=;
break;
}
else vis[w]=;
}
}
if(!flag) break;
}
if(!flag) break;
}
return flag;
}
};

37.dancing link解数独

38.水

39.数的组合,dfs即可

 class Solution {
public:
int n;
vector<vector<int>> finans;
vector<int> nums;
void fun(int pos,int target,vector<int> w){
/*
printf("------%d %d\n",pos,target);
for(int i=0;i<w.size();i++){
printf("%d ",w[i]);
}
printf("\n");
*/
if(target<) return;
if(target==){
finans.push_back(w);
return;
}
if(pos+!=n) fun(pos+,target,w);
w.push_back(nums[pos]);
fun(pos,target-nums[pos],w);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
n=candidates.size();
vector<int> ans;
nums.assign(candidates.begin(),candidates.end());
fun(,target,ans);
return finans;
}
};

40. 和上一题类似,不过每个数只能被选一次,比较不错的题 ***************

 class Solution {
public:
int n;
void fun(vector<int>& nums,int pos,int target,vector<int>& w,vector<vector<int>>& finans){
if(target==){
finans.push_back(w);
return;
}
for(int i=pos;i<n;i++){
if(i!=pos&&nums[i]==nums[i-]) continue; //不选之前选过的
if (nums[i]>target) return;
w.push_back(nums[i]);
fun(nums,i+,target-nums[i],w,finans);
w.pop_back();
}
}
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
sort(candidates.begin(),candidates.end());
n=candidates.size();
vector<int> ans;
vector<vector<int>> finans;
fun(candidates,,target,ans,finans);
return finans;
}
};

41.遍历一遍,该放的数字放到该放的地方

 class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n=nums.size(),i,ans;
if(n==) return ;
for(i=;i<n;i++){
while(nums[i]!=i+){
if(nums[i]>n||nums[i]<=||nums[i]==nums[nums[i]-]) break;
swap(nums[i],nums[nums[i]-]);
}
}
for(i=;i<n;i++){
if(nums[i]!=i+){
ans=i+;
break;
}
}
if(i==n) return n+;
return ans;
}
};

42. 不错的题,一开始的想法是找出递减递增的序列,计算一下,后来发现5,1,2,1,5这样的计算不出来,达到顶点的值仍然可能有水 *****************

44.总之是一个比较复杂的题,题意就是给两串字符,看是否匹配,有?代表单个字符,*代表连续字符(可以为空)代码里有比较详细的解释 *******

 /*
看一下例子
s=abbcb
p=a*bcb 首先肯定匹配s1,p1,两个相同,继续匹配,这时候i=2,j=2
接下来p遇到*,这里标记fi=1,fj=1,但是j我们需要++,j会变成3,因为此时我们默认*代表的是空,所以接下来匹配的应该是s2,p3
s2==p3,这时候i=3,p=4,我们发现不匹配,但是之前有*号,那么可能*号会被用到,因此,令i,j等于之前我们标记的地方,i=2,j=1
这里++fi的原因是因为我们用到了*号,所以i里的字符会被匹配一个,自然fi就需要++
这时候p1依旧等于*,因此继续标记fi=2,fj=1,i=3,j=3;
这时候s3=p3,匹配成功,相对于之前的s2==p3,我们是往*号的地方添加了一个s[fi],也就是b
之后剩下的都继续匹配成功 */
class Solution {
public:
bool isMatch(string s, string p) {
int i=,j=,fi=-,fj=-;
int l1=s.length(),l2=p.length();
while(i<l1){
if(s[i]==p[j]||p[j]=='?') i++,j++; //单个字符匹配成功
else if(p[j]=='*'){
fi=i;
fj=j++; //暂且将其*设为空
}
else{ //都不匹配,看之前的*能不能用,前提是有*存在
if(fj!=-){
i=++fi;
j=fj;
}
else return false;
}
}
while(p[j]=='*')j++;
return j==l2;
}
};

45.  *****************

Given array A = [2,3,1,1,4]

The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)

 class Solution {
public:
int jump(vector<int>& nums) {
int n=nums.size();
int ans=;
int curMax=;
int curreach=;
for(int i=;i<n;i++){
if(curreach<i){
ans++;
curreach=curMax;
} curMax=max(curMax,nums[i]+i);
}
return ans;
}
};

50.做快一点,水题就不记录了,double的快速幂,原理一样,注意n为负的情况

 class Solution {
public:
double myPow(double x, int n) {
double ret=;
double tmp=x;
bool flag=;
long long nn=n;
if(nn<) nn=-nn,flag=;
while(nn){
if(nn&) ret=(ret*tmp);
tmp=tmp*tmp;
nn>>=;
}
if(flag) return 1.0/ret;
return ret;
}
};

51.N皇后问题,小白书上有,比较巧妙的地方就是判断斜线处是否合理

 class Solution {
public:
int c[],tot=;
vector<vector<string>> ans;
void search(int cur,int n){
int i,j;
if(cur==n){
vector<string> anss;
for(i=;i<n;i++){
string s="";
for(j=;j<n;j++){
if(j==c[i]) s+='Q';
else s+='.';
}
anss.push_back(s);
}
ans.push_back(anss);
}
else for(i=;i<n;i++){
int ok=;
c[cur]=i;
for(j=;j<cur;j++){
if(c[cur]==c[j]||c[cur]-cur==c[j]-j||c[cur]+cur==c[j]+j){
ok=;
break;
}
}
if(ok) search(cur+,n);
}
}
vector<vector<string>> solveNQueens(int n) {
search(,n);
return ans;
}
};

56. cmp要加上static

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
static bool cmp(Interval A,Interval B){
if(A.start!=B.start) return A.start<B.start;
else return A.end<B.end;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> ans;
int n=intervals.size();
if(intervals.empty())
return ans;
sort(intervals.begin(),intervals.end(),cmp);
int st=,ed=,stval=intervals[].start,edval=intervals[].end;
for(int i=;i<n;i++){
if(intervals[i].start<=edval){
edval=max(edval,intervals[i].end);
}
else{
ans.push_back(Interval(stval,edval));
stval=intervals[i].start;
edval=intervals[i].end;
}
}
ans.push_back(Interval(stval,edval));
return ans;
}
};

57.写成一坨

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
vector<Interval> ans;
int n=intervals.size();
int flag=;
if(intervals.empty()){
ans.push_back(newInterval);
return ans;
}
int st=,ed=,stval=newInterval.start,edval=newInterval.end;
for(int i=;i<n;i++){
if(intervals[i].end>=stval&&flag==){ //find the newInterval
if(intervals[i].start>edval){ //the newInterval does not cover any of intervals,this can be seen as an individual interval
ans.push_back(Interval(stval,edval));
flag=-;
i--;
continue;
}
flag=;
stval=min(stval,intervals[i].start);
edval=max(intervals[i].end,edval);
if(i==n-){
ans.push_back(Interval(stval,edval));
}
continue;
} if(intervals[i].start<=edval&&flag==){ //the end of the newinterval can be extended
edval=max(edval,intervals[i].end);
if(i==n-){
ans.push_back(Interval(stval,edval));
}
continue;
}
else if(flag==){
flag=-;
ans.push_back(Interval(stval,edval));
} ans.push_back(Interval(intervals[i].start,intervals[i].end)); }
if(!flag){
ans.push_back(newInterval);
}
if(flag!=-){ }
return ans;
}
};

60.找出第k个排列 *************************

62.从左上角到右下角的路径数目,数学方法看一下 ***

68. Text Justification  比较无聊的一题 就是按要求搞就行

 class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int i,j,k,num;
int numspace,avnumspace,extraspace;
int n=words.size();
vector<string> ans;
for(i=;i<n;i+=num){ //k means how many word you use in this line
int len=;
for(j=i;j<n;j++){ //there have j-i words which will be use in this line from i to j-i
if(len+(j==i?words[j].length():words[j].length()+)>maxWidth) break;
else len+=(j==i?words[j].length():words[j].length()+);
//cout<<words[j]<<" "<<len<<endl;
}
num=j-i;
//the next step is to caculate the number of " "between each words
numspace=maxWidth-len+num-;
if(num!=) avnumspace=numspace/(num-);
else avnumspace=numspace;
if(num!=) extraspace=numspace%(num-);
else extraspace=;
//printf("%d %d %d %d %d\n",num,numspace,avnumspace,extraspace,len);
string s=words[i];
if(i+num==n){
for(j=i+;j<i++num-;j++){
s=s+" "+words[j];}
numspace-=num-;
while(numspace>)s+=" ",numspace--; }
else{ if(num==){
for(int u=;u<avnumspace;u++){
s=s+" ";
}
}
for(j=i+;j<i++num-;j++){
for(int u=;u<avnumspace;u++){
s=s+" ";
}
if(extraspace>) s+=" ",extraspace--;
s+=words[j];
}
} //cout<<s<<endl;
ans.push_back(s);
}
return ans;
}
};

71.哇!这题厉害了,大意就是unix路径简化,以前好像在CCF上写过,但是写的一坨,看了discuss后发现C++有个分离函数,爽死了 ****

72 修改字符串使之成为相同的,可以增改删 ******************

 class Solution {
public:
int minDistance(string word1, string word2) {
int dp[][];
int len1 = word1.length();
int len2 = word2.length();
int i,j;
for(i=;i<=len1;i++){
dp[i][]=i;
}
for(i=;i<=len2;i++){
dp[][i]=i;
}
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
dp[i][j]=min(dp[i-][j]+,min(dp[i][j-]+,dp[i-][j-]+(word1[i-]==word2[j-]?:)));
}
}
return dp[len1][len2];
}
};

75.对于只包含0,1,2的数组排序,一种方法是设置01,2的分解index,另一种是记录前面的0,1,2个数,值得一看的题**************

76. 想了个方法但比较难实现,先mark

79.观察一个二维字符数组中是否存在一个字符串 回溯

 class Solution {
public:
bool flag=;
int n,m;
int len;
vector<vector<char>> board1;
string word1;
bool vis[][];
void dfs(int h,int w,int pos){
//printf("%d %d %d\n",h,w,pos);
bool b1=,b2=,b3=,b4=;
if(flag==) return;
if(pos==len){
flag=;
return;
}
if(h+<n&&board1[h+][w]==word1[pos]&&!vis[h+][w]) b1=,vis[h+][w]=,dfs(h+,w,pos+);
if(b1) vis[h+][w]=;
if(h->=&&board1[h-][w]==word1[pos]&&!vis[h-][w]) b2=,vis[h-][w]=,dfs(h-,w,pos+);
if(b2) vis[h-][w]=;
if(w->=&&board1[h][w-]==word1[pos]&&!vis[h][w-]) b3=,vis[h][w-]=,dfs(h,w-,pos+);
if(b3) vis[h][w-]=;
if(w+<m&&(board1[h][w+]==word1[pos])&&!vis[h][w+]) b4=,vis[h][w+]=,dfs(h,w+,pos+);
if(b4) vis[h][w+]=; } bool exist(vector<vector<char>>& board, string word) {
n=board.size();
m=board[].size();
len=word.length();
board1=board;
word1=word;
memset(vis,,sizeof(vis));
if(n*m<len) return false;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(board[i][j]==word[]) vis[i][j]=,dfs(i,j,);
vis[i][j]=;
}
}
return flag;
}
};

84.嘎嘎嘎,单调栈水题

 class Solution {
public:
int largestRectangleArea(vector<int>& heights) {
pair<int,int> ele[];
int height, n;
n=heights.size();
int w;
int i,j,k;
int ans, tot, tmp;
int top=;
ans = ;
for(i=;i<n;i++){
tmp=;
w=heights[i];
while(top>&&w<ele[top-].first){
int aans=ele[top-].first*(ele[top-].second+tmp);
ans=max(ans,aans);
tmp+=ele[top-].second; //这个比较容易忘,这是当前累计的加上之前累计的
top--;
}
ele[top].first=w;
ele[top].second=+tmp;
top++;
}
tmp=;
while(top>){
int aans=ele[top-].first*(ele[top-].second+tmp);
ans=max(ans,aans);
tmp+=ele[top-].second;
top--;
}
return ans;
}
};

87.递归

 class Solution {
public:
int c[];
bool isScramble1(string s1, string s2) {
//cout<<s1<<" "<<s2<<endl;
for(int i=;i<;i++){
c[i]=;
}
for(int i=;i<s1.length();i++){
c[s1[i]-'a']++;
c[s2[i]-'a']--;
}
for(int i=;i<;i++){
if(c[i]!=) return false;
}
string ss1=s1;
reverse(ss1.begin(),ss1.end());
if(ss1==s2) return true;
if(s1==s2) return true;
else if(s1!=s2&&s1.length()==) return false;
else{
bool flag=;
for(int i=;i<s1.length();i++){
//printf("%d %d\n",i,s1.length());
string s1l,s2l,s1r,s2r,s1lr,s1rr; s1l=s1.substr(,i);
s1r=s1.substr(i);
s2l=s2.substr(,i);
s2r=s2.substr(i);
s1lr=s1l;
reverse(s1lr.begin(),s1lr.end());
s1rr=s1r;
reverse(s1rr.begin(),s1rr.end());
//cout<<s1l<<" "<<s1r<<" "<<s2l<<" "<<s2r<<" "<<s1lr<<" "<<s1rr<<" "<<s1<<" "<<s2<<endl;
if( (isScramble1(s1l,s2l)||isScramble1(s1lr,s2l))&&(isScramble1(s1r,s2r)||isScramble1(s1rr,s2r))){
flag=;
if(flag) break;
}
}
return flag; }
}
bool isScramble(string s1,string s2){
string ss1=s1;
reverse(ss1.begin(),ss1.end());
return isScramble1(s1,s2)||isScramble1(ss1,s2);
}
};

89.格雷码 *****

90.interesting

 /*
I think it's a truly interesting problem. In the last problem----Subsets,it's integers are dinstinct, so there is no need to consider the duplicate subsets, but in this problem, we need to take this into acount.
Let's try to see why does duplicate subsets emerge.
For example, For the nums=[1,2,2,3,3],we use primary ways to solve it, we can get [],[1],[1,2],[1,2,2],[1,2,2,3],[1,2,2,3,3] in turn, and from now on, we should backtrack, we got [1,2,2,3], this '3' comes from the second of '3's,and the duplicae subsets emerge.
So how can we avoid situation? Try to think about this, we have used '3', so we can't use it again, it means we can't use '3' if we have used it before. so we can add "if(nums[i]==nums[i-1]) continue;", but another problem happened, the subset[1,2,2,3] is abosolutely fit in the require, but '2' happened twice, so we must reserve the succussive value i!=num+1. Finally, we got the right answer successfully!
*/ class Solution {
public:
void dfs(int pos,int num,vector<int> ans1,vector<vector<int> >&finans,int n,vector<int> nums){
finans.push_back(ans1);
for(int i=num+;i<n;i++){
if(nums[i]==nums[i-]&&i!=num+) continue;
ans1.push_back(nums[i]);
dfs(pos+,i,ans1,finans,n,nums);
ans1.pop_back();
}
}
vector<vector<int>> subsetsWithDup(vector<int>& nums) {
vector<vector<int> > finans;
sort(nums.begin(),nums.end());
int n=nums.size();
vector<int> ans1;
finans.push_back(ans1);
for(int i=;i<n;i++){
if(nums[i]==nums[i-]&&i!=) continue;
ans1.push_back(nums[i]);
dfs(,i,ans1,finans,n,nums);
ans1.pop_back();
}
return finans;
}
};

98.判断二叉查找树,  中序遍历二叉查找树可得到一个关键字的有序序列。或者记录low high

Leetcode 记录(1~100)的更多相关文章

  1. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  2. Leetcode 记录(101~200)

    Now, I want to just use English to explain the problem, it's about two month before the interview, s ...

  3. LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

    100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...

  4. &lt;LeetCode OJ&gt; 100. Same Tree

    100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...

  5. Linux记录-shell 100例(转载)

    1.编写hello world脚本 #!/bin/bash # 编写hello world脚本 echo "Hello World!" 2.通过位置变量创建 Linux 系统账户及 ...

  6. LeetCode记录(1)——Array

    1.Two Sum naive 4.Median of Two Sorted Arrays 找两个已排序数组的中位数 直接数可以过,但很蠢,O(m+n)时间 class Solution { publ ...

  7. Leetcode 记录(201~300)

    实习面试前再完成100题,争取能匀速解释清楚题 204. Count Primes 素数筛 class Solution { public: int countPrimes(int n) { ) ; ...

  8. leetcode记录-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  9. LeetCode记录之13——Roman to Integer

    能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...

随机推荐

  1. 数据库和linux对大小写的区分

  2. 一 time与datetime模块

    时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是float类型. 格式化的时间字 ...

  3. Nancy 返回值详解

    简介 Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono.它的返回值也是多种多样的,适应各种不同的情况.包括Response.AsFile( ...

  4. AspNetCore MVC + Vue.Js 项目搭建

    1.准备 全文重点在于搭建环境,其他相关知识点请百度. VS2017 升级到最新的版本 安装 net core 2.0 安装 npm (npm相关使用请百度或咨询前端小伙伴) 全局安装 webpack ...

  5. javascript OOP实例—探测器

    <script> /*所有探测器都有探测的方法和分析的方法,分析当前的浏览器环境,不管是浏览器还是nodejs*/ /*container容器探测器*/ /*link链接探测器*/ /*外 ...

  6. 考虑实现一个不抛异常的swap

    Effective C++:参考自harttle land 类的swap实现与STL容器是一致的:提供swap成员函数, 并特化std::swap来调用那个成员函数. class Widget { p ...

  7. [BZOJ3011][Usaco2012 Dec]Running Away From the Barn

    题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...

  8. Adjoint operators $T_K$ and $T_{K^{*}}$ in BEM

    In our last article, we introduced four integral operators in the boundary integral equations in BEM ...

  9. spark操作Kudu之写 - 使用DataFrame API

    在通过DataFrame API编写时,目前只支持一种模式“append”.尚未实现的“覆盖”模式 import org.apache.kudu.spark.kudu._ import org.apa ...

  10. PHP使用Apache中的ab测试网站的压力性能及mpm介绍

    打开Apache安装的bin目录 shift+鼠标右键 复制粘贴以下代码->回车 ab -n 1000 -c 100 http://localhost/test.php 上例表示总共访问http ...