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

博客园    首页    新随笔    联系   管理    订阅  订阅

leetcode_question_119 Pascal's Triangle II

 

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

 

vector<int> getRow(int rowIndex) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> matrix(rowIndex+1);
        if(rowIndex < 0) return matrix;
        for(int i = 0; i <= rowIndex; ++i)
        {
    		if( i == 0) {matrix[0] = 1;continue;}
			int mid = i/2;
			int tmpnum = matrix[0];
			for(int j = 1; j <= mid; ++j)
			{
				int tmp = tmpnum + matrix[j];
				tmpnum = matrix[j];
				matrix[j] = tmp;
			}
			++mid;
			while(mid <= i){
				matrix[mid] = matrix[i-mid];
				++mid;
			}
        }
        return matrix;
    }


 

 

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