Mock Interview: Apple #2

JJ·2021년 4월 5일
0

MockTest

목록 보기
21/60

Reorder Data in Log Files

오랜만에 싫어요..rg?^^

class Solution {
    public String[] reorderLogFiles(String[] logs) {
        
        //개그지같은 문제.. 너 어디가서 easy라고 하지 마라 ㅡㅡ
        String[] result = new String[logs.length];
        List<String> letter = new ArrayList<String>();
        List<String> digit = new ArrayList<String>();
        
        for (int i = 0; i < logs.length; i++) {
            String[] log = logs[i].split(" ");
            
            if (Character.isDigit(log[1].charAt(0))) {
                digit.add(logs[i]);
            } else {
                letter.add(log[i]);
            }
        }
        
        String[] la = letter.toArray();
        String[] da = digit.toArray();
        
        Arrays.sort(la);
        Arrays.sort(da);
        
        return la + da; //^^
    }
    
    static class reOrderComparator implements Comparator<String> {
        @Override
        
        public int compare(String l1, String l2) {
            String s1 = l1.split(" ", 2);
            String s2 = l2.split(" ", 2);
        }
            
    }
    
}

Comparator 만들다가 포기했읍니다...
Comparator 만드는거 배워야 할 것 같긴 한데ㅠ

class Solution {
    public String[] reorderLogFiles(String[] logs) {

        Comparator<String> myComp = new Comparator<String>() {
            @Override
            public int compare(String log1, String log2) {
                // split each log into two parts: <identifier, content>
                String[] split1 = log1.split(" ", 2);
                String[] split2 = log2.split(" ", 2);

                boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
                boolean isDigit2 = Character.isDigit(split2[1].charAt(0));

                // case 1). both logs are letter-logs
                if (!isDigit1 && !isDigit2) {
                    // first compare the content
                    int cmp = split1[1].compareTo(split2[1]);
                    if (cmp != 0)
                        return cmp;
                    // logs of same content, compare the identifiers
                    return split1[0].compareTo(split2[0]);
                }

                // case 2). one of logs is digit-log
                if (!isDigit1 && isDigit2)
                    // the letter-log comes before digit-logs
                    return -1;
                else if (isDigit1 && !isDigit2)
                    return 1;
                else
                    // case 3). both logs are digit-log
                    return 0;
            }
        };

        Arrays.sort(logs, myComp);
        return logs;
    }
}

compare 만드는법...냅다 외워

   public String[] reorderLogFiles(String[] logs) {
        var letter = new ArrayList<String>();
        var digits = new ArrayList<String>();
        for(int i = 0; i < logs.length; i++) {
            var token = logs[i].split(" ", 2);
            if( Character.isLetter(token[1].charAt(0)) ) {
                letter.add(logs[i]);
            } else {
                digits.add(logs[i]);
            }
        }
        Collections.sort(letter, (a, b) -> {
            var x = a.split(" ", 2);
            var y = b.split(" ", 2);
            var compare = x[1].compareTo(y[1]);
            if(compare == 0) {
                return x[0].compareTo(y[0]);
            } else {
                return compare;
            }
        });
        var result = new String[logs.length];
        var i = 0;
        for(String log : letter) {
            result[i++] = log;
        }
        for(String log : digits) {
            result[i++] = log;
        }
        return result;
    }
	

이렇게 바로 하는 방식도 있음 (minheap과 비슷)

Generate Parenthesis

class Solution {
    private List<String> result;
    public List<String> generateParenthesis(int n) {
        result = new ArrayList<String>();
        
        StringBuilder cur = new StringBuilder();
        
        helper(cur, n, 0, 0);
        
        return result; 
    }
    
    private void helper(StringBuilder cur, int n, int open, int close) {
        if (cur.length() == n * 2) {
            result.add(cur.toString());
            return;
        }
        
        if (open < n) {
            cur.append("(");
            helper(cur, n, open + 1, close);
            cur.deleteCharAt(cur.length() - 1);
        }
        
        if (close < n && close < open) {
            cur.append(")");
            helper(cur, n, open, close + 1);
            cur.deleteCharAt(cur.length() - 1);
        }
    }
}

Runtime: 0 ms, faster than 100.00% of Java online submissions for Generate Parentheses.
Memory Usage: 38.9 MB, less than 77.63% of Java online submissions for Generate Parentheses.

지워주는거 깜박해서 조금 고생한거 뺴고는 풀만했던 문제~~~

0개의 댓글