code

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        int temp;
        while(stack2.empty()){
              while(!stack1.empty()){
                  temp = stack1.top();
                  stack1.pop();
                  stack2.push(temp);
              }
        }
        temp = stack2.top();
        stack2.pop();
        return temp;
       
        
    }

private:
    stack<int> stack1;
    stack<int> stack2;
};
 
 
 
void sortage(int ages[],int length){
 
    if (ages==NULL || length <0){
        return;
    }
    int oldest = 99; 
    int array[oldest+1];
    for (int i =0; i<oldest;i++){
        array[i] = 0;
    }
    for (int j = 0; j<length; j++){
        int temp_age = ages[j];
        if(temp_age<0 ||temp_age>99){
            cout<<"wrong ages"<<endl;
        }
        array[temp_age] ++;
 
    }
 
    int index;
    for (int i = 0; i<oldest; i++){
        for (int j = 0; j<numbers; j++){
             ages[index] = i;
             index ++;
 
        }
    }
 
}



class Solution { //在这里采用的是自底而上的方法
public:
    int Fibonacci(int n) {
        int array[2] = {0,1};
        if (n<2){
            return array[n];
        }
        else{
             long long one =0;
             long long two =1;
             long long sum =0;
             for (long long i = 2;i<=n;i++){
                sum = one + two;
                one = two;
                two = sum;
             }
            return sum;
        }
       
        
    
    }

————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ttomchy/article/details/104595222


class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.empty()){
            return 0;
        }
        int index1 = 0;
        int index2 = rotateArray.size()-1;
        int midindex = index1;
        while (rotateArray[index1] > rotateArray[index2]){
            midindex = (index1 + index2 ) /2;
            if (index2 - index1 ==1){
                 midindex = index2;
                break;
            }
            else if (rotateArray[index1] == rotateArray[index2] && 
                     rotateArray[index1] == rotateArray[midindex]){
                return minorder(rotateArray,index1,index2);
            }
            else if (rotateArray[midindex] > rotateArray[index2]){
               index1 = midindex ;
            }
            else if (rotateArray[midindex] <= rotateArray[index2]){
               index2 = midindex ;
            }             
        }
        return rotateArray[midindex];
        
    }
    int minorder(vector<int> rotateArray,int index1,int index2){
         int temp = rotateArray[index1];
         for (int i =index1+1; i<=index2;i++){
             if (rotateArray[i]<temp){
                 temp = rotateArray[i];
             }
         }
        return temp;
        
        
      }
};
————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ttomchy/article/details/104594220

class Solution {
public:
    int jumpFloor(int number) {
    int array[2] = {0,1};
        if (number<2){
            return array[number];
        }
        else{
             long long one =1;
             long long two =1;
             long long sum =0;
             for (long long i = 2;i<=number;i++){
                sum = one + two;
                one = two;
                two = sum;
             }
            return sum;
        }
       
        
    
    }
};
————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ttomchy/article/details/104595941


class Solution {
public:
     int  NumberOf1(int n) {
         int count = 0;
         while(n){
                 count ++;
                 n = (n-1) &n;
             }
         return count;
     }
};
————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  剑指 offer 计算连表倒数第 k 个节点


/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        long  count =0;
        ListNode* node = pListHead;
        while(node != NULL){
            node = node->next;
            count ++;
        }
 
        if (pListHead ==NULL || k ==0 || k> count){
            return NULL;
        }
        
        ListNode* first = pListHead;
        ListNode* second = pListHead;
        for (int i = k-1; i>0;i--){
            if (first ->next!= NULL){
                first = first->next;
            }
        }
        
        while (first ->next!= NULL){
            first = first->next;
            second = second->next;
        }
            
        return second;
         
    }
};

剑指 offer 反转链表


/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if (pHead ==NULL){
           return NULL;
        }
        ListNode *pre = NULL;
        ListNode *cur = pHead;
        ListNode *temp = NULL;
        while (cur != NULL){
            temp = cur->next;//在这里暂存一下下一个指针
            cur->next = pre;//把当前的值指向前一个值
            pre = cur;//前一个值向后走一步
            cur = temp;// 把下一个值付给当前的值
        }
        return pre;
        
    }
};

剑指 offer 链表合并



