当前位置:网站首页>leetCode-2221: 数组的三角和
leetCode-2221: 数组的三角和
2022-06-24 09:43:00 【文丑颜不良啊】
题目描述
给你一个下标从 0 开始的整数数组 nums ,其中 nums[i] 是 0 到 9 之间(两者都包含)的一个数字。
nums 的三角和是执行以下操作以后最后剩下元素的值:
nums 初始包含 n 个元素。如果 n == 1 ,终止操作。否则,创建一个新的下标从 0 开始的长度为 n - 1 的整数数组 newNums 。
对于满足 0 <= i < n - 1 的下标 i ,newNums[i] 赋值为 (nums[i] + nums[i+1]) % 10 ,% 表示取余运算。
将 newNums 替换数组 nums 。
从步骤 1 开始重复整个过程。
请你返回 nums 的三角和。
示例
示例 1:
输入:nums = [1,2,3,4,5]
输出:8
解释:
上图展示了得到数组三角和的过程。
示例 2:
输入:nums = [5]
输出:5
解释:
由于 nums 中只有一个元素,数组的三角和为这个元素自己。
解题过程
思路及步骤
(1)双层循环暴力去解,或者加上动态规划也可以解答;
(2)循环时需要注意内层 j 的循环从 0 开始,到 nums.length - i 结束,因为每次循环都较上次少一个数;
(3)最后返回数组最后最后一行第 1 个元素即可。
代码展示
public class TriangularSum {
public int triangularSum(int[] nums) {
int[][] dp = new int[nums.length][nums.length];
dp[0] = nums;
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < nums.length - i; j++) {
dp[i][j] = (dp[i - 1][j] + dp[i - 1][j + 1]) % 10;
}
}
// 将二维数组打印
display(dp);
return dp[nums.length - 1][0];
}
private void display(int[][] dp) {
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[i].length; j++) {
System.out.printf("%4d", dp[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
int[] nums = {
1, 2, 3, 4, 5};
System.out.println(new TriangularSum().triangularSum(nums));
}
}
边栏推荐
- numpy.logical_and()
- Wechat applet learning to achieve list rendering and conditional rendering
- 415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
- YOLOv6:又快又准的目标检测框架开源啦
- Phpstrom code formatting settings
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- SQL Sever中的窗口函数row_number()rank()dense_rank()
- Top issue tpami 2022! Behavior recognition based on different data modes: a recent review
- 植物生长h5动画js特效
- 5.菜品管理业务开发
猜你喜欢

机器学习——感知机及K近邻

小程序学习之获取用户信息(getUserProfile and getUserInfo)

oracle池式连接请求超时问题排查步骤

Queue queue

Cicflowmeter source code analysis and modification to meet requirements

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

Development of anti fleeing marketing software for health products

Groovy obtains Jenkins credentials through withcredentials

有关二叉树 的基本操作

队列Queue
随机推荐
Recursive traversal of 414 binary tree
JS singleton mode
简单的价格表样式代码
416 binary tree (first, middle and last order traversal iteration method)
port 22: Connection refused
被困英西中学的师生安全和食物有保障
Programming questions (continuously updated)
大中型企业如何构建自己的监控体系
uniapp实现禁止video拖拽快进
MYSQL数据高级
Floating point notation (summarized from cs61c and CMU CSAPP)
Thinkphp5 clear the cache cache, temp cache and log cache under runtime
记录一下MySql update会锁定哪些范围的数据
numpy.logical_or
NVIDIA's CVPR 2022 oral is on fire! 2D images become realistic 3D objects in seconds! Here comes the virtual jazz band!
What are the characteristics of EDI local deployment and cloud hosting solutions?
时尚的弹出模态登录注册窗口
Indexeddb local storage, homepage optimization
利用pandas读取SQL Sever数据表
读取csv(tsv)文件出错
输入:nums = [1,2,3,4,5]