Leetcode 记录(1~100)
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)的更多相关文章
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- Leetcode 记录(101~200)
Now, I want to just use English to explain the problem, it's about two month before the interview, s ...
- 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; ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- Linux记录-shell 100例(转载)
1.编写hello world脚本 #!/bin/bash # 编写hello world脚本 echo "Hello World!" 2.通过位置变量创建 Linux 系统账户及 ...
- LeetCode记录(1)——Array
1.Two Sum naive 4.Median of Two Sorted Arrays 找两个已排序数组的中位数 直接数可以过,但很蠢,O(m+n)时间 class Solution { publ ...
- Leetcode 记录(201~300)
实习面试前再完成100题,争取能匀速解释清楚题 204. Count Primes 素数筛 class Solution { public: int countPrimes(int n) { ) ; ...
- leetcode记录-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
随机推荐
- 20165323《Java程序设计》第九周学习总结
一.教材内容学习总结 URL类 1.URL 类是 java.net 包中的一个重要的类,使用 URL 创建对象的应用程序称为客户端程序. 2.一个 URL 对象通常包含最基本的三部分信息:协议.地址和 ...
- 一个页面中使用多个UEditor
如何在一个页面中使用多个Ueditor: 引入这些js: <script src="~/Scripts/ueditor/ueditor.config.js"></ ...
- js中匿名函数和回调函数
匿名函数: 通过这种方式定义的函数:(没有名字的函数) 作用:当它不被赋值给变量单独使用的时候 1.将匿名函数作为参数传递给其他函数 2.定义某个匿名函数来执行某些一次性任务 var f = func ...
- 一脸懵逼学习Hadoop中的序列化机制——流量求和统计MapReduce的程序开发案例——流量求和统计排序
一:序列化概念 序列化(Serialization)是指把结构化对象转化为字节流.反序列化(Deserialization)是序列化的逆过程.即把字节流转回结构化对象.Java序列化(java.io. ...
- url、querystring模块获取请求request.url中的不同部分图解
url.parse(string).query | url.parse(string).pathname | | | | | ------ ------------------- http://loc ...
- Python_自定义模块
自定义模块例子(web简单框架): 专门处理逻辑的包:处理各种访问需求 数据库的交互:面临各种的查询,删改 ,dba, 配置文件(全局配置文件):列存储数据的地方,HTML代码存储地方 实现: 代码: ...
- Flink--Split和select
Split就是将一个DataStream分成两个或者多个DataStream Select就是获取分流后对应的数据 val env = StreamExecutionEnvironment.getEx ...
- Ubuntu16.04中nginx除80之外其他端口不能访问
不废话, 大多数都以为是ufw防火墙的问题. 但我的是因iptables防火墙, 坑死我了. 查了好多也没查到怎么在Ubuntu关闭iptables, 索性直接卸载 apt-get remove ip ...
- Codechef EDGEST 树套树 树状数组 线段树 LCA 卡常
原文链接http://www.cnblogs.com/zhouzhendong/p/9016579.html 题目传送门 - Codechef EDGEST 题意 给定相同点集上的两棵生成树$T_1$ ...
- PAT (Basic Level) Practise - 换个格式输出整数
题目链接:https://www.patest.cn/contests/pat-b-practise/1006 1006. 换个格式输出整数 (15) 时间限制 400 ms 内存限制 65536 k ...