简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Return
[1,3,3,1]
.
Note:
Could you optimize your algorithm to use only O(k) extra space?
» Solve this problemCould you optimize your algorithm to use only O(k) extra space?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> getRow(int rowIndex) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
vector<int> res1, res2; | |
res1.push_back(1); | |
for(int i=1; i<=rowIndex; i++) | |
{ | |
if(i%2==1) | |
{ | |
for(int j=0; j<=i; j++) | |
{ | |
if(j==0||j==i) | |
res2.push_back(res1[0]); | |
else | |
res2.push_back(res1[j-1]+res1[j]); | |
} | |
res1.clear(); | |
} | |
else | |
{ | |
for(int j=0; j<=i; j++) | |
{ | |
if(j==0||j==i) | |
res1.push_back(res2[0]); | |
else | |
res1.push_back(res2[j-1]+res2[j]); | |
} | |
res2.clear(); | |
} | |
} | |
return res1.empty()? res2:res1; | |
} | |
}; |
No comments:
Post a Comment