Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f014f4a

Browse files
authored
Added tasks 404-406.
1 parent f92f3c4 commit f014f4a

File tree

9 files changed

+245
-0
lines changed

9 files changed

+245
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package g0401_0500.s0404_sum_of_left_leaves;
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
5+
import com_github_leetcode.TreeNode;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/*
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode() {}
16+
* TreeNode(int val) { this.val = val; }
17+
* TreeNode(int val, TreeNode left, TreeNode right) {
18+
* this.val = val;
19+
* this.left = left;
20+
* this.right = right;
21+
* }
22+
* }
23+
*/
24+
public class Solution {
25+
public int sumOfLeftLeaves(TreeNode root) {
26+
List<Integer> arr = new ArrayList<>();
27+
traverse(root, arr);
28+
return getSum(arr);
29+
}
30+
31+
private void traverse(TreeNode root, List<Integer> arr) {
32+
if (root == null) {
33+
return;
34+
}
35+
if (root.left != null && root.left.left == null && root.left.right == null) {
36+
arr.add(root.left.val);
37+
}
38+
traverse(root.left, arr);
39+
traverse(root.right, arr);
40+
}
41+
42+
private int getSum(List<Integer> arr) {
43+
int sum = 0;
44+
for (int i = 0; i < arr.size(); i++) {
45+
sum += arr.get(i);
46+
}
47+
return sum;
48+
}
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
404\. Sum of Left Leaves
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return the sum of all left leaves.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)
10+
11+
**Input:** root = \[3,9,20,null,null,15,7\]
12+
13+
**Output:** 24
14+
15+
**Explanation:** There are two left leaves in the binary tree, with values 9 and 15 respectively.
16+
17+
**Example 2:**
18+
19+
**Input:** root = \[1\]
20+
21+
**Output:** 0
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range `[1, 1000]`.
26+
* `-1000 <= Node.val <= 1000`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0401_0500.s0405_convert_a_number_to_hexadecimal;
2+
3+
// #Easy #Math #Bit_Manipulation
4+
5+
public class Solution {
6+
public String toHex(int num) {
7+
if (num == 0) {
8+
return "0";
9+
}
10+
StringBuilder sb = new StringBuilder();
11+
int x;
12+
while (num != 0) {
13+
x = num & 0xf;
14+
if (x < 10) {
15+
sb.append(x);
16+
} else {
17+
sb.append((char) (x + 87));
18+
}
19+
num = num >>> 4;
20+
}
21+
return sb.reverse().toString();
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
405\. Convert a Number to Hexadecimal
2+
3+
Easy
4+
5+
Given an integer `num`, return _a string representing its hexadecimal representation_. For negative integers, [two’s complement](https://en.wikipedia.org/wiki/Two%27s_complement) method is used.
6+
7+
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
8+
9+
**Note:** You are not allowed to use any built-in library method to directly solve this problem.
10+
11+
**Example 1:**
12+
13+
**Input:** num = 26
14+
15+
**Output:** "1a"
16+
17+
**Example 2:**
18+
19+
**Input:** num = -1
20+
21+
**Output:** "ffffffff"
22+
23+
**Constraints:**
24+
25+
* <code>-2<sup>31</sup> <= num <= 2<sup>31</sup> - 1</code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0406_queue_reconstruction_by_height;
2+
3+
// #Medium #Array #Sorting #Greedy
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public int[][] reconstructQueue(int[][] people) {
11+
Arrays.sort(people, (a, b) -> a[0] != b[0] ? -a[0] + b[0] : a[1] - b[1]);
12+
List<int[]> res = new ArrayList<>();
13+
for (int[] a : people) {
14+
res.add(a[1], a);
15+
}
16+
return res.toArray(new int[people.length][]);
17+
}
18+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
406\. Queue Reconstruction by Height
2+
3+
Medium
4+
5+
You are given an array of people, `people`, which are the attributes of some people in a queue (not necessarily in order). Each <code>people[i] = [h<sub>i</sub>, k<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> person of height <code>h<sub>i</sub></code> with **exactly** <code>k<sub>i</sub></code> other people in front who have a height greater than or equal to <code>h<sub>i</sub></code>.
6+
7+
Reconstruct and return _the queue that is represented by the input array_ `people`. The returned queue should be formatted as an array `queue`, where <code>queue[j] = [h<sub>j</sub>, k<sub>j</sub>]</code> is the attributes of the <code>j<sup>th</sup></code> person in the queue (`queue[0]` is the person at the front of the queue).
8+
9+
**Example 1:**
10+
11+
**Input:** people = \[\[7,0\],\[4,4\],\[7,1\],\[5,0\],\[6,1\],\[5,2\]\]
12+
13+
**Output:** \[\[5,0\],\[7,0\],\[5,2\],\[6,1\],\[4,4\],\[7,1\]\]
14+
15+
**Explanation:**
16+
17+
Person 0 has height 5 with no other people taller or the same height in front.
18+
Person 1 has height 7 with no other people taller or the same height in front.
19+
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
20+
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
21+
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
22+
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
23+
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.
24+
25+
**Example 2:**
26+
27+
**Input:** people = \[\[6,0\],\[5,0\],\[4,0\],\[3,2\],\[2,2\],\[1,4\]\]
28+
29+
**Output:** \[\[4,0\],\[5,0\],\[2,2\],\[3,2\],\[1,4\],\[6,0\]\]
30+
31+
**Constraints:**
32+
33+
* `1 <= people.length <= 2000`
34+
* <code>0 <= h<sub>i</sub> <= 10<sup>6</sup></code>
35+
* <code>0 <= k<sub>i</sub> < people.length</code>
36+
* It is guaranteed that the queue can be reconstructed.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0401_0500.s0404_sum_of_left_leaves;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import com_github_leetcode.TreeNode;
7+
import java.util.Arrays;
8+
import org.junit.jupiter.api.Test;
9+
10+
class SolutionTest {
11+
@Test
12+
void sumOfLeftLeaves() {
13+
assertThat(
14+
new Solution()
15+
.sumOfLeftLeaves(
16+
TreeNode.create(Arrays.asList(3, 9, 20, null, null, 15, 7))),
17+
equalTo(24));
18+
}
19+
20+
@Test
21+
void sumOfLeftLeaves2() {
22+
assertThat(new Solution().sumOfLeftLeaves(TreeNode.create(Arrays.asList(1))), equalTo(0));
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0405_convert_a_number_to_hexadecimal;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void toHex() {
11+
assertThat(new Solution().toHex(26), equalTo("1a"));
12+
}
13+
14+
@Test
15+
void toHex2() {
16+
assertThat(new Solution().toHex(-1), equalTo("ffffffff"));
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0401_0500.s0406_queue_reconstruction_by_height;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void reconstructQueue() {
11+
assertThat(
12+
new Solution()
13+
.reconstructQueue(
14+
new int[][] {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}}),
15+
equalTo(new int[][] {{5, 0}, {7, 0}, {5, 2}, {6, 1}, {4, 4}, {7, 1}}));
16+
}
17+
18+
@Test
19+
void reconstructQueue2() {
20+
assertThat(
21+
new Solution()
22+
.reconstructQueue(
23+
new int[][] {{6, 0}, {5, 0}, {4, 0}, {3, 2}, {2, 2}, {1, 4}}),
24+
equalTo(new int[][] {{4, 0}, {5, 0}, {2, 2}, {3, 2}, {1, 4}, {6, 0}}));
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /