/* 自定義代碼塊樣式 */

herrDeng網內搜尋

自訂搜尋

Ads

2025年9月30日 星期二

C++ Pascal三角與模10算術解Leetcode 2221 Find Triangular Sum of an Array(含Py3 Code)


C++ Pascal三角與模10算術解Leetcode 2221  Find Triangular Sum of an Array
如果採用有支援非常長int的程式語言,mod 10算術就整個省略,就用Pascal三角的性質,可得快速解

----
If a programming language that supports very long ints is used, the mod 10 arithmetic can be omitted entirely, and the properties of Pascal's triangle can be used to obtain a fast solution.
class Solution:
    def triangularSum(self, nums: List[int]) -> int:
        n=len(nums)-1
        ans, A=nums[0], 1
        for k in range(1, n+1):
            A=A*(n-k+1)//k
            ans=(ans+nums[k]*A)%10
        return ans
        

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章