当前位置: 首页 > news >正文

Leetcode :杨辉三角

给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

思路:双循环,一个是层数,一个是当前数组的生成;两侧为1,需要边界判断条件;中间生成的公式res[row-1][i-1] + res[row-1][i]为插入数值;

!!!不能直接二位数组插入单个字符元素,可以先生成temp数组,一行结束后讲temp以元素形式插入到res结果数组中。

!!!记得temp清空temp.clear()

#include <iostream>
#include <vector>using namespace std;class Solution {
public:vector<vector<int>> generate(int numRows) {vector<vector<int>> res;vector<int> temp;for (int row = 0; row < numRows; row++){for (int i = 0; i < row + 1; i++){if (i == 0 || i == row){temp.push_back(1);}else{temp.push_back(res[row-1][i-1] + res[row-1][i]);}}res.push_back(temp); // 保存前一行temp.clear(); // 清空临时数组}return res;}
};int main(){Solution s;vector<vector<int>> res = s.generate(5);cout << "[";for (auto i : res){if (i == res[0]) cout << "[";else cout << ",[";for (auto j : i){if (j == i[0])   cout << j;else cout << "," << j;}cout << "]";}cout << "]";return 0;
}

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

思路:在原有基础上改进的算法,就是输出最后一行,浪费资源,时间复杂度较高

!!!看了示例代码,才知道杨辉三角可以推导,不得不说,单循环遍历就够了,直接生成

#include <iostream>
#include <vector>using namespace std;// class Solution {
// public:
//     vector<int> getRow(int rowIndex) {
//         vector<vector<int>> res;
//         vector<int> temp;
//         for (int row = 0; row <= rowIndex; row++){
//             for (int i = 0; i <= row; i++){
//                 if (i == 0 || i == row){
//                     temp.push_back(1);
//                 }
//                 else{
//                     temp.push_back(res[row-1][i-1] + res[row-1][i]);
//                 }
//             }
//             res.push_back(temp); // 保存前一行
//             temp.clear(); // 清空临时数组
//         }
//         return res[rowIndex];
//     }
// };class Solution {
public:vector<int> getRow(int rowIndex) {vector<int> ans = {1};long long c = rowIndex;int n = rowIndex;for (int i = 1; i <= rowIndex;) {ans.push_back(c);c *= --n;c /= ++i;}return ans;}
};
int main(){Solution s;vector<int> res = s.getRow(3);for (int i = 0; i < res.size(); i++){cout << res[i] << " ";}cout << endl;return 0;
}

http://www.lryc.cn/news/309292.html

相关文章:

  • MWC 2024丨美格智能CEO杜国彬出席中国联通创新成果发布会并发表主题演讲
  • 个人建站前端篇(七)vite + vue3企业级项目模板
  • centos7 安装 docker-compose
  • 剑指offer面试题28:对称的二叉树
  • JS:原型与原型链(附带图解与代码)
  • 电子电器架构新趋势 —— 最佳着力点:域控制器
  • C++记录
  • ConcurrentModificationException并发修改异常
  • 小程序事件处理
  • 蓝桥杯-单片机组基础6——定时计数器与外部中断混合使用(附小蜜蜂课程代码)
  • 交友社交软件开发-php交友聊天系统-
  • vue2 开发记录
  • QML中表格中数据获取
  • 【mysql 数据库事务】开启事务操作数据库,写入失败后,不回滚,会有问题么? 这里隐藏着大坑,复试,面试时可以镇住面试老师!!!!
  • Go语言的100个错误使用场景(55-60)|并发基础
  • 钉钉机器人发送折线图卡片 工具类代码
  • 基于springboot的大型商场应急预案管理系统论文
  • 强化学习嵌入Transformer(代码实践)
  • 决定西弗吉尼亚州地区版图的关键历史事件
  • LeetCode_22_中等_括号生成
  • Verilog(未完待续)
  • 【Linux实践室】Linux初体验
  • Flutter中高级JSON处理:使用json_serializable进行深入定制
  • 华为OD技术面试案例4-2024年
  • 【TestNG】(4) 重试机制与监听器的使用
  • “智农”-高标准农田
  • 利用 lxml 库的XPath()方法在网页中快速查找元素
  • nginx---------------重写功能 防盗链 反向代理 (五)
  • unity shaderGraph实例-物体线框显示
  • 分类问题经典算法 | 二分类问题 | Logistic回归:公式推导