Minimum Number of Operations to Move All Balls to Each Box

给一个string, 里面是0和1, 给一个操作是移动一个1到相邻位置, cost为1, 求把所有1的位置挪到当前位置的cost.

就是两个循环算一下.

class Solution {
    public int[] minOperations(String boxes) {
        int n = boxes.length();
        int[] res = new int[n];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i == j)
                    continue;
                res[i] += Math.abs(i - j) * (boxes.charAt(j) - '0');
            }
        }
        return res;
    }
}