7.reverse interger
class Solution {
//beat 97.87%
//但是太繁琐了
public:
int reverse(int x) {
int flag,number;
if(x==-pow(2,31))
return 0;
if(x>=0)
flag=1;
if(x<0) {
flag=-1;
x=-x;
}
int num[10];
number=x;
int i=0;
while(number!=0){
num[i]=number%10;
//cout<2)
return 0;
if(i==10&&num[0]==2&&num[1]>1)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]>4)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]>7)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]>4)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]>8)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]==8&&num[6]>3)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]==8&&num[6]==3&&num[7]>6)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]==8&&num[6]==3&&num[7]==6&&num[8]>4)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]==8&&num[6]==3&&num[7]==6&&num[8]==4&&num[9]>7&&flag==1)
return 0;
if(i==10&&num[0]==2&&num[1]==1&&num[2]==4&&num[3]==7&&num[4]==4&&num[5]==8&&num[6]==3&&num[7]==6&&num[8]==4&&num[9]>8&&flag==-1)
return 0;
int result=0;
for(int j=0;j<=i-1;j++){
result=result*10+num[j];
}
if(flag==-1)
result*=-1;
if(flag==0)
result=0;
return result;
}
};
class Solution {
//beat 97.87%
//网上找到的写的较好的代码
public:
int reverse(int x) {
int temp;
int ans=0;
while(x){
temp=ans*10+x%10;
if(temp/10!=ans)//不相等的时候说明已经越界了。
return 0;
x=x/10;
ans=temp;
}
return ans;
}
};
9.palindrome number
class Solution {
//beat 50.93%
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int num[20],number;
number=x;
int i=0;
while(number!=0){
num[i]=number%10;
number/=10;
i++;
}
for(int j=0;j<=i-1;j++){
if(num[j]!=num[i-1-j])
return false;
}
return true;
}
};
13.Roman to Interger
class Solution {
//beat 91%,分情况分的很繁琐
public:
int romanToInt(string s) {
int slen;
slen=s.length();
int i=0;
int sum=0;
while(i<=slen-1){
if((s[i]=='I'&&s[i+1]!='V')&&(s[i]=='I'&&s[i+1]!='X')){
sum+=1;
i++;
}
else if((s[i]=='X'&&s[i+1]!='L')&&(s[i]=='X'&&s[i+1]!='C')){
sum+=10;
i++;
}
else if((s[i]=='C'&&s[i+1]!='D')&&(s[i]=='C'&&s[i+1]!='M')){
sum+=100;
i++;
}
else if(s[i]=='V'){
sum+=5;
i++;
}
else if(s[i]=='L'){
sum+=50;
i++;
}
else if(s[i]=='D'){
sum+=500;
i++;
}
else if(s[i]=='M'){
sum+=1000;
i++;
}
else if(s[i]=='I'&&s[i+1]=='V'){
sum+=4;
i+=2;
}
else if(s[i]=='I'&&s[i+1]=='X'){
sum+=9;
i+=2;
}
else if(s[i]=='X'&&s[i+1]=='L'){
sum+=40;
i+=2;
}
else if(s[i]=='X'&&s[i+1]=='C'){
sum+=90;
i+=2;
}
else if(s[i]=='C'&&s[i+1]=='D'){
sum+=400;
i+=2;
}
else if(s[i]=='C'&&s[i+1]=='M'){
sum+=900;
i+=2;
}
}
return sum;
}
};
class Solution {
//利用map,还有一个规律,如果前一位比后一位大,那就返回前一位的相反数
public:
int romanToInt(string s) {
unordered_map Dic={{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
int sum=Dic[s.back()];
for(int i=s.length()-2;i>=0;i--){
sum+=(Dic[s[i]]
14.Longest common prefix
class Solution {
//beat 98.72%,但是思路比较繁琐啊
public:
string longestCommonPrefix(vector& strs) {
int sizeofstrs=strs.size();
string empty="";
if(sizeofstrs==0)
return empty;
string prefix;
prefix=strs[0];
for(int i=1;istrs[i].size()){
prefix=strs[i];
}
}
return prefix;
}
};
//网上看到的解答,思路很清晰,以第一个字符串为比较单位,不符合就返回,符合就添加
//beat 98%
class Solution {
public:
string longestCommonPrefix(vector& strs) {
if(strs.size()==0)
return string ();
if(strs.size()==1)
return strs[0];
string output;
for(int i=0;i!=strs[0].length();i++){
for(int j=1;j!=strs.size();j++){
if(strs[j][i]!=strs[0][i])
return output;
}
output+=strs[0][i];
}
return output;
}
};
20.Valid Panrentheses
class Solution {
//beat 100%,但是分情况太繁琐啦
public:
bool isValid(string s) {
SqStack S;
S.base=(char*)malloc(1000*sizeof(char));
S.top=S.base;
S.size=1000;
if(s[0]==')'||s[0]=='}'||s[0]==']')//第一个就错了
return false;
int i=0;
while(s[i]==')'||s[i]=='}'||s[i]==']'||s[i]=='('||s[i]=='{'||s[i]=='['){
switch(s[i]){
case '(':
case '[':
case '{':
*S.top=s[i];
S.top++;
break;
case ')':
if(*(S.top-1)=='('){
--S.top;
}
else{
*S.top=s[i];
S.top++;
}
break;
case ']':
if(*(S.top-1)=='['){
--S.top;
}
else{
*S.top=s[i];
S.top++;
}
break;
case '}':
if(*(S.top-1)=='{'){
--S.top;
}
else{
*S.top=s[i];
S.top++;
break;
}
default:
break;
}
i++;
}
if(S.top==S.base)
return true;
else return false;
}
private:
typedef struct {
char *base;
char *top;
int size;
}SqStack;
};
class Solution {
//可以借助栈顶元素来判断
public:
bool isValid(string s) {
stack my_stack;
if(s.length()==0)
return true;
my_stack.push('#');
for(int i=0;i!=s.length();i++){
if((s[i]==']'&&my_stack.top()=='[')||(s[i]=='}'&&my_stack.top()=='{')||(s[i]==')'&&my_stack.top()=='(')){
my_stack.pop();
}
else{
my_stack.push(s[i]);
}
}
if(my_stack.top()=='#')
return true;
else return false;
}
};
21.Merge two sorted lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
//beat 34%,真的是又慢又繁琐了...
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *p,*q,*merge,*temp,*out;
p=l1,q=l2;
if(l1==NULL)
return l2;
if(l2==NULL)
return l1;
if(l1==NULL&&l2==NULL)
return NULL;
merge=(ListNode*)malloc(sizeof(ListNode));
if(p->valval){
merge->val=p->val;
temp=p;
p=p->next;
temp->next=NULL;
}
else{
merge->val=q->val;
temp=q;
q=q->next;
temp->next=NULL;
}
out=merge;
while(p!=NULL&&q!=NULL){
if(p->valval){
merge->next=p;
p=p->next;
merge=merge->next;
merge->next=NULL;
}
else{
merge->next=q;
q=q->next;
merge=merge->next;
merge->next=NULL;
}
}
if(p!=NULL){
merge->next=p;
}
if(q!=NULL){
merge->next=q;
}
return out;
}
};
class Solution {
//递归:每次返回值较小的那个结点,让它接在上个结点下面。
//beat 34%
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL) return l2;
if(l2==NULL) return l1;
if(l1->valval){
l1->next=mergeTwoLists(l1->next,l2);
return l1;
}
else{
l2->next=mergeTwoLists(l1,l2->next);
return l2;
}
}
};
26.remove duplicates from sorted array
class Solution {
//beat 45.83%
public:
int removeDuplicates(vector& nums) {
if(nums.size()==0)
return 0;
vector::iterator p=nums.begin();
vector::iterator q=p;
while(p!=nums.end()-1){
if(*p==*(p+1)){
nums.erase(p+1);
}
else{
p++;
}
}
return nums.size();
}
};
class Solution {
//题后大神的五行c++
//beat 99.17%
public:
int removeDuplicates(vector& nums) {
int i=0;
for(int n : nums)
if(!i||n>nums[i-1])
nums[i++]=n;
return i;
}
};
27.Remove element
class Solution {
//beat 98%
public:
int removeElement(vector& nums, int val) {
if(nums.size()==0)
return 0;
vector::iterator p=nums.begin();
while(p!=nums.end()){
if(*p==val){
nums.erase(p);
p=nums.begin();
}
else p++;
}
return nums.size();
}
};
28.Implement strstr()
class Solution {
//beat 98%
public:
int strStr(string haystack, string needle) {
int lh=haystack.length();
int ln=needle.length();
if(ln==0)
return 0;
if(lh
class Solution {
//kmp beat98%
public:
int strStr(string haystack, string needle) {
int lh=haystack.length();
int ln=needle.length();
if(needle=="")
return 0;
if(haystack=="")
return -1;
if(ln>lh)
return -1;
vector next=getnext(needle);
int i=0;
int j=0;
while(i!=lh&&j!=ln){
if(j==-1||haystack[i]==needle[j]){
i++;
j++;
}
else{
j=next[j];
}
}
if(j==ln)
return i-j;
else return -1;
}
private:
vector getnext(string needle){
int ln=needle.length();
vector tonext;
tonext.push_back(-1);
int k=-1;
int i=0;
while(i!=ln-1){
if(k==-1||needle[k]==needle[i]){
if(needle[++k]==needle[++i])
tonext.push_back(tonext[k]);
else
tonext.push_back(k);
}
else{
k=tonext[k];
}
}
return tonext;
}
};
35. Search insert position
class Solution {
public:
int searchInsert(vector& nums, int target) {
if(nums.size()==0)
return 0;
if(target<=nums[0])
return 0;
if(target>nums[nums.size()-1])
return nums.size();
int i;
for(i=0;i!=nums.size();i++){
if(nums[i]<=target&&target<=nums[i+1])
break;
}
return i+1;
}
};
class Solution {
//binary search beat 98%
public:
int searchInsert(vector& nums, int target) {
int low=0;
int high=nums.size()-1;
while(low<=high){
int mid=(low+high)/2;
if(target>nums[mid])
low=mid+1;
else
high=mid-1;
}
return low;
}
};
38.count and say
之前提交的时候一直报 index 100 out of bounds for type 'char' ,无论怎么改char[]的大小都没用,猜想可能是在将char[]转化为string的时候没有添加结束标志导致的,添加'\0'后就AC了。
class Solution {
//beat 100%
public:
string countAndSay(int n) {
string countnsay;
string temp;
countnsay="1";
for(int i=0;i!=n-1;i++){
temp=compute(countnsay);
countnsay=temp;
}
return countnsay;
}
private:
string compute(string eg){
int i = 0;
int j=0;
int outi = 0;
char out[100000];
int count;
while (i != eg.size()) {
while (eg[i] == eg[j]) {
j++;
}
//cout<<"j "<
53.Maximum subarray
class Solution {
//beat 4%
public:
int maxSubArray(vector& nums) {
if(nums.size()==1)
return nums[0];
int max=nums[0];
int sum=0;
for(int i=0;i!=nums.size();i++){
sum=0;
for(int j=i;j!=nums.size();j++){
sum=sum+nums[j];
if(sum>max)
max=sum;
}
}
return max;
}
};
class Solution {
//dp, sum表示累积到第i项的和
public:
int maxSubArray(vector& nums) {
int length=nums.size();
int sum=nums[0];
int maxsum=nums[0];
for(int i=1;i!=length;i++){
sum=(sum>0?sum:0)+nums[i];
if(sum>maxsum)
maxsum=sum;
}
return maxsum;
}
};
58.Length of Last Word
class Solution {
//beat 64%
public:
int lengthOfLastWord(string s) {
if(s.length()==0)
return 0;
int len=s.length();
int i=len-1;
int count=0;
while(s[i]==' ')
i--;
if(i==-1)
return 0;
while(s[i]!=' '&&i>=0){
i--;
count++;
}
return count;
}
};
class Solution {
//移到最后一个不是空格的地方,再往前走到空格
//beat 64%
public:
int lengthOfLastWord(string s) {
int last=s.length()-1;
int count=0;
while(last>=0&&s[last]==' ') last--;
while(last>=0&&s[last]!=' '){
last--;
count++;
}
return count;
}
};
66.Plus one
class Solution {
//思路:存到数组里(好像多此一举了)
//beat 100%
public:
vector plusOne(vector& digits) {
int num[1000];
int i;
vector out;
for(i=0;i!=digits.size();i++){
num[i]=digits[i];
}
num[digits.size()-1]+=1;
for(i=digits.size()-1;i>=1;i--){
if(num[i]>=10){
num[i]=num[i]%10;
num[i-1]+=1;
}
}
if(num[0]!=10){
i=0;
while(i!=digits.size()){
out.push_back(num[i]);
i++;
}
return out;
}
else{
out.push_back(1);
out.push_back(0);
i=1;
while(i!=digits.size()){
out.push_back(num[i]);
i++;
}
return out;
}
}
};
class Solution {
//直接对vector操作就可以了
public:
vector plusOne(vector& digits) {
int len=digits.size();
for(int i=len-1;i>=0;i--){
if(digits[i]==9){
digits[i]=0;
}
else{
digits[i]++;
return digits;
}
}
digits[0]=1;
digits.push_back(0);
return digits;
}
};
class Solution {
//discuss里的回答,进制运算可以考虑取余
//beat 100%
public:
string addBinary(string a, string b) {
int la=a.length()-1;
int lb=b.length()-1;
int c=0;
string s="";
while(la>=0||lb>=0||c==1){
c=c+(la>=0?a[la--]-'0':0);
c=c+(lb>=0?b[lb--]-'0':0);
s=char(c%2+'0')+s;//如果是s+char(c%2+'0')的话,就是反的了
c/=2;
}
return s;
}
};
69.sqrt(x)
class Solution {
//discuss里的牛顿迭代法
//beat 98.3%
public:
int mySqrt(int x) {
long r=x;
while(r*r>x){
r=(r+x/r)/2;
}
return r;
}
};
class Solution {
//binary search
//beat 58%
public:
int mySqrt(int x) {
int left=1;
int right=x;
if(x==0)
return 0;
while(true){
int mid=(left+right)/2;
if(mid>x/mid){
right=mid-1;
}
else{
if((mid+1)>x/(mid+1))
return mid;
else{
left=mid+1;
}
}
}
}
};
70.Climbing Stairs
class Solution {
//beat 100%
public:
int climbStairs(int n) {
int dp[10000];
memset(dp,0,sizeof(dp));
dp[1]=1;
dp[2]=2;
for(int i=3;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}
};
83.Remove Duplicates from Sorted List
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
//beat 36%
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* p;
ListNode* q;
p=head,q=head;
if(head==NULL)
return NULL;
while(p->next!=NULL){
if(p->val==p->next->val){
q=p->next;
if(q->next!=NULL){
p->next=q->next;
q->next=NULL;
q=p;
}
else{
p->next=NULL;
}
}
else{
p=p->next;
}
}
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
//不是很懂怎么释放内存
//beat 98%
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL)
return NULL;
ListNode* cur=head;
ListNode* del;
while(cur->next!=NULL){
if(cur->val==cur->next->val){
del=cur->next;
cur->next=cur->next->next;
del->next=NULL;
}
else cur=cur->next;
}
return head;
}
};
88.Merge Sorted array
class Solution {
//beat 99.46%
public:
void merge(vector& nums1, int m, vector& nums2, int n) {
if(nums1.size()==0){
for(int i=0;i!=nums2.size();i++){
nums1.push_back(nums2[i]);
}
}
else{
int k=0;
for(int i=m;i!=m+n;i++){
nums1[i]=nums2[k];
k++;
}
int temp;
for(int i=0;i!=nums1.size()-1;i++){
for(int j=0;j!=nums1.size()-1-i;j++){
if(nums1[j]>nums1[j+1]){
temp=nums1[j];
nums1[j]=nums1[j+1];
nums1[j+1]=temp;
}
}
}
}
}
};
class Solution {
//beat 98%
public:
void merge(vector& nums1, int m, vector& nums2, int n) {
int i=m-1;
int j=n-1;
int k=m+n-1;
while(i>=0&&j>=0){
if(nums1[i]>nums2[j])
nums1[k--]=nums1[i--];
else nums1[k--]=nums2[j--];
}
while(j>=0){
nums1[k--]=nums2[j--];
}
}
};
100. Same Tree
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
//beat 100%
int flag=1;
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
compare(p,q);
if(flag==1)
return true;
else return false;
}
private:
void compare(TreeNode* p,TreeNode* q){
if(p!=NULL&&q!=NULL){
if(p->val==q->val){
compare(p->left,q->left);
compare(p->right,q->right);
}
else{
flag=0;
return;
}
}
else if(p==NULL&&q==NULL){
return;
}
else if((p!=NULL&&q==NULL)||(p==NULL&&q!=NULL)){
flag=0;
return;
}
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int flag=1;
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
//原来还可以这么return...
if(p==NULL||q==NULL) return(p==q);
return (p->val==q->val&&isSameTree(p->left,q->left)&&isSameTree(p->right,q->right));
}
};
101.Symmetric Tree
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
//beat 98%
int flag=1;
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true;
else if(root->left==NULL&&root->right==NULL)
return true;
else if((root->left==NULL&&root->right!=NULL)||(root->left!=NULL&&root->right==NULL))
return false;
else if(root->left!=NULL&&root->right!=NULL){
checksides(root->left,root->right);
}
if(flag==1)
return true;
else return false;
}
private:
void checksides(TreeNode* left,TreeNode* right){
if(left!=NULL&&right!=NULL){
if(left->val==right->val){
checksides(left->left,right->right);
checksides(left->right,right->left);
}
else{
flag=0;
return;
}
}
else if(left==NULL&&right==NULL){
return;
}
else if((left==NULL&&right!=NULL)||(left!=NULL&&right==NULL))
{
flag=0;
return;
}
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true;
return judge(root->left,root->right);
}
bool judge(TreeNode* left,TreeNode* right){
if(left==NULL&&right==NULL)
return true;
else if(left==NULL||right==NULL)
return false;
if(left->val!=right->val)
return false;
return judge(left->left,right->right)&&judge(left->right,right->left);
}
};
104.Maximum depth of binary tree
class Solution {
//beat 99%
public:
int maxDepth(TreeNode* root) {
if(root==NULL)
return 0;
else{
return check(root);
}
}
private:
int check(TreeNode *node){
if(node==NULL)
return 0;
else{
int left;
int right;
left=1+check(node->left);
right=1+check(node->right);
return max(left,right);
}
}
};
class Solution {
//深度是靠加1加出来的
public:
int maxDepth(TreeNode* root) {
if(root==NULL)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
107. Binary Tree Level Order Traversal II
class Solution {
//beat 98%
vector> out;
public:
vector> levelOrderBottom(TreeNode* root) {
if(root==NULL){
return out;
}
else{
gothrough(root,1);
vector> reverse;
while(out.size()!=0){
reverse.push_back(out[out.size()-1]);
out.pop_back();
}
return reverse;
}
}
private:
void gothrough(TreeNode* node,int level){
if(node!=NULL){
if(level>out.size()){
vector temp;
out.push_back(temp);
out[level-1].push_back(node->val);
}
else{
out[level-1].push_back(node->val);
}
gothrough(node->left,level+1);
gothrough(node->right,level+1);
}
else return;
}
};
class Solution {
public:
vector> out;
vector> reverse;
vector> levelOrderBottom(TreeNode* root) {
if(root==NULL)
return out;
dfs(root,0);
while(out.size()!=0){
reverse.push_back(out[out.size()-1]);
out.pop_back();
}
return reverse;
}
void dfs(TreeNode* node,int level){
if(node==NULL)
return;
if(level==out.size()){
vector temp;
out.push_back(temp);
}
out[level].push_back(node->val);
dfs(node->left,level+1);
dfs(node->right,level+1);
}
};
108.convert sorted array to bst
class Solution {
//beat 40%
public:
using iter=vector::iterator;
TreeNode* sortedArrayToBST(vector& nums) {
if(nums.size()==0)
return nullptr;
return buildTree(nums.begin(),nums.end());
}
TreeNode* buildTree(iter p,iter q){
if(p>=q)
return nullptr;
iter mid;
mid=p+(q-p)/2;
TreeNode* node=new TreeNode(*mid);
node->left=buildTree(p,mid);
node->right=buildTree(mid+1,q);
return node;
}
};
110.balanced binary tree
class Solution {
//beat 60%
int flag=1;
public:
bool isBalanced(TreeNode* root) {
if(root==NULL)
return true;
else{
judge(root);
if(flag==1)
return true;
else return false;
}
}
private:
void judge(TreeNode* node){
if(node!=NULL){
if(node->left!=NULL&&node->right!=NULL){
if(abs(geth(node->left)-geth(node->right))<=1){
judge(node->left);
judge(node->right);
}
else{
flag=0;
return;
}
}
else if((node->left==NULL&&node->right!=NULL)||node->right==NULL&&node->left!=NULL){
if(geth(node)>2){
flag=0;
return;
}
}
else{
return;
}
}
}
int geth(TreeNode* node){
if(node==NULL){
return 0;
}
else{
int lh,rh;
lh=geth(node->left);
rh=geth(node->right);
return max(lh,rh)+1;
}
}
};
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==NULL)
return true;
int depth=0;
return dfs(root,depth);
}
bool dfs(TreeNode* node,int& depth){
if(node==NULL){
depth=0;
return true;
}
int ldepth=0;
int rdepth=0;
bool leftside=dfs(node->left,ldepth);
bool rightside=dfs(node->right,rdepth);
depth=max(ldepth,rdepth)+1;
return leftside&&rightside&&abs(ldepth-rdepth)<2;
}
};
111. Minimum depth of binary tree
class Solution {
//beat 41%
public:
int minDepth(TreeNode* root) {
if(root==NULL)
return 0;
else{
return find(root);
}
}
private:
int find(TreeNode* node){
if(node==NULL)
return 0;
else{
if(node->left!=NULL&&node->right!=NULL){
int lh,rh;
lh=find(node->left);
rh=find(node->right);
return min(lh,rh)+1;
}
else if(node->left!=NULL&&node->right==NULL){
int h=find(node->left);
return h+1;
}
else if(node->left==NULL&&node->right!=NULL){
int h=find(node->right);
return h+1;
}
else if(node->left==NULL&&node->right==NULL){
return 1;
}
}
}
};
class Solution {
beat 100%
public:
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
if(root->left==NULL) return 1+minDepth(root->right);
if(root->right==NULL) return 1+minDepth(root->left);
return 1+min(minDepth(root->left),minDepth(root->right));
}
};
112.path sum
class Solution {
//beat 100%
int flag=0;
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root==NULL){
return false;
}
else{
check(root,sum);
if(flag==1)
return true;
else return false;
}
}
private:
void check(TreeNode* node,int csum){
if(node==NULL){
return;
}
else{
if(node->left!=NULL){
check(node->left,csum-node->val);
}
if(node->right!=NULL){
check(node->right,csum-node->val);
}
if(node->left==NULL&&node->right==NULL){
if(node->val==csum)
flag=1;
return;
}
}
}
};
class Solution {
//beat 42%其实是个简化版本
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root==NULL)
return false;
if(root->left==NULL&&root->right==NULL&&root->val==sum) return true;
return (hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val));
}
};
118.pascal's triangle
class Solution {
//beat 100%
public:
vector> generate(int numRows) {
vector> big;
if(numRows==0)
return big;
for(int i=0;i!=numRows;i++){
vector temp;
big.push_back(temp);
}
big[0].push_back(1);
if(numRows==1)
return big;
big[1].push_back(1);
big[1].push_back(1);
if(numRows==2)
return big;
for(int i=2;i!=numRows;i++){
big[i].push_back(1);
for(int j=1;j<=i-1;j++){
big[i].push_back(big[i-1][j-1]+big[i-1][j]);
}
big[i].push_back(1);
}
return big;
}
};
class Solution {
public:
vector> generate(int numRows) {
vector> r(numRows);
for(int i=0;i
119.pascal's triangle II
class Solution {
//beat 100%
vector therow;
public:
vector getRow(int rowIndex) {
vector> big;
for(int i=0;i<=rowIndex;i++){
vector temp;
big.push_back(temp);
}
big[0].push_back(1);
if(rowIndex==0)
return big[0];
big[1].push_back(1);
big[1].push_back(1);
if(rowIndex==1)
return big[1];
for(int i=2;i<=rowIndex;i++){
big[i].push_back(1);
for(int j=1;j<=i-1;j++){
big[i].push_back(big[i-1][j-1]+big[i-1][j]);
}
big[i].push_back(1);
}
return big[rowIndex];
}
};
class Solution {
public:
vector getRow(int rowIndex) {
vector out(rowIndex+1,0);
out[0]=1;
for(int i=0;i!=rowIndex;i++){
//控制更新次数,第i次要更新i次
for(int j=i+1;j>=1;--j){
out[j]+=out[j-1];
}
}
return out;
}
};
121.best time to buy and sell stock
class Solution {
//beat 4%
public:
int maxProfit(vector& prices) {
int max=0;
if(prices.size()==0||prices.size()==1)
return 0;
else{
for(int i=0;i!=prices.size();i++){
for(int j=i+1;j!=prices.size();j++){
if(prices[j]-prices[i]>max)
max=prices[j]-prices[i];
}
}
return max;
}
}
};
class Solution {
beat99% kandane's algorithm
public:
int maxProfit(vector& prices) {
if(prices.size()==0)
return 0;
int max_so_far=0;
int max_end_here=0;
for(int i=1;i!=prices.size();i++){
max_end_here=max(0,max_end_here+=prices[i]-prices[i-1]);
max_so_far=max(max_so_far,max_end_here);
}
return max_so_far;
}
};
122.best time to buy and sell stock II
class Solution {
beat 99%
public:
int maxProfit(vector& prices) {
if(prices.size()==0)
return 0;
int openprice=prices[0];
int closeprice=prices[0];
int profit=0;
for(int i=1;i!=prices.size();i++){
if(closeprice
125.valid palindrome
class Solution {
//beat 60%
public:
bool isPalindrome(string s) {
if(s=="")
return true;
else{
int i=0;
int j=s.length()-1;
while(i='a')||(s[i]>='A'&&s[i]<='Z')||(s[i]<='9'&&s[i]>='0'))&&((s[j]<='z'&&s[j]>='a')||(s[j]>='A'&&s[j]<='Z')||(s[j]<='9'&&s[j]>='0'))){
if(s[i]<='Z'&&s[i]>='A') s[i]=s[i]-'A'+'a';
if(s[j]<='Z'&&s[j]>='A') s[j]=s[j]-'A'+'a';
if(s[i]!=s[j]) return false;
else{
i++;
j--;
}
}
else if(!((s[i]<='z'&&s[i]>='a')||(s[i]>='A'&&s[i]<='Z')||(s[i]<='9'&&s[i]>='0'))) i++;
else if(!((s[j]<='z'&&s[j]>='a')||(s[j]>='A'&&s[j]<='Z')||(s[j]<='9'&&s[j]>='0'))) j--;
}
return true;
}
}
};
class Solution {
//beat 60%
public:
bool isPalindrome(string s) {
if(s.length()==0)
return true;
for(int i=0,j=s.length()-1;i
136.single number
class Solution {
//beat 98%
public:
int singleNumber(vector& nums) {
int result=nums[0];
for(int i=1;i!=nums.size();i++){
result=result^nums[i];
}
return result;
}
};
141.linked list cycle
class Solution {
//beat 100%
public:
bool hasCycle(ListNode *head) {
if(head==NULL)
return false;
ListNode* p;
ListNode* q;
p=head;
q=head;
if(p->next==NULL)
return false;
if(p->next->next==NULL)
return false;
while(q->next!=NULL){
if(q->next->next!=NULL)
q=q->next->next;
else{
q=q->next;
}
p=p->next;
if(p==q)
return true;
}
return false;
}
};
155. min stack
class MinStack {
//beat 60%
private:
stack total;
stack min;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
total.push(x);
if(min.empty()||x<=getMin())
min.push(x);
}
void pop() {
if(total.top()==getMin())
min.pop();
total.pop();
}
int top() {
return total.top();
}
int getMin() {
return min.top();
}
};
160.intersection of two linked list
class Solution {
//beat 87%
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA==NULL||headB==NULL)
return NULL;
if(!ifintersection(headA,headB))
return NULL;
int lenA=getlength(headA);
int lenB=getlength(headB);
ListNode* p=headA;
ListNode* q=headB;
if(lenA>lenB){
for(int i=0;i!=lenA-lenB;i++){
p=p->next;
}
}
else{
for(int i=0;i!=lenB-lenA;i++){
q=q->next;
}
}
while(p!=q){
p=p->next;
q=q->next;
}
return p;
}
private:
bool ifintersection(ListNode *headA, ListNode *headB){
ListNode *p;
ListNode *q;
p=headA;
q=headB;
while(p!=NULL)
p=p->next;
while(q!=NULL)
q=q->next;
if(p==q)
return true;
else return false;
}
int getlength(ListNode* head){
int len=1;
ListNode* p;
p=head;
while(p->next!=NULL){
p=p->next;
len++;
}
return len;
}
};
class Solution {
//beat 87%
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p=headA;
ListNode *q=headB;
if(p==NULL||q==NULL)
return nullptr;
while(p!=q){
p=p->next;
q=q->next;
if(p==q)
return p;
if(p==NULL) p=headB;
if(q==NULL) q=headA;
}
return p;
}
};
167.two sum II -input array is sorted
class Solution {
//beat 2%
public:
vector twoSum(vector& numbers, int target) {
vector twoindex;
int temp;
for(int i=0;i!=numbers.size();i++){
twoindex.push_back(i+1);
temp=target-numbers[i];
for(int j=i+1;j!=numbers.size();j++){
if(numbers[j]==temp){
twoindex.push_back(j+1);
return twoindex;
}
}
twoindex.pop_back();
}
}
};
class Solution {
//beat 98%
public:
vector twoSum(vector& numbers, int target) {
int low=0;
int high=numbers.size()-1;
while(numbers[low]+numbers[high]!=target){
if(numbers[low]+numbers[high]>target)
high--;
else
low++;
}
return vector{low+1,high+1};
}
};
168.excel sheet column title
class Solution {
//beat 100%
public:
string convertToTitle(int n) {
string res;
char temp;
while(n){
n-=1;
temp='A'+n%26;
res=temp+res;
n/=26;
}
return res;
}
};
169.majoriry element
class Solution {
//beat 97%
struct node{
int val;
int seq;
};
public:
int majorityElement(vector& nums) {
vector allnum;
node p;
p.val=nums[0];
p.seq=1;
allnum.push_back(p);
for(int i=1;i!=nums.size();i++){
int flag=0;
for(int j=0;j!=allnum.size();j++){
if(nums[i]==allnum[j].val){
allnum[j].seq++;
flag=1;
}
}
if(flag==0){
p.val=nums[i];
p.seq=1;
allnum.push_back(p);
}
}
int maxseq=0;
int seqence=0;
for(int i=0;i!=allnum.size();i++){
if(allnum[i].seq>maxseq){
maxseq=allnum[i].seq;
seqence=i;
}
}
return allnum[seqence].val;
}
};
class Solution {
//beat 41%
public:
int majorityElement(vector& nums) {
unordered_map counts;
for(int i=0;i!=nums.size();i++){
counts[nums[i]]++;
if(counts[nums[i]]>(nums.size()/2))
return nums[i];
}
}
};
171.excel sheet column number
class Solution {
//beat 98%
public:
int titleToNumber(string s) {
int sum=0;
int temp=1;
for(int i=0;i!=s.length();i++){
temp=(s[i]-'A'+1);
for(int j=0;j!=s.length()-i-1;j++){
temp*=26;
}
sum+=temp;
}
return sum;
}
};
172.factorial trailing zeroes
class Solution {
//beat 62.95%
public:
int trailingZeroes(int n) {
int num_0=0;
int temp=0;
while(n/5>0){
temp=n/5;
num_0+=temp;
n=temp;
}
return num_0;
}
};
189.rotate array
class Solution {
//beat 17%
public:
void rotate(vector& nums, int k) {
vector::iterator p;
vector::iterator q;
p=nums.begin();
q=nums.end()-1;
for(int i=0;i!=k;i++){
nums.insert(p,*q);
nums.pop_back();
p=nums.begin();
q=nums.end()-1;
}
}
};
class Solution {
//beat 40%
public:
void rotate(vector& nums, int k) {
vector nums_copy=nums;
for(int i=0;i!=nums.size();i++){
nums[(i+k)%nums.size()]=nums_copy[i];
}
}
};