/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if (pHead1 ==NULL){
            return pHead2;
        }
        if (pHead2 ==NULL){
            return pHead1;
        }
        ListNode * result;
        if (pHead1->val < pHead2->val){
            result = pHead1;
            result->next =  Merge(pHead1->next,pHead2);
         
        }
        else{
            result = pHead2;
            result->next =  Merge(pHead1,pHead2->next);
        }
        return result;
        
    }
};





剑指 offer 判断一棵树是否包含另一棵树

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {  
       if (pRoot1 == NULL || pRoot2 ==NULL){
        return false;
        }
        bool result= false;
        if (pRoot1->val ==pRoot2->val){
            result = treeonecntainstree2(pRoot1,pRoot2);
        }
        if (!result){
            result = HasSubtree(pRoot1->left,pRoot2);
        }
        if (!result){
           result = HasSubtree(pRoot1->right,pRoot2);
        }
        return result;
        
    }
    
    bool treeonecntainstree2(TreeNode* pRoot1, TreeNode* pRoot2){
        if (pRoot2==NULL){
            return true;
        }
        if (pRoot1 ==NULL){
            return false;
        }
        if (pRoot1->val != pRoot2->val){
            return false;
        }
        
        return treeonecntainstree2(pRoot1->left,pRoot2->left) && treeonecntainstree2(pRoot1->right,pRoot2->right) ;}
};

剑指 offer 树的镜像


/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/


class Solution {
public:

    void Mirror(TreeNode *pRoot) {
        if (pRoot ==NULL){
            return;
        }
       
        TreeNode *temp ;
        temp =  pRoot->left;
        pRoot->left =  pRoot->right;
        pRoot->right = temp;
       
        if(pRoot->left!=NULL){
            Mirror(pRoot->left);
        }
       
        if (pRoot->right!=NULL){
            Mirror(pRoot->right);
        }
    }

};


剑指 offer 计算栈中最小的数据


class Solution {//在这里利用一个辅助栈来解决问题
public:
    void push(int value) {
        datastack.push(value);
        if (minstack.empty()){
            minstack.push(value);
        }
        if (value <= minstack.top()){
            minstack.push(value);
        }
        
    }
    void pop() {
        if (datastack.empty()){
            return;
        }
        if (minstack.top() == datastack.top() ){
            minstack.pop();
        }
        datastack.pop();
        
    }
    int top() {
        return datastack.top();
    }
    int min() {
        return minstack.top();
    }
 private:
    stack<int> datastack;
    stack<int> minstack;
 
 
};

剑指 offer 从上到下打印二叉树


  vector<TreeNode*> data;
  vector<int> result;
  data.push_back(root);
  if (root == nullptr) return result;
  while (!data.empty()){
      TreeNode *temp = data[0];
      result.push_back(temp->val);
      data.erase(data.begin());
      if(temp->left){
          data.push_back(temp->left);
      }
      if(temp->right){
          data.push_back(temp->right);
      }
  }
return result;

剑指 offer 从上到下打印二叉树 (二)

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
    
      vector<int> nodevalue;
      deque<TreeNode*> dequenode;
      if (root ==nullptr) {
          return nodevalue;
      }
      dequenode.push_back(root);
      while (dequenode.size()){
          TreeNode* temp_node = dequenode.front();
          nodevalue.push_back(temp_node->val);
          dequenode.pop_front();
          
          if(temp_node->left !=NULL){
              dequenode.push_back(temp_node->left);
          }
          if(temp_node->right!=NULL){
              dequenode.push_back(temp_node->right);
          }
      }
        return nodevalue;
        
    }
};


树的遍历


void preordersearch(Tree * t){
    if(t == NULL){
        return;
    }
    cout<<t->val<<endl;//先序遍历,根左右
    preordersearch(t->left);
    preordersearch(t->right);
}
 
void inorder(Tree *t){ //中序遍历,左中右
    if(t==NULL){
        return;
    }
     preordersearch(t->left);
     cout<< t->val<<endl;
     preordersearch(t->right);
}
 
void tailorder(Tree *t){//后序遍历,左右中
    if (t== NULL){
        return ;
    }
    tailorder(t->left);
    tailorder(t->right);
    cout<<t->val<<endl;
}

剑指 offer 判断一棵树是不是后序遍历

class Solution {
public:

    bool VerifySquenceOfBST(vector<int> sequence) {
        return bst(sequence, 0, sequence.size() - 1);
    }
    
private:

