LeetCode练习-难题卷

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JIEJINQUANIL/article/details/52561464

题目链接:LeetCode练习-难题卷

1、There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you must give?

分析:由于每个孩子至少有一颗糖,因此初始给每个孩子1颗糖。首先从左向右扫描,若第i个孩子的权值大于第i-1个孩子的权值,那么第i个孩子的糖要比第i-1个孩子的糖要多一颗。然后从右向左扫描,若第i个孩子的权值大于第i+1个孩子的权值并且第i个孩子的糖果数小于等于第i+1个孩子的糖果数,那么第i个孩子的糖要比第i+1个孩子的糖要多一颗。Talk is cheap,show me the code!

class Solution {
public:
    int candy(vector<int> &ratings) {
        int len=ratings.size();
        if(len<=1) 
			return len;
        int sum=0;
        vector<int> v(len,1);          //初始将每个孩子的糖果数都设为1

        for(int i=1;i<len;i++)         //从左向右
            if(ratings[i] > ratings[i-1])
                v[i]=v[i-1]+1;
        for(int i=len-2;i>=0;i--)      //从右向左
            if(ratings[i] > ratings[i+1] && v[i] <= v[i+1])
                v[i]=v[i+1]+1;
         
        for(int i=0;i<len;i++)
            sum+=v[i];
        return sum;
    }
};

2、Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word ="ABCCED", -> returns true,
word ="SEE",         -> returns true,
word ="ABCB",      -> returns false.

分析:(1)、回溯法。

class Solution { 
public: 
    bool check(vector<vector<char>> &board, string word, int i, int j){ 
        if(word.length() == 0) 
            return true;
        if(i<0|| j<0|| i>=board.size() || j>=board[0].size()) 
            return false; 
        if(word[0] == board[i][j]){
            char c = word[0];
            board[i][j] = '\0'; 
            if( check(board, word.substr(1), i+1, j) || 
                check(board, word.substr(1), i-1, j) || 
                check(board, word.substr(1), i, j+1) || 
                check(board, word.substr(1), i, j-1) )
                return true; 
            board[i][j] = c;
        }
        return false;
    }
    bool exist(vector<vector<char> > &board, string word) { 
        if(board.size() == 0|| board[0].size() == 0) 
            return false; 
        for(int i = 0; i < board.size(); i++){ 
            for(int j = 0; j < board[0].size(); j++){ 
                if(check(board, word, i, j)) 
                    return true; 
            } 
        } 
        return false;
    } 
    
};
(2)深度优先
class Solution {
    int N,M;
public:
    bool exist(vector<vector<char> > &board, string word) {
        if(board.size()==0) 
            return false;
        if(word.length()==0) 
            return false;
        int i,j; 
        N=board.size(); M=board[0].size();
        vector<vector<int> > book(N,vector<int>(M,0));
        for(i=0;i<N;i++)
            for(j=0;j<M;j++)
                if(dfs(board,i,j,word,0,book))
            return true;
        return false;
    }
    bool dfs(vector<vector<char> > &board,int x,int y,string word,int p,vector<vector<int> > &book)
    {
        if(p==word.length()) 
            return true;
        if(x<0||x>=N||y<0||y>=M) 
            return false;
        if(book[x][y]==1) 
            return false;
        if(board[x][y]!=word[p]) 
            return false;
        book[x][y]=1;
        bool k=dfs(board,x+1,y,word,p+1,book)||dfs(board,x-1,y,word,p+1,book)||dfs(board,x,y+1,word,p+1,book)||dfs(board,x,y-1,word,p+1,book);
        book[x][y]=0;
        return k;
    }
};

3、Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)
分析:
class Solution {
public:
    vector<vector<int> > threeSum(vector<int> &num) {       
        sort(num.begin(), num.end());//数组排序(小-->大)       
        vector<vector<int>> ans;//结果集合
        for (int i = 0; i < num.size(); i++){    //先依次取出一个数,转换为 2 数求和问题
            if ( i == 0 || num[i] != num[i-1]){  //防止重复         
                int left = i + 1, right = num.size() - 1;  //定义左右指针 
                while (left < right){       // 2 数求和问题,固定left, 找出最大的right
                    while (left < right && num[i] + num[left] + num[right] > 0) right --;
                    if (left < right && num[i] + num[left] + num[right] == 0){      
                        vector<int> temp(3); //保存当前结果
                        temp[0] = num[i];
                        temp[1] = num[left];
                        temp[2] = num[right];
                        
                        ans.push_back(temp);  //保存到最终的结果中
                        while (left < right && num[left] == temp[1]) 
                            left ++;
                    } 
                    else  // num[i] + num[left] + num[right] < 0的情况,left右移,增大num[left]的值
                        left++;
                }
            }
        }
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/JIEJINQUANIL/article/details/52561464