Skip to content

Commit 706f738

Browse files
committed
add: product of array except self solution
1 parent 2fae935 commit 706f738

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
정수 배열 nums가 주어질 때 answer[i]가 nums[i]를 제외한 나머지 요소의 곱인 answer 배열을 반환하시오.
3+
*/
4+
public class Solution {
5+
6+
/** 시간복잡도 O(n) */
7+
public int[] productExceptSelf(int[] nums) {
8+
9+
int[] answer = new int[nums.length];
10+
11+
int start = 1;
12+
for (int i = 0; i < nums.length; i++) {
13+
answer[i] = start;
14+
start *= nums[i];
15+
}
16+
17+
int end = 1;
18+
for (int i = nums.length - 1; i >= 0; i--) {
19+
answer[i] *= end;
20+
end *= nums[i];
21+
}
22+
23+
return answer;
24+
}
25+
}
26+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
/**
17+
이진 트리의 root가 주어졌을 때 유효한 이진 탐색 트리인지 검증하세요.
18+
*/
19+
class Solution {
20+
21+
/** 시간복잡도 O(n) */
22+
public boolean isValidBST(TreeNode root) {
23+
return isValidRange(root, Long.MIN_VALUE, Long.MAX_VALUE);
24+
}
25+
26+
private boolean isValidRange(TreeNode root, long min, long max) {
27+
if (root == null) {
28+
return true;
29+
}
30+
if (root.val <= min || root.val >= max) {
31+
return false;
32+
}
33+
return isValidRange(root.left, min, root.val) && isValidRange(root.right, root.val, max);
34+
}
35+
}
36+

0 commit comments

Comments
 (0)