    bool bst(vector<int> seq, int begin, int end){
        // 边界条件
        if(seq.empty() || begin > end)
            return false;
 
        // 划分左右子树,并判断左右子树和根节点的关系
        int i = begin;
        for(; i < end; ++i)
            if(seq[i] > seq[end])
                break;
 
        int j = i;
        for(; j < end; ++j)
            if(seq[j] < seq[end])
                return false;
 
        // 判断左子树是不是二叉搜索树
        bool left = true;
        if(i > begin)
            left = bst(seq, begin, i - 1);
 
        // 判断右子树是不是二叉搜索树
        bool right = true;
        if(i < end - 1)
            right = bst(seq, i , end - 1);
 
        return left && right;
    }
};

c++ 归并排序

 
 
void merge(int array,int L;int mid; int R){
   int p1 = L,p2 = mid+1, i=0;
   int temp[L+R-1];
 
   while (p1<mid && p2<R){
 
       temp[i++] = array[p1] <  array[p2] ? array[p1++] : array[p2++];
 
   }
   while (p1<mid){
      temp[i++] =  array[p1++];
   }
   
     while (p2<R){
      temp[i++] =  array[p2++];
   }
 
   for (int i =0; i< R+L-1;i++){
       array[L+i] = temp[i];
   }
 
}
 
void sort(int array[], int L,int R){
 
   if (L<R){
        int mid = (L+R)/2;
        sort(array,L,mid);
        sort(array,mid+1,R);
        merge (array,L,mid,R);
   }
    
}


 
int getlength(ListNode* listnode){
    int count =0;
    ListNode* temp = listnode;
    while(temp!=NULL){
 
        count ++;
        temp = temp->next;
    }
}



求两个单链表的共同节点

Listnode * getcommon(ListNode* list1, ListNode* list2){
 
    int length1 = getlength(list1);
    int length2 = getlength(list2);
 
    if(length1>length2){
        ListNode* longlist = list1;
        ListNode* shortlist = list2;
        int diff = length1 - length2;
    } else{
        ListNode* longlist = list2;
        ListNode* shortlist = list1;
        int diff = length2 - length1;
    }
 
    for (int i=0;i<diff;i++){
        longlist = longlist->next;
    }
 
    while (longlist->next!= NULL && shortlist!= NULL && (longlist != shortlist)){
        longlist = longlist->next;
        shortlist = shortlist->next;
 
    }
    return longlist;
 
 
}


计算树的深度

int treedepth(binarytree *tree){
 
    if(tree==NULL){
        return  0;
    }
    int left = treedepth(tree->left);
    int right = treedepth(tree->right);
    return (left> right) ? left++: right++;
}


快速排序算法的递归与非递归


//quick sort
 
void quicksort (int array[], int low, int high){
    if(low<high){
        int index = getindex(array,low,high);
        quicksort(array,index+1,high);
        quicksort(array,0,index);
    }
}
 
int getindex(int array[],int low, int high){
    int temp = array[low];
    while (low<high){
        if(array[high] >temp ){
            high --;
        }
        array[low] = array[high];
        if(array[low]<temp){
            low++;
        }
        array[high] = array[low];
 
    }
    array[low] =temp;
    return low;
}
 
 
 
//非递归的形式来实现快排
 
void quichsortnorecusive(int array[],int low,int high[]){
     stack<int> s;
 
     s.push(low);
     s.push(high);
 
     while(!s.empty()){
         int h = s.top(); s.pop();
         int l = s.top(); s.pop();
         int index= getindex(array,l,h);
         if(index-1>l){
             s.push(l);//左边
             s.push(index-1);//右边
 
         }
         else(index+1<h){
             s.push(index +1);
             s.push(h);
         }
 
     }
     
}

树的遍历 递归与非递归实现

 
void preorder(Tree *tree){
    if(t==NULL){
        return;
    }
    cout<<tree->val<<endl;
    preorder(tree->left);
    preorder(tree->right);
}
 
 
void inorder(Tree *tree){
    if(tree==NULL){
        return;
    }
    inorder(tree->left);
    cout<<tree->val;
    inorder(tree->right);
}
 
void lastorder (Tree *tree){
    if (tree== NULL){
        return;
    }
    lastorder(tree->left);
    lastorder(tree->right);
    cout<<tree->val;
}
 
