-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
585 changed files
with
24,787 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# [542. 01 Matrix](https://leetcode.com/problems/01-matrix/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> | ||
|
||
<p>The distance between two adjacent cells is <code>1</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-1-grid.jpg" style="width: 253px; height: 253px;" /> | ||
<pre> | ||
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[0,0,0]] | ||
<strong>Output:</strong> [[0,0,0],[0,1,0],[0,0,0]] | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/01-2-grid.jpg" style="width: 253px; height: 253px;" /> | ||
<pre> | ||
<strong>Input:</strong> mat = [[0,0,0],[0,1,0],[1,1,1]] | ||
<strong>Output:</strong> [[0,0,0],[0,1,0],[1,2,1]] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>m == mat.length</code></li> | ||
<li><code>n == mat[i].length</code></li> | ||
<li><code>1 <= m, n <= 10<sup>4</sup></code></li> | ||
<li><code>1 <= m * n <= 10<sup>4</sup></code></li> | ||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li> | ||
<li>There is at least one <code>0</code> in <code>mat</code>.</li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# [456. 132 Pattern](https://leetcode.com/problems/132-pattern/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>Given an array of <code>n</code> integers <code>nums</code>, a <strong>132 pattern</strong> is a subsequence of three integers <code>nums[i]</code>, <code>nums[j]</code> and <code>nums[k]</code> such that <code>i < j < k</code> and <code>nums[i] < nums[k] < nums[j]</code>.</p> | ||
|
||
<p>Return <code>true</code><em> if there is a <strong>132 pattern</strong> in </em><code>nums</code><em>, otherwise, return </em><code>false</code><em>.</em></p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [1,2,3,4] | ||
<strong>Output:</strong> false | ||
<strong>Explanation:</strong> There is no 132 pattern in the sequence. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [3,1,4,2] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> There is a 132 pattern in the sequence: [1, 4, 2]. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [-1,3,2,0] | ||
<strong>Output:</strong> true | ||
<strong>Explanation:</strong> There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0]. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>n == nums.length</code></li> | ||
<li><code>1 <= n <= 2 * 10<sup>5</sup></code></li> | ||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# [16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p> | ||
|
||
<p>Return <em>the sum of the three integers</em>.</p> | ||
|
||
<p>You may assume that each input would have exactly one solution.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1 | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [0,0,0], target = 1 | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> The sum that is closest to the target is 0. (0 + 0 + 0 = 0). | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>3 <= nums.length <= 500</code></li> | ||
<li><code>-1000 <= nums[i] <= 1000</code></li> | ||
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# [15. 3Sum](https://leetcode.com/problems/3sum/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p> | ||
|
||
<p>Notice that the solution set must not contain duplicate triplets.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4] | ||
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]] | ||
<strong>Explanation:</strong> | ||
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. | ||
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. | ||
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. | ||
The distinct triplets are [-1,0,1] and [-1,-1,2]. | ||
Notice that the order of the output and the order of the triplets does not matter. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [0,1,1] | ||
<strong>Output:</strong> [] | ||
<strong>Explanation:</strong> The only possible triplet does not sum up to 0. | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> nums = [0,0,0] | ||
<strong>Output:</strong> [[0,0,0]] | ||
<strong>Explanation:</strong> The only possible triplet sums up to 0. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>3 <= nums.length <= 3000</code></li> | ||
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# [67. Add Binary](https://leetcode.com/problems/add-binary/) | ||
|
||
<p style="color:green;">Easy</p> | ||
|
||
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
<pre><strong>Input:</strong> a = "11", b = "1" | ||
<strong>Output:</strong> "100" | ||
</pre><p><strong class="example">Example 2:</strong></p> | ||
<pre><strong>Input:</strong> a = "1010", b = "1011" | ||
<strong>Output:</strong> "10101" | ||
</pre> | ||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= a.length, b.length <= 10<sup>4</sup></code></li> | ||
<li><code>a</code> and <code>b</code> consist only of <code>'0'</code> or <code>'1'</code> characters.</li> | ||
<li>Each string does not contain leading zeros except for the zero itself.</li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# [258. Add Digits](https://leetcode.com/problems/add-digits/) | ||
|
||
<p style="color:green;">Easy</p> | ||
|
||
<p>Given an integer <code>num</code>, repeatedly add all its digits until the result has only one digit, and return it.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num = 38 | ||
<strong>Output:</strong> 2 | ||
<strong>Explanation:</strong> The process is | ||
38 --> 3 + 8 --> 11 | ||
11 --> 1 + 1 --> 2 | ||
Since 2 has only one digit, return it. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num = 0 | ||
<strong>Output:</strong> 0 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>0 <= num <= 2<sup>31</sup> - 1</code></li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Follow up:</strong> Could you do it without any loop/recursion in <code>O(1)</code> runtime?</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# [415. Add Strings](https://leetcode.com/problems/add-strings/) | ||
|
||
<p style="color:green;">Easy</p> | ||
|
||
<p>Given two non-negative integers, <code>num1</code> and <code>num2</code> represented as string, return <em>the sum of</em> <code>num1</code> <em>and</em> <code>num2</code> <em>as a string</em>.</p> | ||
|
||
<p>You must solve the problem without using any built-in library for handling large integers (such as <code>BigInteger</code>). You must also not convert the inputs to integers directly.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num1 = "11", num2 = "123" | ||
<strong>Output:</strong> "134" | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num1 = "456", num2 = "77" | ||
<strong>Output:</strong> "533" | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num1 = "0", num2 = "0" | ||
<strong>Output:</strong> "0" | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= num1.length, num2.length <= 10<sup>4</sup></code></li> | ||
<li><code>num1</code> and <code>num2</code> consist of only digits.</li> | ||
<li><code>num1</code> and <code>num2</code> don't have any leading zeros except for the zero itself.</li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# [989. Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | ||
|
||
<p style="color:green;">Easy</p> | ||
|
||
<p>The <strong>array-form</strong> of an integer <code>num</code> is an array representing its digits in left to right order.</p> | ||
|
||
<ul> | ||
<li>For example, for <code>num = 1321</code>, the array form is <code>[1,3,2,1]</code>.</li> | ||
</ul> | ||
|
||
<p>Given <code>num</code>, the <strong>array-form</strong> of an integer, and an integer <code>k</code>, return <em>the <strong>array-form</strong> of the integer</em> <code>num + k</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num = [1,2,0,0], k = 34 | ||
<strong>Output:</strong> [1,2,3,4] | ||
<strong>Explanation:</strong> 1200 + 34 = 1234 | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num = [2,7,4], k = 181 | ||
<strong>Output:</strong> [4,5,5] | ||
<strong>Explanation:</strong> 274 + 181 = 455 | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> num = [2,1,5], k = 806 | ||
<strong>Output:</strong> [1,0,2,1] | ||
<strong>Explanation:</strong> 215 + 806 = 1021 | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= num.length <= 10<sup>4</sup></code></li> | ||
<li><code>0 <= num[i] <= 9</code></li> | ||
<li><code>num</code> does not contain any leading zeros except for the zero itself.</li> | ||
<li><code>1 <= k <= 10<sup>4</sup></code></li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# [445. Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.</p> | ||
|
||
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg" style="width: 523px; height: 342px;" /> | ||
<pre> | ||
<strong>Input:</strong> l1 = [7,2,4,3], l2 = [5,6,4] | ||
<strong>Output:</strong> [7,8,0,7] | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4] | ||
<strong>Output:</strong> [8,0,7] | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> l1 = [0], l2 = [0] | ||
<strong>Output:</strong> [0] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li> | ||
<li><code>0 <= Node.val <= 9</code></li> | ||
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li> | ||
</ul> | ||
|
||
<p> </p> | ||
<p><strong>Follow up:</strong> Could you solve it without reversing the input lists?</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# [2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | ||
|
||
<p style="color:orange;">Medium</p> | ||
|
||
<p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The digits are stored in <strong>reverse order</strong>, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.</p> | ||
|
||
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" /> | ||
<pre> | ||
<strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4] | ||
<strong>Output:</strong> [7,0,8] | ||
<strong>Explanation:</strong> 342 + 465 = 807. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> l1 = [0], l2 = [0] | ||
<strong>Output:</strong> [0] | ||
</pre> | ||
|
||
<p><strong class="example">Example 3:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] | ||
<strong>Output:</strong> [8,9,9,9,0,0,0,1] | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li> | ||
<li><code>0 <= Node.val <= 9</code></li> | ||
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li> | ||
</ul> | ||
|
Oops, something went wrong.