当前位置:网站首页>Leetcode-2221: triangular sum of arrays
Leetcode-2221: triangular sum of arrays
2022-06-24 10:23:00 【Ugly and ugly】
Title Description
I'll give you a subscript from 0 The starting array of integers nums , among nums[i] yes 0 To 9 Between ( Both contain ) A number of .
nums The values of the following elements are the final and subsequent operations :
nums Initially contains n Elements . If n == 1 , Termination operation . otherwise , Create a new subscript from 0 The starting length is n - 1 Array of integers for newNums .
For satisfying 0 <= i < n - 1 The subscript i ,newNums[i] The assignment is (nums[i] + nums[i+1]) % 10 ,% Represents the remainder operation .
take newNums Replace array nums .
From step 1 Start repeating the whole process .
Please return nums Triangle and .
Example
Example 1:
Input :nums = [1,2,3,4,5]
Output :8
explain :
The above figure shows the process of obtaining the triangular sum of arrays .
Example 2:
Input :nums = [5]
Output :5
explain :
because nums There is only one element in , The triangle sum of the array is itself for this element .
The problem solving process
Ideas and steps
(1) The solution of double-layer circular violence , Or it can be solved by adding dynamic programming ;
(2) Pay attention to the inner layer when circulating j The cycle begins with 0 Start , To nums.length - i end , Because each cycle is one less number than the last time ;
(3) Finally, the last row of the array is returned 1 Just one element .
Code display
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;
}
}
// Print 2D array
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));
}
}
边栏推荐
- np.float32()
- Recursive traversal of 414 binary tree
- Phpstrom code formatting settings
- Resolved: methods with the same name as their class will not be constructors in
- 415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)
- 时尚的弹出模态登录注册窗口
- tf.errors
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- 包装类型的缓存机制
- p5.js千纸鹤动画背景js特效
猜你喜欢

Leetcode-498: diagonal traversal

形状变化loader加载jsjs特效代码

uniapp实现点击拨打电话功能

简单的价格表样式代码

leetCode-223: 矩形面积

4.分类管理业务开发

numpy. linspace()

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

Yolov6: the fast and accurate target detection framework is open source

分布式事务原理以及解决分布式事务方案
随机推荐
Three ways to use applicationcontextinitializer
Engine localization adaptation & Reconstruction notes
tf.errors
4. classification management business development
Leetcode-929: unique email address
1. project environment construction
一群骷髅在飞canvas动画js特效
Record the range of data that MySQL update will lock
Groovy obtains Jenkins credentials through withcredentials
leetCode-1823: 找出游戏的获胜者
Distributed | how to make "secret calls" with dble
牛客-TOP101-BM29
uniapp实现禁止video拖拽快进
卷妹带你学jdbc---2天冲刺Day1
uniapp 开发微信公众号,下拉框默认选中列表第一个
Status of the thread pool
leetCode-1089: 复写零
Resolved: methods with the same name as their class will not be constructors in
正规方程、、、
SQL sever基本数据类型详解
Input :nums = [1,2,3,4,5]