void preorder( Tree *tree){
    stack<Tree *> s;
    while(tree != NULL || (!s.empty())){
        if(tree!=NULL){
            cout<<tree->val<<endl;
            s.push(tree);
            tree = tree->left;
        }else{
            tree = s.top();
            s.pop();
            tree = tree->right;
        }
    }
}
 
void inorder(Tree* tree){
    stack<Tree*>s;
    while(tree!=NULL || !s.empty()){
        if (tree!=NULL){
            s.push(tree);
            tree = tree->left();
        } else{
            tree =s.top();
            s.pop();
            cout<<tree->val;
            tree = tree->right;
        }
 
    }
}
 
void lastvisit(Tree *tree){
    stack<int> s;
    Tree * last = root;
    while (tree!=NULL || !s.empty()){
        if(tree!=NULL){
            s.push(tree);
            tree = tree->left;
        } else if( tree->right ==NULL || last == tree->right){
            tree = s.top();
            cout<<tree->val<<endl;
            last = tree;
            tree =NULL;
        } else{
            tree = tree->right;
        }
        
    }
 
}


// 从上到下打印二叉树
 
void printtree(Tree * tree){
    if (tree ==NULL){
        return;
    }
    dequeue<Tree*> s;
    s.push_back(tree);
    while(!s.empty()){
        Tree *temp = s.front();
        s.pop_front();
 
        if(tree->left!=NULL){
            tree = tree->left;
            s.push_back(tree);
        }
        if(tree->right !=NULL){
            tree = tree->right;
            s.push_back(tree)
        }
    }
}


leetcode 62 路径问题

————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ttomchy/article/details/105023012

class Solution {
public:
    int uniquePaths(int m, int n) {
        if(m<0 ||n<0){
            return 0;
        }
        long long dp[m][n];
        for (int i=0; i<m;i++ ){
            for (int j=0;j<n;j++){
                dp[i][j]=0;
            }
        }
        for (int i=0;i<m;i++){
             dp[i][0]=1;
        }
        for (int j=0;j<n;j++){
             dp[0][j]=1;
        }
        for (int i=1;i<m;i++){
            for (int j=1;j<n;j++){
                dp[i][j] = dp[i-1][j]+dp[i][j-1];
            }
        }        
 
        return dp[m-1][n-1];
    }
};

 leetcode 计算最小路径


 
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        if(m<=0||n<=0){
            return 0;
        }
        int dp[m][n];
 
        dp[0][0] = grid[0][0];
        for (int i =1;i<m;i++){
            dp[i][0]= grid[i][0]+ dp[i-1][0];
        }
        for (int j=1;j<n;j++){
            dp[0][j] = grid[0][j] + dp[0][j-1];
        }
        
        for (int i=1;i<m;i++){
            for (int j=1;j<n;j++){
                dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]; 
            }
        }
        return dp[m-1][n-1];
    
    }
};
 
 
 
 
 
  
 
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        if(m<=0||n<=0){
            return 0;
        }
        int dp[m][n];
 
        dp[0][0] = grid[0][0];
        for (int i =1;i<m;i++){
            dp[i][0]= grid[i][0]+ dp[i-1][0];
        }
        for (int j=1;j<n;j++){
            dp[0][j] = grid[0][j] + dp[0][j-1];
        }
        
        for (int i=1;i<m;i++){
            for (int j=1;j<n;j++){
                dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]; 
            }
        }
        return dp[m-1][n-1];
    
    }
};
 
 
 
 
 
 
class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        if(m<=0||n<=0){
            return 0;
        }
        int dp[m][n];
 
        dp[0][0] = grid[0][0];
        for (int i =1;i<m;i++){
            dp[i][0]= grid[i][0]+ dp[i-1][0];
        }
        for (int j=1;j<n;j++){
            dp[0][j] = grid[0][j] + dp[0][j-1];
        }
        
        for (int i=1;i<m;i++){
            for (int j=1;j<n;j++){
                dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]; 
            }
        }
        return dp[m-1][n-1];
    
    }
};
 
 
 
 
 
 




01 揹包问题 
状态转移方程:
定义f[i][j]:前i个物品,揹包容量j下的最优解

1)当前揹包容量不够(j < w[i]),为前i-1个物品最优解:f[i][j] = f[i-1][j]
2)当前揹包容量够,判断选与不选第i个物品

