Pascal's Triangle
2015-05-26 10:07
169 查看
题目描述:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given
The longest consecutive elements sequence is
Your algorithm should run in O(n) complexity.
代码:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given
[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is
[1, 2, 3, 4]. Return its length:
4.
Your algorithm should run in O(n) complexity.
代码:
class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> result; vector<int> line1 = {1}; vector<int> line2 = {1,1}; if(numRows == 0) return result; result.push_back(line1); if(numRows == 1) return result; result.push_back(line2); if(numRows == 2) return result; for(int i = 3;i <= numRows;i++) { vector<int> temp; for(int j = 0;j < i;j++) { if(j == 0 || j == (i-1)) temp.push_back(1); else temp.push_back(result[i-2][j-1]+result[i-2][j]); } result.push_back(temp); } } };
相关文章推荐
- Pascal's Triangle
- Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]
- Pascal's Triangle
- Pascal's Triangle I & II
- LeetCode Pascal's Triangle
- [Leetcode] Pascal's Triangle (Java)
- Pascal's Triangle
- Pascal's Triangle
- LeetCode 118. Pascal's Triangle(帕斯卡三角)
- leetcode之Pascal's Triangle
- Pascal's Triangle (leetcode)
- [leetCode 118 & 119] Pascal's Triangle I && II (杨辉三角问题)
- Pascal's Triangle
- LeetCode Pascal's Triangle
- 【LeetCode】Pascal's Triangle (杨辉三角)
- Lettcode_118_Pascal's Triangle
- leetcode:119 Pascal's Triangle II-每日编程第二十五题
- Pascal's Triangle
- LeetCode Pascal's Triangle
- LeetCode 118. Pascal's Triangle