Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
201\. Bitwise AND of Numbers Range

Medium

Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.

**Example 1:**

**Input:** left = 5, right = 7

**Output:** 4

**Example 2:**

**Input:** left = 0, right = 0

**Output:** 0

**Example 3:**

**Input:** left = 1, right = 2147483647

**Output:** 0

**Constraints:**

* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
39 changes: 39 additions & 0 deletions src/main/java/g0201_0300/s0202_happy_number/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
202\. Happy Number

Easy

Write an algorithm to determine if a number `n` is happy.

A **happy number** is a number defined by the following process:

* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
* Those numbers for which this process **ends in 1** are happy.

Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.

**Example 1:**

**Input:** n = 19

**Output:** true

**Explanation:**

1<sup>2</sup> + 9<sup>2</sup> = 82

8<sup>2</sup> + 2<sup>2</sup> = 68

6<sup>2</sup> + 8<sup>2</sup> = 100

1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1

**Example 2:**

**Input:** n = 2

**Output:** false

**Constraints:**

* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
203\. Remove Linked List Elements

Easy

Given the `head` of a linked list and an integer `val`, remove all the nodes of the linked list that has `Node.val == val`, and return _the new head_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/06/removelinked-list.jpg)

**Input:** head = \[1,2,6,3,4,5,6\], val = 6

**Output:** \[1,2,3,4,5\]

**Example 2:**

**Input:** head = \[\], val = 1

**Output:** \[\]

**Example 3:**

**Input:** head = \[7,7,7,7\], val = 7

**Output:** \[\]

**Constraints:**

* The number of nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
* `1 <= Node.val <= 50`
* `0 <= val <= 50`
29 changes: 29 additions & 0 deletions src/main/java/g0201_0300/s0204_count_primes/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
204\. Count Primes

Medium

Given an integer `n`, return _the number of prime numbers that are strictly less than_ `n`.

**Example 1:**

**Input:** n = 10

**Output:** 4

**Explanation:** There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

**Example 2:**

**Input:** n = 0

**Output:** 0

**Example 3:**

**Input:** n = 1

**Output:** 0

**Constraints:**

* <code>0 <= n <= 5 * 10<sup>6</sup></code>
33 changes: 33 additions & 0 deletions src/main/java/g0201_0300/s0205_isomorphic_strings/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
205\. Isomorphic Strings

Easy

Given two strings `s` and `t`, _determine if they are isomorphic_.

Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

**Example 1:**

**Input:** s = "egg", t = "add"

**Output:** true

**Example 2:**

**Input:** s = "foo", t = "bar"

**Output:** false

**Example 3:**

**Input:** s = "paper", t = "title"

**Output:** true

**Constraints:**

* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
* `t.length == s.length`
* `s` and `t` consist of any valid ascii character.
34 changes: 34 additions & 0 deletions src/main/java/g0201_0300/s0206_reverse_linked_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
206\. Reverse Linked List

Easy

Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)

**Input:** head = \[1,2,3,4,5\]

**Output:** \[5,4,3,2,1\]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)

**Input:** head = \[1,2\]

**Output:** \[2,1\]

**Example 3:**

**Input:** head = \[\]

**Output:** \[\]

**Constraints:**

* The number of nodes in the list is the range `[0, 5000]`.
* `-5000 <= Node.val <= 5000`

**Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?
33 changes: 33 additions & 0 deletions src/main/java/g0201_0300/s0207_course_schedule/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
207\. Course Schedule

Medium

There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.

* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.

Return `true` if you can finish all courses. Otherwise, return `false`.

**Example 1:**

**Input:** numCourses = 2, prerequisites = \[\[1,0\]\]

**Output:** true

**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

**Example 2:**

**Input:** numCourses = 2, prerequisites = \[\[1,0\],\[0,1\]\]

**Output:** false

**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

**Constraints:**

