楼主:
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
}
}