Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array,
you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
public void reverseString(char[] s) {
int length = s.length;
char[] ans = new char[length];
for (int i = 0; i < length; i++) {
ans[i] = s[length - 1 - i];
}
for (int i = 0; i < length; i++) {
s[i] = ans[i];
}
}
막 풀어도 될까 했는데 풀리는구나.
그래도 좋은 코드 참고하자.
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 (?)