选:f[i][j] = f[i-1][j-w[i]] + v[i]
不选:f[i][j] = f[i-1][j]

 
#include<iostream>
using namespace std;
const int N=1010;
int n,m;
int value[N];
int weight[N];
int f[N][N];
int main(){
    
    cin>>n>>m;
    for(int i =1;i<=n;i++){
        cin>>weight[i]>> value[i];
    }
    
    for (int i=1;i<=n;i++){
    
        for (int j=1;j<=m;j++){  
                 f[i][j] = f[i-1][j];//左半边的子集,就是对于不选择当前物体;
            
            if(j>=weight[i])
            {
                f[i][j]  = max(f[i][j],f[i-1][j-weight[i]] +value[i] );
            }
            
        }
    }
    
   //m 为最开始定义的体积的总的质量
    cout<< f[n][m]<<endl;
    
}
————————————————
版权声明:本文为CSDN博主「ttomchy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ttomchy/article/details/105157114
 
完全揹包问题与01揹包问题分析

 /*在这里对比分析一下 01揹包问题和完全揹包问题的解法
01揹包问题:f[i][j] = max(f[i-1][j],f[i-1][j-v[i]+w[i])
完全揹包问题:f[i][j] = max(f[i-1][j],f[i][j-v[i]+w[i])
//v 代表体积; w 代表价值;
*/
 
 
#include<iostream>
using namespace std;
const int N=1010;
int w[N],v[N];
//v 代表体积; w 代表价值;
int f[N][N];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n; i++){
        cin >>v[i]>>w[i];
    }
    
    for (int i =1;i<=n;i++){
        for (int j =1;j<=m;j++){
            f[i][j] = f[i-1][j];
            if(j>=v[i]){
                 f[i][j] = max(f[i][j], f[i][j-v[i]]+w[i]);
            }
            
        }
    }
    
    cout<< f[n][m]<<endl;
    
}


#include<iostream>
using namespace std;
const int N= 10000;
char A[N],B[N];
int n,m;
int f[N][N];
int main(){
    
  cin>>n>>m;
  for (int i =1;i<=n;i++){
      cin>> A[i];
  }
  for (int j =1;j<=m;j++){
      cin>> B[j];
  }
  
  
  for (int i =1;i<=n;i++){
      for (int j=1;j<=m;j++){
          f[i][j] = max(f[i-1][j],f[i][j-1]); 
          if(A[i]==B[j]){
              f[i][j]  = max(f[i][j],f[i-1][j-1]+1);
          }
      }
  }
  
  cout<<f[n][m]<<endl;
  
  
  
return 0;
 
}



leetcode 两数之和

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
     
     
     
        map<int,int> m;
 
        for (int i=0;i<nums.size(); i++){
            m[nums[i]] = i;//在这里进行一遍哈希,把数据存下来
        }
        vector<int > result;
 
        for (int i =0; i<nums.size(); i++){
            if(m.find(target - nums[i])!=m.end() && m[ target - nums[i]] != i) { //在这里进行一下数据的查找,如果找的到数据,并且不是 i 本身的话 就可以放入结果之中
               result.push_back(i);
               result.push_back(m[target-nums[i]]);
               return result;
            }
        }
       return {};
 
 
       /*
        vector<int> result;
        for(int i=0;i<nums.size();i++){
            for(int j=i;j<nums.size();j++){
                if (nums[i] + nums[j] ==target &&i!=j){
                    result.push_back(i);
                    result.push_back(j);
                }
            }
        }
        return result;
        */
    }
};
 
 
// 加油加油 Try to make yourself more excellent.



class Solution {
public:
     int searchInsert(vector<int>& nums, int target) {
        long i =0;
        long j = nums.size();
        if(j ==0){
            return 0;//这里也要判断元素的个数
        }
 
        if (nums[j-1]<target){
            return j; //在这里也要加入判断元素的个数
        }
 
        while (i<=j){
            long mid = (i+j)/2;
            if(nums[mid] ==target){
                return mid;
            } 
            else if (nums[mid] >target){
                j = mid -1;
            }
            else {
                i = mid +1;
            }
        }
        return i;
    }
};
 
//加油加油,try to make yourself more excellent

leetcode 两数之和

