취중 알고리즘. Microsoft #3 ????????????????

와~
술마셔서 그런지 술술 풀리네요^^
물론 런타임은 노답이라는점~

Flipping an Image

class Solution:
    def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
        result = []
        
        for img in image:
            img.reverse()
            l = []
            for i in img:
                if i == 1:
                    l.append(0)
                else:
                    l.append(1)
            
            result.append(l)
        
        
        return result
        

Runtime: 96 ms, faster than 5.46% of Python3 online submissions for Flipping an Image.
Memory Usage: 14.3 MB, less than 25.64% of Python3 online submissions for Flipping an Image.

Leaf-Similar Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> l1 = new ArrayList<Integer>();
        List<Integer> l2 = new ArrayList<Integer>();
        
        helper(root1, l1);
        helper(root2, l2);
        
        if (l1.equals(l2)) {
            return true;
        } else {
            return false; 
        }
        
        
    }
    
    public void helper(TreeNode root, List<Integer> l) {
        if (root.left == null && root.right == null) {
            l.add(root.val);
        }
        
        if (root.left != null) {
            helper(root.left, l);
        }
        
        if (root.right != null) {
            helper(root.right, l);
        }
    }
}

Runtime: 1 ms, faster than 28.30% of Java online submissions for Leaf-Similar Trees.
Memory Usage: 38.7 MB, less than 15.98% of Java online submissions for Leaf-Similar Trees.

재귀사용

전씨ㅏ 괜히 쉽다고 한게 아니네요..^^
물론 런타임은 개거지라는점~
런타임 빠르게 하면 쉬운게 아닔도..

이거는 빨리 break 해줘버리면 조금 나을수도??
근데 생각하기 싫어요ㅠ

좋은 웹페이지 즐겨찾기