Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
public int maxDepth(TreeNode root) {
return find(0, root);
}
public int find(int depth, TreeNode node) {
if (node == null) {
return depth;
}
return Math.max(find(depth + 1, node.left), find(depth + 1, node.right));
}
머리 아팠다.
오랜만에 재귀로 푸려니까 너무 어려웠다.
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return getMaxDepth(root, 1);
}
public int getMaxDepth(TreeNode node, int ans) {
if (node == null) {
return ans;
}
int leftMaxDepth = 0;
int rightMaxDepth = 0;
if (node.left != null) {
leftMaxDepth = getMaxDepth(node.left, ans + 1);
System.out.println("leftMaxDepth : " + leftMaxDepth);
}
if (node.right != null) {
rightMaxDepth = getMaxDepth(node.right, ans + 1);
System.out.println("rightMaxDepth : " + rightMaxDepth);
}
ans = Math.max(ans, Math.max(leftMaxDepth, rightMaxDepth));
return ans;
}
}
LeetCode 웹 IDE에서 바로 풀었다.
풀 수 있을까? 했는데 두드려보니 풀렸다.
class Solution {
public int maxDepth(TreeNode root) {
return go(root,0,0);
}
private int go(TreeNode node, int depth, int ans) {
if (node == null) {
return depth;
}
ans = Math.max(ans, go(node.left, depth+1, ans));
ans = Math.max(ans, go(node.right, depth+1, ans));
return ans;
}
}
Google Adsense Home에 들어가니
다음과 같은 화면이 나를 반겼다.
바로 업데이트 버튼을 눌렀다.
Google Adsense 가입 시
결제 수단을 설정하지 않았기 때문에 설정을 해준다.
Given a 32-bit signed integer, reverse digits of an integer.
Note:
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1].
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
Input: x = 123
Output: 321
public int reverse(int x) {
if (x == 0) {
return 0;
}
while (true) {
if (x % 10 == 0) {
x /= 10;
} else {
break;
}
}
StringBuilder answer = new StringBuilder();
if (x < 0) {
answer.append("-");
x *= -1;
}
while (x != 0) {
answer.append(x % 10);
x /= 10;
}
int ans;
try {
ans = Integer.parseInt(answer.toString());
} catch (Exception e){
return 0;
}
return ans;
}
Case 1
class Solution {
public int reverse(int x) {
long num = 0;
while (x != 0) {
num = num * 10 + x % 10;
x = x / 10;
}
if (num != (int) num) {
return 0;
}
return (int) num;
}
}
Hibernate가 DB에 날리는 모든 Query를 보여준다.
해당 옵션과 관련해서는 반드시 알아야 할 부분이 있다.
그 부분에 대해서는 Spring Boot SQL Option : ‘show_sql’ Option Deep 하게 알아보기 글을 참고하자.
application.yml
spring:
jpa:
properties:
hibernate:
show_sql: true
application.properties
spring.jpa.properties.hibernate.show_sql = true
Output
Hibernate: select testentity0_.id as id1_8_0_ from test_entity testentity0_ where testentity0_.id=?
Hibernate: insert into test_entity (id) values (?)
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
정말 오랜만에 Algorithm을 푸려니까 머리가 돌아가지 않는다.
꾸준히 풀어줘야겠다.