码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Pascal's Triangle II

时间:2014-06-30 23:30:51      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   io   leetcode   

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        int half = (rowIndex + 1) / 2;
        int len = half * 2  + !(rowIndex & 0x1);
        
        int* ra = new int[half + 1];
        int* rb = new int[half + 1];
        
        ra[0] = 1;
        rb[0] = 1;
        
        for (int i=1; i<=rowIndex; i++) {
            int h = (i + 1) / 2;
            int j = 1;
            for (; j<h; j++) {
                rb[j] = ra[j-1] + ra[j];
            }
            if (!(i & 0x1)) {
                rb[j] = ra[j-1] * 2;
            }
            int* t = ra;
            ra = rb;
            rb = t;
        }
        vector<int> res(len, 0);
        
        for (int i=0; i<half; i++) {
            res[i] = ra[i];
            res[len - i - 1] = ra[i];
        }
        if (!(rowIndex & 0x1)) res[half] = ra[half];
        return res;
    }
};

不顺

LeetCode Pascal's Triangle II,布布扣,bubuko.com

LeetCode Pascal's Triangle II

标签:style   blog   color   for   io   leetcode   

原文地址:http://www.cnblogs.com/lailailai/p/3816065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!