<?php
class Solution
{
/**
* @param Integer[] $nums
* @return Integer
*/
public function triangularSum(array $nums): int
{
while (count($nums) > 1) {
$newNums = [];
for ($i = 0; $i < count($nums)-1; $i++) {
$newNums[] = ($nums[$i] + $nums[$i+1]) % 10;
}
$nums = $newNums;
}
return $nums[0];
}
/**
* @param Integer[] $nums
* @return Integer
*/
public function triangularSum2(array $nums): int
{
for ($n = count($nums) - 1; $n > 0; $n--) {
for ($i = 0; $i < $n; $i++) {
$nums[$i] = ($nums[$i] + $nums[$i+1]) % 10;
}
}
return $nums[0];
}
}
$solution = new Solution();
var_dump($solution->triangularSum([1, 2, 3, 4, 5]));
var_dump($solution->triangularSum2([1, 2, 3, 4, 5]));
力扣2221
于 2023-02-08 15:41:40 首次发布