129.Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

Read More

38.Count and Say

The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, …

Read More

371.Sum of Two Integers

Leetcode

Read More

359.Logger Rate Limiter

Design a logger system that receive stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds.

Read More

057.Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

Read More

200.Number of Islands

Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Read More

146.LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

Read More

111.Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.

Read More

70.Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.

Read More

66.Plus One

Given a non-negative number represented as an array of digits, plus one to the number.

Read More

345.Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string.

Read More

344.Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = “hello”, return “olleh”.

Read More

SmallestAdjacent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class SmallestAdjacent {

// first version, convert int x to a string
public static int solutionString(int x){
String input = String.valueOf(x);
int min = Integer.MAX_VALUE;

for (int i = 0; i < input.length() - 1; i++) {
int index = (input.charAt(i) - '0' < input.charAt(i + 1) - '0') ? i : i + 1;
String result = input.substring(0, index) + input.substring(index + 1);
int res = Integer.parseInt(result);
min = res > min ? min : res;
}
return min;
}

public static void main(String[] args) {
System.out.println(solutionString(233614));
}

// second solution, manipulate of a number
public static int solution(int x){
int min = Integer.MAX_VALUE;

// copy the digits to an array
ArrayList<Integer> digits = new ArrayList<>();
int input = x;
while (input > 0) {
digits.add(input % 10);
input /= 10;
}

// for each pair of adjacent digits
for (int i = 0; i < digits.size() - 1; i++) {
int removedIndex = digits.get(i) > digits.get(i + 1) ? i + 1 : i;
int replacedInt = reconstruct(x, removedIndex);
min = replacedInt > min ? min : replacedInt;
}

return min;
}

private static int reconstruct(int x, int removedIndex) {
// this will not overflow since it must be less than the given integer x
int afterPower = (int) Math.pow(10, removedIndex);
// concatenate the digits before and after the removed digit
return x / afterPower / 10 * (int) afterPower + x % afterPower;
}
}

Read More

388.Longest Absolute File Path

O(n) time O(n) space stack solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
public int lengthLongestPath(String input) {
String[] lines = input.split("\n");
Stack<Integer> path = new Stack<>();
path.push(0);
int max = 0;

for (String line : lines) {
int tabIndex = line.lastIndexOf("\t") + 1;
while (path.size() - 1 > tabIndex) {
path.pop();
}
int length = path.peek() + 1 + line.length() - tabIndex;
path.push(length);
if (line.indexOf(".") != -1)
max = Math.max(max, length - 1);
}

return max;
}
}

Read More

377.Combination Sum IV

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Read More