• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Pascal's Triangle II

小失误,基本一次过

 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 }
View Code

 

posted @ 2013-04-19 04:09  ying_vincent  阅读(146)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3