class Solution {
public:
    vector<int> twoSum(vector<int>& numbers, int target) {
 
        //在这里利用剑指 offer 的方法,使用两个指针,一个从前向后扫,一个从后向前扫
        int N = numbers.size();
        vector<int> result;
        
        if(N<2){
            return result;
        }
        
     
        int left =0;
        int right =N-1;
        
        while (left<right){
 
            if (numbers[left] + numbers[right] == target){
                result.push_back(left+1);
                result.push_back(right+1);
                break;
            }
            else if(numbers[left] + numbers[right] < target){
                left ++;
            }
            else{
                right --;
            }
        }
 
        return result;
    }
};
 

leetcode 二分查找错误版本


// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
 
class Solution {
public:
    int firstBadVersion(int n) {
        long left = 0;
        long right = n;
        if(n==0){
            return 0;
        }
        while (left<=right){ //典型的二分查找算法
              long mid = (right +left)/2;
 
              if(isBadVersion(mid)){
                  right = mid-1;
              } else{
                  left = mid +1;
              }
        }
        return left;
 
 
 
    }
};
leetcode 27 移除元素

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int size = nums.size();
 
        for (int i =0; i<nums.size(); i++){
            if(nums[i] ==val){
                nums[i] = INT_MAX;
            }
        }
 
        nums.erase(remove(nums.begin(),nums.end(),INT_MAX),nums.end());
        return nums.size();
    }
};// 加油加油,Try to make yourself more excellent...


leetcode 53. 最大子序和


class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int size = nums.size();
 
        if(size ==0){
            return 0;
        }
        
        int dp = nums[0];
        int maxdp = nums[0];
        
        for (int i =1; i<size;i++){
        
            if (dp>0){
                dp  = dp + nums[i];
            } else {
                dp = nums[i];
            }  
            
            if (dp > maxdp){
                maxdp = dp;
            }
            
        }
        return maxdp;
    }
};
//动态规划,递归公式:dp[i] =  data[i]          i=0或dp[i-1]<=0
    //                       dp[i-1]+data[i]  i!=0且dp[i-1]>0
    //由于只需知道前一个情况的dp值,因此可省去dp数组,申请个变量记录即可
//加油加油 Try to make yourself more excellent



leetcode 860. 柠檬水找零



class Solution {
public:
    bool lemonadeChange(vector<int>& bills) {
     
     int five =0; //在这里定义一下初始值
     int ten =0;
     for (auto b:bills){ //开始遍历数据
          if(b==5){
              five ++; //如果 遇到了个 5 ; five +1;
          }else if(b==10){
              ten ++; //如果是10 ; ten +1; fvie -1;
              if(five){
                   five --;
              } else{
                  return false; //如果没有 five 则 返回 false
              }
          }
          else{
              int temp =15;//定义一个临时变量 15
              if(ten){// 如果有10的话 
                  ten--;// ten -1
                  temp -=10; //temp -10;
              }
 
              while (temp && five){ //当temp >0 或者 five 存在时
                  five --;
                  temp -=5; //temp 每次减 5
              }
              if(temp){ //如果最后temp还存在的话,就是无法找开零钱
                  return false;
              }
 
        }
 
 
     }
 
     return true; //否则的话就返回 true
 
// 加油加油 。Try to make yourself more excellent
 
    }
};



455. 贪心算法 分发饼干

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
       sort (g.begin(),g.end());// g 为 孩子的胃口大小
       sort (s.begin(),s.end());// s 为饼干的大小
       int result =0;
       int i =0;
       int j =0; // 这道题是一个典型的贪心算法的例子,我们首先将孩子们的期望的大小和饼干的大小进行一下排序,
                // 然后进行遍历饼干的大小判断一下是否符合孩子们的期望不是的话 就向后移动一个指针,如果满足的话
                // 就把 res +1; j+1; 因为这个孩子的期望已经满足了
       for( int i =0;i<g.size();i++){//s 为饼干的大小
 
            while(j< s.size() && s[j]<g[i] ){
                j++; // 饼干向后移动一下
            }
 
            if(j< s.size()) {
                j++; //这个饼干已经用完所以要+1
                result ++;//最后的结果要 +1;
            }
 
       }
 
       return result;
    }
};
// 加油加油。 Try to make yourself more excellent...

