LeetCode--日常刷题

985. Sum of Even Numbers After Queries

class Solution {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int oddSum=0,evenSum=0;
        for(int i=0;i<A.length;i++){
            if((A[i] & 1)==1)
                oddSum+=A[i];
            else
                evenSum+=A[i];
        }
        int[] ans=new int[queries.length];
        for(int i=0;i<ans.length;i++){
            //A[queries[i][1]]为偶数  queries[i][0]为偶数
            if((A[queries[i][1]] & 1)==0){
                if((queries[i][0] & 1)==0)
                {
                    evenSum+=queries[i][0];
                    ans[i]=evenSum;
                    A[queries[i][1]]+=queries[i][0];
                }
                else   //A[queries[i][1]]为偶数  queries[i][0]为奇数
                {
                    evenSum-=A[queries[i][1]];
                    ans[i]=evenSum;
                    oddSum+=queries[i][0]+A[queries[i][1]];
                    A[queries[i][1]]+=queries[i][0];
                }
            }
            else{
                //A[queries[i][1]]为奇数,queries[i][0]为偶数
                if((queries[i][0] & 1)==0){
                    oddSum+=queries[i][0];
                    A[queries[i][1]]+=queries[i][0];
                    ans[i]=evenSum;
                }
                else  //A[queries[i][1]]为奇数,queries[i][0]为奇数
                {
                    evenSum+=A[queries[i][1]]+queries[i][0];
                    oddSum-=A[queries[i][1]];
                    ans[i]=evenSum;
                    A[queries[i][1]]+=queries[i][0];
                }
            }
        }
        return ans;
    }
}

365. Water and Jug Problem

class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        if(z==0 || z==x || z==y || x+y==z)
            return true;
        if(x+y<z)
            return false;
        int GCD=gcd(x,y);
        return GCD==0?false:z%GCD==0;
        
    }
    
    int gcd(int x,int y){
        return y==0?x:gcd(y,x%y);
    }
}

399. Evaluate Division

class Solution {
    
    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        int n_points = 0;
        HashMap<String, Integer> map = new HashMap<>();
        for (List<String> list : equations) {
            for (String s : list) {
                if (!map.containsKey(s)) {
                    map.put(s, n_points++);
                }
            }
        }
        double[][] adj = new double[n_points][n_points];
        for (int i = 0; i < equations.size(); i++) {

            String start = equations.get(i).get(0);
            int startIndex = map.get(start);
            String end = equations.get(i).get(1);
            int endIndex = map.get(end);
            adj[startIndex][endIndex] = values[i];
            if (values[i] != 0.0)
                adj[endIndex][startIndex] = 1 / values[i];
        }
        double[] ans = new double[queries.size()];
        Arrays.fill(ans, -1.0);

        for (int i = 0; i < ans.length; i++) {
            String a = queries.get(i).get(0);
            String b = queries.get(i).get(1);
            if (!map.containsKey(a) || !map.containsKey(b))
                ans[i] = -1.0;
            else if(a.equals(b)){
                ans[i]=1.0;
            }
            else {
                
                int start = map.get(a), end = map.get(b);
                System.out.println(start+end);
                Queue<double[]> que = new LinkedList<>();
                boolean[] visited = new boolean[n_points + 1];
                for (int k = 0; k < n_points; k++) {
                    if (adj[start][k] != 0.0) {
                        visited[k] = true;
                        System.out.print(k);
                        double[] tmp = {(double) k, adj[start][k]};
                        que.add(tmp);
                    }
                }
                
                while (!que.isEmpty()) {
                    double[] tmp = que.poll();
                    int source = (int) tmp[0];
                    if (source == end) {
                        ans[i] = tmp[1];
                        System.out.println("haha");
                        break;
                    }
                    for (int j = 0; j < n_points; j++) {
                        if (!visited[j] && adj[source][j] != 0.0) {
                            double[] temp = {(double) j, adj[source][j] * tmp[1]};
                            System.out.println("xixi"+adj[source][j] * tmp[1]);
                            que.add(temp);
                            visited[j] = true;
                        }
                    }
                }
            }
        }
        return ans;
    }
}

443. String Compression

class Solution {
    public int compress(char[] chars) {
        int i=0,j=0;
        while(j<chars.length){
            int idx=j;
            char c=chars[idx];
            int count=1;
            while(idx+1<chars.length && chars[idx+1]==c){
                count++;
                idx++;
            }
                
            if(count==1)
            {
                chars[i++]=chars[j];
            }
            else{
                chars[i++]=chars[j];
                String str=String.valueOf(count);
                for(int n=0;n<str.length();n++)
                    chars[i++]=str.charAt(n);
            }
            j=idx+1;
        }
        return i;
    }
}

404. Sum of Left Leaves

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        return dfs(root, null);
    }
    
    private int dfs(TreeNode root, TreeNode parent) {
        if (root == null) return 0;
        if (root.left == null && root.right == null && parent != null && parent.left == root) {
            return root.val;
        }
        return dfs(root.left, root) + dfs(root.right, root); 
    }
}
上一篇:English trip V2 - 12 Movies and Televison Teacher:Corrine Key:


下一篇:图的传播