알고리즘 문제풀이 For Java - 707 스터디일지

스터디일지 : 2021-07-07 목요일

사람들

  • 이번주 주제 : 리트코드
  • 참석인원 : 동, 연, 새리, 시온, 산희 ,케이(사전 순 6명)
  • 진행시간 : 19시~21시(3시간)

알고리즘 문제들

문제풀이 현황판

번호 새리 시온 산희 케이
1 링크 O X O O O O
2 링크 O O O O X O
3 링크 O O O X O O
4 링크 X O O X X O
5 링크 O O O O O O

제출코드

  • 순서대로 새리, 시온, 동, 연, 산희 케이 각각 2문제씩

  • Subdomain Visit Count

class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String, Integer> counter = new HashMap<>();
        for (String domains : cpdomains) {
            int count = Integer.parseInt(domains.split(" ")[0]);
            String domain = domains.split(" ")[1];
            for (int i = domain.length() - 1; i >= 0; i--) {
                if (i == 0 || domain.charAt(i - 1) == '.') {
                    String subDomain = domain.substring(i);
                    if (!counter.containsKey(subDomain)) {
                        counter.put(subDomain, count);
                    } else {
                        counter.replace(subDomain, counter.get(subDomain) + count);
                    }
                }
            }
        }
        List<String> answer = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : counter.entrySet()) {
            answer.add(entry.getValue() + " " + entry.getKey());
        }
        return answer;
    }
}
//2. Middle of the Linked List
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode temp = head;
        int counter = 1;
        int mid = 0;
        while (temp.next != null) {
            counter++;
            temp = temp.next;
        }
        mid = counter / 2;
        if (counter % 2 == 0) {
            mid = counter / 2;
        }
        temp = head;
        while (mid != 0) {
            temp = temp.next;
            mid--;
        }
        return temp;
    }
}
//3. Counting Bits
class Solution {    
        public int[] countBits(int n) {
        int[] answer = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            answer[i] = Integer.bitCount(i);
        }
        return answer;
    }
}
//4. Reverse String
class Solution {
    public void reverseString(char[] s) {
        char temp = ' ';
        int mid = s.length / 2 - 1;
        if (s.length % 2 == 0) {
            mid += 1;
        }
        for (int i = 0; i < s.length / 2; i++) {
            temp = s[i];
            s[i] = s[s.length - i - 1];
            s[s.length - i - 1] = temp;
        }
    }
}
public class Solutaion338 {
    public int[] countBits(int n) {

        int[] answer = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            int getCountOne = getCntOne(i);
            answer[i] = getCountOne;
        }

        return answer;
    }

    private int getCntOne(int i) {
        return getCntOneRecursive(i, 0);
    }

    private int getCntOneRecursive(int i, int cntOne) {
        if (i == 0) {
            return cntOne;
        } else {
            if (i % 2 == 0) {
               return getCntOneRecursive(i / 2, cntOne);
            } else {
               return getCntOneRecursive(i / 2, cntOne + 1);
            }
        }
    }


}
package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution811 {
    public List<String> subdomainVisits(String[] cpdomains) {
        Map<String, Integer> counter = new HashMap<>();

        for(String cur : cpdomains) {
            String[] f1 = cur.split(" ");

            int visitCounter = Integer.parseInt(f1[0]);

            String[] token = f1[1].split(".");
            for(int i=0 ; i<token.length ; i++) {
                String subDo = tokStrSum(token.length, i,token);

                if(counter.containsKey(subDo)) {
                    int temp = counter.get(subDo);
                    counter.put(subDo, temp + visitCounter);
                }else {
                    counter.put(subDo,visitCounter);
                }
            }
        }
        List<String> answer = new ArrayList<>();
        //counter.keySet().toArray()
        //String[] hou = counter.keySet().toArray();

        for(String s : counter.keySet()){
            answer.add(String.format("%d %s",counter.get(s),s));
        }

        //for(int i=0 ; i<hou.length ; i++) {
        //    answer.add(String.format("%d %s",counter.get(hou[i]),hou[i]));
        //}
        return answer;
    }

    private String tokStrSum(int length, int cnt, String[] token) {
        StringBuilder builder = new StringBuilder();
        for(int i=cnt ; i<length ; i++) {
            builder.append(token[i]);
            if(i != length-1) {
                builder.append(".");
            }
        }
        return builder.toString();
    }


    public static void main(String[] args) {
        Solution811 s = new Solution811();

        s.tester(new String[] {"9001 discuss.leetcode.com"},
                new String[] { "9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"
});
    }

    public void tester(String[] in, String[] ans) {
        List<String> response = subdomainVisits(in);
        if(response.toString().equals(Arrays.toString(ans))) {
            System.out.println("OK");
        }
        System.out.println("NG");
        System.out.println("ANSWER : "+Arrays.toString(ans));
        System.out.println("EXPECTED : " + response.toString());

    }
}

package leetcode;

class ListNode {
    int val;
    ListNode next;

    private ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution876 {
    public ListNode middleNode(ListNode head) {
        int len = getlen(head,0);
        return getAnswer(head,len/2);
    }

    private ListNode getAnswer(ListNode next, int tar) {
        if(tar==0) {
            return next;
        }
        else {
            return getAnswer(next.next,tar-1);
        }
    }

    private int getlen(ListNode next,int len) {
        if(next==null) {
            return len;
        }
        else {
            return getlen(next.next,len+1);
        }
    }
}
//876
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode next = head.next;
        int totalLength = 1; // head 있다고 가정
        while (next != null) {
            totalLength++;
            next = next.next;
        }
        if (totalLength == 1) {
            return head;
        }
        next = head.next;
        ListNode middleNode = null;
        int targetLength = totalLength / 2;
        int length = 1;
        while (length != targetLength) {
            next = next.next;
            length++;
        }
        middleNode = next;
        return middleNode;
    }
}
//338
class Solution {
    public int[] countBits(int n) {
        int[] result = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            String binary = Integer.toBinaryString(i);
            binary = binary.replaceAll(“0”, “”);
            result[i] = binary.length();
        }
        return result;
    }
}

댓글을 작성해보세요.

채널톡 아이콘