Java, use Integer[][] to store values


  • 0
    M
    class Solution {
        public int pathSum(int[] nums) {
            Integer[][] A = new Integer[4][8];
            for(int e : nums) {
                A[e / 100 - 1][(e % 100) / 10 - 1] = new Integer(e % 10);
            }
            int res = 0;
            for(int i = 0; i < 4; i++) {
                for(int j = 0; j < 8; j++) {
                    if(A[i][j] == null) {
                        continue;
                    }
                    if(i > 0) {
                        A[i][j] += A[i - 1][j / 2];
                    }
                    
                    if(i == 3) {
                        res += A[i][j];
                    } else if(A[i + 1][j * 2] == null && A[i + 1][j * 2 + 1] == null) {
                        res += A[i][j];
                    }
                }
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.