목록분류 전체보기 (166)
공부기록
문제 https://leetcode.com/problems/unique-binary-search-trees/ 내 코드 import java.util.*; class Solution { int dp[]; public int numTrees(int n) { dp = new int[20]; for(int i=0; i
문제 https://leetcode.com/problems/validate-binary-search-tree/ 코드 dfs import java.util.*; /** * 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 {..
문제 https://leetcode.com/problems/valid-anagram/ 코드 import java.util.*; class Solution { public boolean isAnagram(String s, String t) { Map sm = new HashMap(); Map tm = new HashMap(); for(char c : s.toCharArray()){ if(sm.get(c) == null) sm.put(c, 0); sm.replace(c, sm.get(c)+1); } for(char c : t.toCharArray()){ if(tm.get(c) == null) tm.put(c, 0); tm.replace(c, tm.get(c)+1); } for(char key : sm.key..
문제 https://leetcode.com/problems/best-team-with-no-conflicts/ 내 코드 import java.util.*; class Solution { class Node implements Comparable{ int age; int score; public Node(int age, int score){ this.age = age; this.score = score; } @Override public int compareTo(Node obj){ if(this.score > obj.score) return 1; else if(this.score obj.age) return 1; else if(t..
문제 https://leetcode.com/problems/happy-number/ 내 코드 1차 코드 (시간복잡도 O(N), 공간복잡도 O(N)) import java.util.*; class Solution { public int makeSum(int n){ int tmp = n; int sum = 0; while(tmp != 0){ int rem = tmp%10; sum += (rem*rem); tmp/=10; } return sum; } public boolean isHappy(int n) { Set s = new HashSet(); int curNum = n; while(true){ if(s.contains(curNum)) break; s.add(curNum); curNum = makeSum(c..
select format('%03d', 8); 과 같은 쿼리가 있으면 df = pandas.read_sql(query, engine) 했을 때, 오류가 난다. TypeError: %d format: a number is required, not dict % 문자가 escaping이 안되고 인자로 받아져서 일어나는 문제인 것 같다. 이럴 땐 먼저 sqlalchemy의 text를 사용해주면 된다. from sqlalchemy import text ... df = pandas.read_sql(text(query), engine) 그럼 잘 된다.
티켓 판매 어플리케이션 구현하기 티켓 판매 어플리케이션을 구현하기 위한 다음과 같은 코드들이 있다. # 영화관 초대장 클래스이다. 초대장이 있으면 티켓이 무료이다. public class Invitation{ private LocalDateTime when; } # 티켓 클래스이다. public class Ticket{ private Long fee; public Long getFee(){ return fee; } } # 돈, 초대장, 티켓을 담는 가방이다. public class Bag{ private Long amount; private Invitation invitation; private Ticket ticket; public Bag(long amount){ this(null, amount); }..
ITEM 2 : 생성자에 매개변수가 많다면 빌더를 고려하라 객체의 생성자 패턴에는 먼저 점층적 생성자 패턴, 자바빈즈 패턴이 있었다. 먼저 점층적 생성자 패턴이다. 생성자를 필수 매개변수를 받는 생성자부터 시작해서 선택 매개변수 1개 ... N개 까지 생성자를 늘려가는 식이다. class Example1{ private final int a; private final int b; private final int c; public Example(int a){ this(a,0,0); } public Example(int a, int b){ this(a,b,0); } public Example(int a, int b, int c){ this.a = a; this.b = b; this.c = c; } } 이 패..