* <code>1 <= numCourses <= 10<sup>5</sup></code>
* `0 <= prerequisites.length <= 5000`
* `prerequisites[i].length == 2`
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
* All the pairs prerequisites\[i\] are **unique**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
208\. Implement Trie (Prefix Tree)

Medium

A [**trie**](https://en.wikipedia.org/wiki/Trie) (pronounced as "try") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.

Implement the Trie class:

* `Trie()` Initializes the trie object.
* `void insert(String word)` Inserts the string `word` into the trie.
* `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise.
* `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise.

**Example 1:**

**Input**

["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]

**Output:** \[null, null, true, false, true, null, true\]

**Explanation:**

Trie trie = new Trie();
trie.insert("apple"); trie.search("apple"); // return True
trie.search("app"); // return False
trie.startsWith("app"); // return True
trie.insert("app");
trie.search("app"); // return True

**Constraints:**

* `1 <= word.length, prefix.length <= 2000`
* `word` and `prefix` consist only of lowercase English letters.
* At most <code>3 * 10<sup>4</sup></code> calls **in total** will be made to `insert`, `search`, and `startsWith`.
33 changes: 33 additions & 0 deletions src/main/java/g0201_0300/s0209_minimum_size_subarray_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
209\. Minimum Size Subarray Sum

Medium

Given an array of positive integers `nums` and a positive integer `target`, return the minimal length of a **contiguous subarray** <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> of which the sum is greater than or equal to `target`. If there is no such subarray, return `0` instead.

**Example 1:**

**Input:** target = 7, nums = \[2,3,1,2,4,3\]

**Output:** 2

**Explanation:** The subarray \[4,3\] has the minimal length under the problem constraint.

**Example 2:**

**Input:** target = 4, nums = \[1,4,4\]

**Output:** 1

**Example 3:**

**Input:** target = 11, nums = \[1,1,1,1,1,1,1,1\]

**Output:** 0

**Constraints:**

* <code>1 <= target <= 10<sup>9</sup></code>
* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>

**Follow up:** If you have figured out the `O(n)` solution, try coding another solution of which the time complexity is `O(n log(n))`.
18 changes: 12 additions & 6 deletions src/main/java/g0201_0300/s0210_course_schedule_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Medium

There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.

* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.

Expand All @@ -14,15 +14,21 @@ Return _the ordering of courses you should take to finish all courses_. If there

**Output:** \[0,1\]

**Explanation:** There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is \[0,1\].
**Explanation:**

There are a total of 2 courses to take. To take course 1 you should have finished course 0.
So the correct course order is [0,1].

**Example 2:**

**Input:** numCourses = 4, prerequisites = \[\[1,0\],\[2,0\],\[3,1\],\[3,2\]\]

**Output:** \[0,2,1,3\]

**Explanation:** There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is \[0,1,2,3\]. Another correct ordering is \[0,2,1,3\].
**Explanation:**

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].

**Example 3:**

Expand All @@ -35,6 +41,6 @@ Return _the ordering of courses you should take to finish all courses_. If there
* `1 <= numCourses <= 2000`
* `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
* `prerequisites[i].length == 2`
* `0 <= ai, bi < numCourses`
* `ai != bi`
* All the pairs `[ai, bi]` are **distinct**.
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
* <code>a<sub>i</sub> != b<sub>i</sub></code>
* All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are **distinct**.
33 changes: 33 additions & 0 deletions src/main/java/g0201_0300/s0273_integer_to_english_words/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
273\. Integer to English Words

Hard

Convert a non-negative integer `num` to its English words representation.

**Example 1:**

**Input:** num = 123

**Output:** "One Hundred Twenty Three"

**Example 2:**

**Input:** num = 12345

**Output:** "Twelve Thousand Three Hundred Forty Five"

**Example 3:**

**Input:** num = 1234567

**Output:** "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

**Example 4:**

**Input:** num = 1234567891

**Output:** "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

**Constraints:**

* <code>0 <= num <= 2<sup>31</sup> - 1</code>