Leetcode(3)-Summary Ranges

228 Summary Ranges
For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].

class Solution {
    //此处将头尾格式化为字符串
    string format(int begin, int end)  
    {  
        char buffer[32];  
        if (end == begin)  
        {  
            sprintf(buffer, "%d", begin);  
        }else{  
            sprintf(buffer, "%d->%d", begin, end);  
        }  
        return string(buffer);  
    } 

    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> result;  
        if (nums.size()<1)  
            return result; 
        int begin, end;  
        begin = nums[0];
        end = nums[0];
        for(int i=1; i<nums.size(); i++)
        {
            if(nums[i] == end || nums[i] == end+1)
                end = nums[i];
            else
            {
                result.push_back(format(begin, end));
                begin = nums[i];
                end = nums[i];
            }
        }
        //别忘了处理最后一个元素,同时也处理了单个元素的情况
        result.push_back(format(begin, end)); 
        return result;
    }
};

62 Unique Paths
动态规划,从左上到右下;
此处注意 vector二维向量用法,等价于

int path[101][101];
for(int i = 0; i < 101; i ++) { for(int j = 0; j < 101; j ++) { path[i][j] = 1; } }

此外,每次只需要用到上一行当前列,以及前一列当前行的信息,也可以只要用一个一维数组存上一行的信息,即res[i]+=res[i-1]

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> path(m, vector<int>(n, 1));
        for(int i = 1; i < m; i ++)
        {
            for(int j = 1; j < n; j ++)
            {
                path[i][j] = path[i-1][j] + path[i][j-1];
            }
        }
        return path[m-1][n-1];
    }
};

63.Unique Paths II
依然是动态规划,但由于障碍物,使得机器人不是每次都有两个方向的选择。所以要添加初始化过程,以及修正动态规划过程中计算策略。

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        int i,j;
        vector<vector<int>> path(m, vector<int>(n, 0));
        //初始化:若有障碍则path=0,否则为1
        for (i = 0; i < m; i ++) 
        {
            if (obstacleGrid[i][0] == 0) 
                path[i][0] = 1;
            else
                break;
        }
        for (j = 0; j < n; j ++) 
        {
            if (obstacleGrid[0][j] == 0)
                path[0][j] = 1;
            else 
                break;
        }
        //动态规划:若有障碍则此处path=0
        for(i = 1; i < m; i ++)
        {
            for(j = 1; j < n; j ++)
            {
                if(obstacleGrid[i][j] == 1)
                    path[i][j] = 0;
                else
                    path[i][j] = path[i-1][j] + path[i][j-1];
            }
        }
        return path[m-1][n-1];
    }
};

PS:在其他人的答案中发现使用三目运算符可以简化代码:

//哪里不太对的样子。。
class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int i,j;
        vector<vector<int> > path(obstacleGrid.size(), vector<int>(obstacleGrid[0].size()));
        //初始化
        path[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1;
        for (i = 1; i < path.size(); i ++) 
            path[i][0] = obstacleGrid[i][0] == 1 ? 0 : path[i-1][0];
        for (j = 1; j < path[0].size(); j ++) 
            path[0][j] = obstacleGrid[0][j] == 1 ? 0 : path[0][i-1];
        //动态规划
        for(i = 1; i < path.size(); i ++)
            for(j = 1; j < path[0].size(); j ++)
                path[i][j] = obstacleGrid[i][j] == 1 ? 0 : path[i-1][j] + path[i][j-1];

        return path[path.size()-1][path[0].size()-1];
    }
};

120 triangle

class Solution {
public:
    int minimumTotal(vector<vector<int> > &triangle) {
        for (int i = triangle.size() - 2; i >= 0; --i)
            for (int j = 0; j <= i; ++j)
                triangle[i][j] += min(triangle[i+1][j+1],triangle[i+1][j]);
        return triangle[0][0];
  }
};

5.最长回文子串
字符串s中的最长回文串是s的倒转s’和s的最长连续公共子串
或者直接使用动态规划求解O(N^2),考虑Define P[ i, j ] ← true iff the substring Si … Sj is a palindrome, otherwise false.
P[ i, j ] ← ( P[ i+1, j-1 ] and Si = Sj )
或者遍历子串,以每个字母为中心向两边扩散O(N^2)

class Solution {
public:
    //从中间向两边展开  
    string expandAroundCenter(string s, int c1, int c2) {  
        int l = c1, r = c2;  
        int n = s.length();  
        while (l >= 0 && r <= n-1 && s[l] == s[r]) {  
            l--;  
            r++;  
        }  
        return s.substr(l+1, r-l-1);  
    }  

    string longestPalindromeSimple(string s) {  
        int n = s.length();  
        if (n == 0) return "";  
        string longest = s.substr(0, 1);  // a single char itself is a palindrome 
        for (int i = 0; i < n-1; i++) {  
            string p1 = expandAroundCenter(s, i, i); //长度为奇数的候选回文字符串 
            if (p1.length() > longest.length())  
                longest = p1;  

            string p2 = expandAroundCenter(s, i, i+1);//长度为偶数的候选回文字符串 
            if (p2.length() > longest.length())  
                longest = p2;  
        }  
        return longest;  
    }  
};

你可能感兴趣的:(LeetCode)