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

LeetCode: Pascal's Triangle

一点小失误,少数次过

 1 class Solution {
 2 public:
 3     vector<vector<int> > generate(int numRows) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         vector<vector<int>> ret;
 7         for (int i = 0; i < numRows; i++) {
 8             vector<int> line;
 9             for (int j = 0; j <= i; j++) {
10                 if (!j || j == i) line.push_back(1);
11                 else {
12                     vector<int> pre = ret[i-1];
13                     line.push_back(pre[j-1]+pre[j]);
14                 }
15             }
16             ret.push_back(line);
17         }
18         return ret;
19     }
20 };

 C#

 1 public class Solution {
 2     public List<List<int>> Generate(int numRows) {
 3         List<List<int>> ans = new List<List<int>>();
 4         for (int i = 0; i < numRows; i++) {
 5             List<int> line = new List<int>();
 6             for (int j = 0; j <= i; j++) {
 7                 if (j == 0 || j == i) line.Add(1);
 8                 else {
 9                     List<int> pre = new List<int>(ans[i-1].ToArray());
10                     line.Add(pre[j-1] + pre[j]);
11                 }
12             }
13             ans.Add(line);
14         }
15         return ans;
16     }
17 }
View Code

 

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