小失误,基本一次过
1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 vector<int> pre; 7 for (int i = 0; i <= rowIndex; i++) { 8 vector<int> cur; 9 if (!pre.size()) { 10 cur.push_back(1); 11 pre = cur; 12 } 13 else { 14 for (int j = 0; j <= pre.size(); j++) { 15 if (!j || j == i) cur.push_back(1); 16 else cur.push_back(pre[j-1]+pre[j]); 17 } 18 pre = cur; 19 } 20 } 21 return pre; 22 } 23 };
C#
1 public class Solution { 2 public List<int> GetRow(int rowIndex) { 3 List<int> ans = new List<int>(); 4 for (int i = 0; i <= rowIndex; i++) { 5 List<int> cur = new List<int>(); 6 if (ans.Count == 0) { 7 cur.Add(1); 8 ans = new List<int>(cur.ToArray()); 9 } 10 else { 11 for (int j = 0; j <= ans.Count; j++) { 12 if ( j == 0 || j == i) cur.Add(1); 13 else cur.Add(ans[j-1] + ans[j]); 14 } 15 ans = new List<int>(cur.ToArray()); 16 } 17 } 18 return ans; 19 } 20 }