Jerry's Blog

Recording what I learned everyday

View on GitHub


22 October 2019

LeetCode(45) -- 985, 989,

by Jerry Zhang

P985. Sum of Even Numbers After Queries (Easy)

一个整数数组A,还有一组query。每个query都是由一个长度为2的小数组构成,第一个数是要加到A里的值,第二个数是A的索引。问每次相加之后,A中所有偶数的和是多少

我的思路

把相加前和相加后,奇数偶数所有的情况分析清楚即可。

我的代码

public class E_985_SumofEvenNumbersAfterQueries {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int evenSum = 0;
        for (int i : A) {
            if ((i & 1) == 0) {
                evenSum += i;
            }
        }
        int[] ans = new int[queries.length];
        int i = 0;
        for (int[] query : queries) {
            if ((A[query[1]] & 1) == 0 && (query[0] & 1) == 0) {
                evenSum += query[0];
            } else if ((A[query[1]] & 1) == 0 && (query[0] & 1) == 1) {
                evenSum -= A[query[1]];
            } else if ((A[query[1]] & 1) == 1 && (query[0] & 1) == 1) {
                evenSum += A[query[1]] + query[0];
            }
            A[query[1]] += query[0];
            ans[i] = evenSum;
            i++;
        }
        return ans;
    }
}

99%

最优解

class Solution {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int[] answer = new int[queries.length];
        int even_sum = 0;
        for(int c: A) {
            even_sum += (c & 1 ^ 1) * c;
        }

        for(int i = 0; i < queries.length; i++) {

            int val = queries[i][0];
            int index = queries[i][1];

            even_sum -= A[index] * (A[index] & 1 ^ 1);
            A[index] += val;
            even_sum += A[index] * (A[index] & 1 ^ 1);

            answer[i] = even_sum;
        }
        return answer;
    }
}

学习even_sum += (c & 1 ^ 1) * c; 把除以2的余数跟1做XOR,这样就能使偶数时得1,奇数时得0。这个因子再乘以本身,就不需要再提前判断一次了。

P989. Add to Array-Form of Integer (Easy)

一个整数,用单个数字的一个数组来表示。另给一个整数K。求X + K后得到的数字数组。

我的思路

按一般的加法法则实现。

我的代码

public class E_989_AddtoArrayFormofInteger {
    public List<Integer> addToArrayForm(int[] A, int K) {
        int carry = 0, sum = 0;
        for (int i = A.length - 1; i >= 0; i--) {
            if (carry != 0 || K > 0) {
                sum = A[i] + K % 10 + carry;
                A[i] = sum % 10;
                if (sum >= 10) {
                    carry = 1;
                } else {
                    carry = 0;
                }
                K /= 10;
            }
        }
        LinkedList<Integer> ans = new LinkedList<>();
        while (carry != 0 || K > 0) {
            sum = K % 10 + carry;
            ans.addFirst(sum % 10);
            if (sum >= 10) {
                carry = 1;
            } else {
                carry = 0;
            }
            K /= 10;
        }
        for (int i : A) {
            ans.add(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] test = {0};
        System.out.println(new E_989_AddtoArrayFormofInteger().addToArrayForm(test, 23));
    }
}

99%

最优解

class Solution {
    public List<Integer> addToArrayForm(int[] A, int K) {
        for(int i = A.length-1;i>=0 && K!=0;i--){
            int temp = A[i] + K;
            A[i] = temp%10;
            K = temp/10;
        }

        List<Integer> B = new ArrayList<>();
        while(K!=0){
            B.add(0,K%10);
            K=K/10;
        }

        for(int i:A){
            B.add(i);
        }

        return B;
    }
}
tags: LeetCode