作者:
yam276 ('_')
2025-03-05 18:55:44https://leetcode.com/problems/count-total-number-of-colored-cells/
2579. Count Total Number of Colored Cells
格子每次會從自己四角沒格子的地方長出新格子
求第n次總共多少格子
Solution:
n=1 1 1
n=2 5 1+4
n=3 13 1+4+8
n=4 25 1+4+8+12
讓程式實際模擬不實際
連return都給你i64就知道了
所以找規律然後這樣這樣內樣內樣
可以得到: 1 + 4 * (n-1)n/2
乘開後變成: n^2 + (n-1)^2
Code:
impl Solution {
pub fn colored_cells(n: i32) -> i64 {
let n64 = n as i64;
let result = n64 * n64 + (n64 - 1) * (n64 - 1);
result
}
}