485. Max Consecutive Ones \ 118. Pascal's Triangle
生活随笔
收集整理的這篇文章主要介紹了
485. Max Consecutive Ones \ 118. Pascal's Triangle
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- Max Consecutive Ones
- Pascals Triangle
485. Max Consecutive Ones
法一:
class Solution { public:int findMaxConsecutiveOnes(vector<int>& nums) {int result_max = 0;int nums_len = nums.size();int tmp = 0;if(!nums_len)return 0;else if(nums[0]) {result_max = 1;} for(int i = 0; i < nums_len - 1; i++){if((nums[i] == nums[i + 1]) && nums[i]){// cout << i << " " << tmp << endl;tmp = (tmp==0)?2:(tmp+1);if(tmp > result_max)result_max = tmp;} else {if(nums[i] || nums[i+1]){if(!result_max)result_max = 1;}if(tmp > result_max)result_max = tmp;tmp = 0;}}return result_max;} };法二:
public:int findMaxConsecutiveOnes(vector<int>& nums) {int count = 0, max = 0;for (int i = 0; i < nums.size(); ++i){if (nums[i]){if (++count > max) max = count;}else count = 0;}return max;} };118. Pascal’s Triangle
法一:
class Solution { public:vector<vector<int>> generate(int numRows) {vector<vector<int> > result;for(int i = 1; i <= numRows; i++){vector<int> tmp;for(int j = 1; j <= i; j++){if(j == 1 || j == i){tmp.push_back(1);}if(i > 2 && j>1 && j<i){tmp.push_back(result[i-2][j-2]+result[i-2][j-1]);}}result.push_back(tmp);}return result;} };法二:
class Solution { public:vector<vector<int>> generate(int numRows) {vector<vector<int>> res;for(auto i=0;i<numRows;++i){res.push_back(vector<int>(i+1,1));for(auto j=1; j<i; ++j) res[i][j] = res[i-1][j-1] + res[i-1][j];}return res;} };在刷題的時候我發現C++的for循環會檢查初始條件,C不會。
總結
以上是生活随笔為你收集整理的485. Max Consecutive Ones \ 118. Pascal's Triangle的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 标签 href 怎么拼接_【微信】用户-
- 下一篇: 如何根据灰度直方图计算标准差_如何根据电