Mock Interview: Facebook

JJ·2021년 7월 5일
0

MockTest

목록 보기
45/60

824. Goat Latin

class Solution {
    public String toGoatLatin(String sentence) {
        String[] words = sentence.split("\\s+");
        
        Set<Character> vowel = new HashSet<Character>();
        vowel.add('a');
        vowel.add('i');
        vowel.add('e');
        vowel.add('o');
        vowel.add('u');
        vowel.add('A');
        vowel.add('I');
        vowel.add('E');
        vowel.add('O');
        vowel.add('U');
        
        StringBuilder result = new StringBuilder();
        
        for (int i = 0; i < words.length; i++) {
            StringBuilder sb = new StringBuilder();
        
            if (vowel.contains(words[i].charAt(0))) {
                sb.append(words[i]);
                sb.append("ma");
            } else {
                String rw = words[i].substring(1) + words[i].charAt(0);
                sb.append(rw);
                sb.append("ma");
            }
            
            for (int j = 0; j < i + 1; j++) {
                sb.append("a");
            }
            
            result.append(sb.toString());
            result.append(" ");
            
            
        }
        
        return result.toString().substring(0, result.length() - 1);
    }
    
    
}

Runtime: 10 ms, faster than 26.89% of Java online submissions for Goat Latin.
Memory Usage: 39.5 MB, less than 19.63% of Java online submissions for Goat Latin.

착한 아이처럼~
시키는 대로 했읍니다

166. Fraction to Recurring Decimal

class Solution {
    public String fractionToDecimal(int numerator, int denominator) {
        Set<Integer> num = new HashSet<Integer>();
        StringBuilder result = new StringBuilder();
        int val = 0;
        int remainder = 
        while (! num.contains())
        
    }
}

뭔가 하다가 화장실을 다녀왔더니 끝나있었어요^^
생각한거는 쉬셋이를 만들어서 똑같은 값 나오거나 0이 나올때까지 계속 돌려주는 방식을 ㅅ각했읍니다

public String fractionToDecimal(int numerator, int denominator) {
    if (numerator == 0) {
        return "0";
    }
    StringBuilder fraction = new StringBuilder();
    // If either one is negative (not both)
    if (numerator < 0 ^ denominator < 0) {
        fraction.append("-");
    }
    // Convert to Long or else abs(-2147483648) overflows
    long dividend = Math.abs(Long.valueOf(numerator));
    long divisor = Math.abs(Long.valueOf(denominator));
    fraction.append(String.valueOf(dividend / divisor));
    long remainder = dividend % divisor;
    if (remainder == 0) {
        return fraction.toString();
    }
    fraction.append(".");
    Map<Long, Integer> map = new HashMap<>();
    while (remainder != 0) {
        if (map.containsKey(remainder)) {
            fraction.insert(map.get(remainder), "(");
            fraction.append(")");
            break;
        }
        map.put(remainder, fraction.length());
        remainder *= 10;
        fraction.append(String.valueOf(remainder / divisor));
        remainder %= divisor;
    }
    return fraction.toString();
}

Runtime: 1 ms, faster than 100.00% of Java online submissions for Fraction to Recurring Decimal.
Memory Usage: 36.4 MB, less than 64.53% of Java online submissions for Fraction to Recurring Decimal.

진짜 더럽게 귀찮은데 대충 방법은 같은 것 같아요..

0개의 댓글