当前位置:网站首页>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));
}
}
边栏推荐
猜你喜欢

4.分类管理业务开发

Normal equation

canvas无限扫描js特效代码

Machine learning - principal component analysis (PCA)

Leetcode-498: diagonal traversal

Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default

413 binary tree Foundation

2. login and exit function development

Groovy obtains Jenkins credentials through withcredentials

使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
随机推荐
6. package management business development
利用pandas读取SQL Sever数据表
Record the range of data that MySQL update will lock
Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
整理接口性能优化技巧,干掉慢代码
PHP encapsulates a file upload class (supports single file and multiple file uploads)
2. login and exit function development
SQL Sever中的窗口函数row_number()rank()dense_rank()
6.套餐管理业务开发
Safety and food security for teachers and students of the trapped Yingxi middle school
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
包装类型的缓存机制
用扫描的方法分发书稿校样
Uniapp implementation forbids video drag fast forward
4.分类管理业务开发
Difference between package type and basic type
leetCode-498: 對角線遍曆
413 binary tree Foundation
TP5 using post to receive array data times variable type error: solution to array error
leetCode-1051: 高度检查器
Input :nums = [1,2,3,4,5]