class Solution {
public:
    int jump(vector<int>& nums) {
 
 /*
对于这道题目的话,首先这道题目可以看出来 假设F(x) 为 能跳到的最远的距离
那么 F(x) 为分段函数,举个例子 
0,1,1,1,2,2,2,2,3,3,3,3,3,4,4,4,4
第 0个格子只需要 0步就可以跳的到;
第 1到3 个格子需要1 步就可以跳的到
第4 到7 个格子可以两个步骤就可以跳的到
 */
 
 
        int left =0; //因此在这里我们利用一个区间搜索的形式来进行搜索 left左边界,right 右边界
        int right =0;
        int temp =0;
        if(nums.size() ==1){
            return 0;
        }
        while (left <=right){//最开始的时候 leftleft==right==0
             int max_dist =0; //最开始的时候我们利用一个变量来代表当前区间内最远能到的距离
             for(int i=left;i<=right; i++){
                 max_dist = max(max_dist, nums[i] + i);
             }
 
             left = right +1;//在这里不断地更新左边界和右边界;
             right = max_dist;
             temp ++;//步数 +1
             if(right >= (int) nums.size()-1) {
                 break;//当最后的数据已经在我们当前区间能到达的最远范围的时候,就停止,用 break;
             }  
        }
        return temp;
 
    }
};
 
//加油,加油,Try to make yourself more excellent...
454. 使用hash 四数相加 II



class Solution {
public:
    int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
        unordered_map <int ,int> res; //在这里使用hash map 来进行计算
        for(auto a:A){
            for (auto b:B){
                 res[a+b] ++;
            }
        }
        int result =0;
        for( auto c:C){
            for(auto d:D){
               result += res[-c-d]; //在这里对每个值进行累加 注意这里的: -C-D
            }
        }
        return result;
    }
};
 
// 加油加油。 Try to make yourself more excellent


class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        unordered_map <int ,int> hash;//在这里的思想为: 我们假设一个区间
        int result =0;
        int s =0;
        hash[0] =1;//这里设置为 1 的原因是,如果输入为 [3,-3], K=0;
        //  那么就需要我们设置 hash[0] =1; 这个初始值了
        for(auto x:nums){
           s +=x;
           if(hash.find(s-k) != hash.end()){
               result += hash[s-k];//把前面 hash[s-k] 的次数累加起来
           }
           hash[s] ++;
        }
        return result; //返回来结果
    }
};
 
 
/*假如存在区间[left,right],使得在[left,right]这个区间的子数组的和为k。换句话说,就是前right项和减去前left项和等于k,即前left项和等于前right项和减去k。
可以这样做,在扫描数组的同时,假设当前扫到第i位,记录它的前i项和sum,用该和减去k,即sum-k,判断sum-k是否为某个位置的前n项和,若是,更新统计量。
作者:jarvis1890
链接:https://leetcode-cn.com/problems/subarray-sum-equals-k/solution/qian-zhui-he-shi-xian-jian-dan-by-jarvis1890/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
*/
// 加油加油。Try to make yourself more excellent

class Solution {
public:
 
  static  bool compare(pair<int, int> left, pair<int,int> right){
       return  left.second > right.second; //首先在这里我们定义一个函数用来比较数据,结果为从大到小排序
   }
 
    vector<int> topKFrequent(vector<int>& nums, int k) {
       unordered_map <int, int> hash;
       for (int i =0; i<nums.size(); i++){
           hash[nums[i]] ++;// 在这里利用哈希表进行统计每个数字出现的频率
       }
 
       vector <pair<int,int>> v(hash.begin(),hash.end());// 把hash 的数据放到vector里面
       sort (v.begin(),v.end(),compare); //进行排序,结果为从大到小排序
 
       vector<int> result;
       for(int i =0; i<k; i++){
         result.push_back(v[i].first);// 在这里我们将前 K 个数据进行输出
 
       }
 
       return result;
    }
};
 
// 加油加油. Try  to make yourself more excellent...


class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
       if(nums1.size() >nums2.size()){ // 在这里我们设置一下数据,少的那个找出来
           return intersect(nums2,nums1);// 交换数据
       }
       unordered_multiset<int> hash; //在这里我们选择multiset
       for(auto x : nums1){
           hash.insert(x);//插入数据
       }
       vector<int> result;
 
       for(auto x : nums2){
           if(hash.count(x)>0){//如果数据已经存在的话
              result.push_back(x);  //保存数据    
              auto it = hash.find(x);// 删除掉其中一个数据
              hash.erase(it);
           }
       }
 
       return result; //返回结果
 
    }
};
 
// 加油加油... Try to make yourself more excellent...

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章