📜  hackerrank 对角线差 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:13.990000             🧑  作者: Mango

代码示例1
var right int32
    var left int32

    for i := 0; i < len(arr); i++ {
        for j := 0; j < len(arr); j++ {
            if i == j {
                right += int32(arr[i][j])
            }

            if i+j == len(arr)-1 {
                left += int32(arr[i][j])
            }
        }
    }

    sum := right - left

    if sum < 0 {
        sum -= sum
    }

    return sum
   }
   
# step one
0 == 0 = 11 // true
1 == 0 = 2
2 == 0 = 4
0 == 1 = 4
1 == 1 = 5 // true
2 == 1 = 6
0 == 2 = 10
1 == 2 = 8
2 == 2 = -12 // true

= 11 + 5 - 12 = 4

# step two
0 == 2 = 11
1 == 2 = 2
2 == 2 = 4 // true
1 == 2 = 4
2 == 2 = 5 // true
3 == 2 = 6
2 == 2 = 10 // t rue
3 == 2 = 8
4 == 2 = 12

= 4 + 5 + 10 = 19