2021.03.01 연습

 

  • 사용언어 : java

 


class Solution {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        
        int count = 0;
        
        for (List<String> i : items) {
            
            if (ruleKey.equals("type") && ruleValue.equals(i.get(0))) { count++; }
            if (ruleKey.equals("color") && ruleValue.equals(i.get(1))) { count++; }
            if (ruleKey.equals("name") && ruleValue.equals(i.get(2))) { count++; }
            
        }
        
        return count;
        
    }
}

Runtime: 5 ms, faster than 33.33% of Java online submissions for Count Items Matching a Rule. Memory Usage: 51.8 MB, less than 16.67% of Java online submissions for Count Items Matching a Rule.



class Solution {
    public int[][] flipAndInvertImage(int[][] image) {
        
        for (int i = 0; i < image.length; i ++) {
            
            for (int j = 0; j < image[i].length; j ++) {  
                if (image[i][j] == 0) {
                    image[i][j] = 1;
                } else {
                    image[i][j] = 0;
                }
            }
            
            for (int k = 0; k < image[i].length/2; k ++) {  
                int temp = image[i][k];
                image[i][k] = image[i][image[i].length -k - 1];
                image[i][image[i].length -k - 1] = temp;
            }
            
        }
        
        return image;
        
    }
}

Runtime: 1 ms, faster than 24.69% of Java online submissions for Flipping an Image. Memory Usage: 38.8 MB, less than 94.72% of Java online submissions for Flipping an Image.

  • 다른 코드 참고

class Solution {
    public int[][] flipAndInvertImage(int[][] a) {
        for(int j=0;j<a.length;j++){
        for(int i=0;i<(a[j].length+1)/2;i++){
            int temp = a[j][i];
            a[j][i]=(a[j][a[j].length-1-i]^1);
            a[j][a[j].length-1-i]=(temp^1);
        }
        }
        return a;
    }
}