首页 > 其他分享 >[LeetCode] 2221. Find Triangular Sum of an Array

[LeetCode] 2221. Find Triangular Sum of an Array

时间:2022-11-20 14:23:57浏览次数:73  
标签:newNums triangular nums int sum Triangular LeetCode array Sum

You are given a 0-indexed integer array nums, where nums[i] is a digit between 0 and 9 (inclusive).

The triangular sum of nums is the value of the only element present in nums after the following process terminates:

  1. Let nums comprise of n elements. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n - 1.
  2. For each index i, where 0 <= i < n - 1, assign the value of newNums[i] as (nums[i] + nums[i+1]) % 10, where % denotes modulo operator.
  3. Replace the array nums with newNums.
  4. Repeat the entire process starting from step 1.

Return the triangular sum of nums.

Example 1:

Input: nums = [1,2,3,4,5]
Output: 8
Explanation:
The above diagram depicts the process from which we obtain the triangular sum of the array.

Example 2:

Input: nums = [5]
Output: 5
Explanation:
Since there is only one element in nums, the triangular sum is the value of that element itself.

Constraints:

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 9

数组的三角和。

给你一个下标从 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 的三角和。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-triangular-sum-of-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题是关于帕斯卡三角,类似118,119题。遇到这一类的题,基本思路还是把三角形转换成直角三角形做。这道题唯一需要注意的是计算结果需要对 10 取模。

时间O(n^2)

空间O(1) - 直接修改 input 数组

Java实现

 1 class Solution {
 2     public int triangularSum(int[] nums) {
 3         int len = nums.length;
 4         for (int i = len; i > 0; i--) {
 5             for (int j = 1; j < i; j++) {
 6                 nums[j - 1] += nums[j];
 7                 nums[j - 1] %= 10;
 8             }
 9         }
10         return nums[0];
11     }
12 }

 

相关题目

118. Pascal's Triangle

119. Pascal's Triangle II

120. Triangle

799. Champagne Tower

2221. Find Triangular Sum of an Array

LeetCode 题目总结

标签:newNums,triangular,nums,int,sum,Triangular,LeetCode,array,Sum
From: https://www.cnblogs.com/cnoodle/p/16908395.html

相关文章

  • Hitachi2020E Odd Sum Rectangles
    Hitachi2020E看到\(\oplus\)操作和\(2^n-1\)时猜结论\(ans_{i,j}=\operatorname{popcount}(i\&j)\bmod2\),过不去,然后就不会了。然而令答案的二维前缀和为\(\opera......
  • Leetcode 799.香槟塔:动态规划+递归
    香槟塔:动态规划+递归题目来源:Leetcode22/11/20每日一题:799.香槟塔https://leetcode.cn/problems/champagne-tower我们把玻璃杯摆成金字塔的形状,其中第一层有1个玻......
  • leetcode-1380-easy
    LuckyNumbersinaMatrixGivenanmxnmatrixofdistinctnumbers,returnallluckynumbersinthematrixinanyorder.Aluckynumberisanelementofthe......
  • leetcode-507-easy
    PerfectNumberAperfectnumberisapositiveintegerthatisequaltothesumofitspositivedivisors,excludingthenumberitself.Adivisorofanintegerx......
  • leetcode-1403-easy
    MinimumSubsequenceinNo-IncreasingOrderGiventhearraynums,obtainasubsequenceofthearraywhosesumofelementsisstrictlygreaterthanthesumofth......
  • leetcode-506-easy
    RelativeRanksYouaregivenanintegerarrayscoreofsizen,wherescore[i]isthescoreoftheithathleteinacompetition.Allthescoresareguaranteedt......
  • 代码随想录day4---LeetCode24. 两两交换链表中的节点&19.删除链表的倒数第N个节点&面
    LeetCode24.两两交换链表中的节点题目链接给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节......
  • XOR Sum of Arrays
    section>ProblemStatementForsequences$B=(B_1,B_2,\dots,B_M)$and$C=(C_1,C_2,\dots,C_M)$,eachoflength$M$,consistingofnon-negativeintegers,lettheX......
  • C. Sum of Substrings题解
    C.SumofSubstrings题目大概意思,给你一个01串,求和最小,其中和是该串所有相邻字符所组成的十进制数的和。如:0110,sum=01+11+10=22。通过观察我们可以发现,除了第......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:2 的幂
    题目:给你一个整数n,请你判断该整数是否是2的幂次方。如果是,返回true;否则,返回false。如果存在一个整数x使得 n==2x,则认为n是2的幂次方。 示例1:输入:n=1输......