1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-02 05:13:29 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

更新国内版力扣题目描述变更

This commit is contained in:
2022-05-02 23:42:43 +08:00
parent 74598d1cf8
commit 7ea03594b3
198 changed files with 11378 additions and 11118 deletions

View File

@@ -1,25 +1,25 @@
<p>Given an array filled with letters and numbers, find the longest subarray with an equal number of letters and numbers.</p>
<p>Return the subarray. If there are more than one answer, return the one which has the smallest&nbsp;index of its left endpoint. If there is no answer, return an empty arrary.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>[&quot;A&quot;,&quot;1&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;E&quot;,&quot;5&quot;,&quot;F&quot;,&quot;G&quot;,&quot;6&quot;,&quot;7&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;L&quot;,&quot;M&quot;]
<strong>Output: </strong>[&quot;A&quot;,&quot;1&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;E&quot;,&quot;5&quot;,&quot;F&quot;,&quot;G&quot;,&quot;6&quot;,&quot;7&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>[&quot;A&quot;,&quot;A&quot;]
<strong>Output: </strong>[]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>array.length &lt;= 100000</code></li>
</ul>
<p>Given an array filled with letters and numbers, find the longest subarray with an equal number of letters and numbers.</p>
<p>Return the subarray. If there are more than one answer, return the one which has the smallest&nbsp;index of its left endpoint. If there is no answer, return an empty arrary.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>[&quot;A&quot;,&quot;1&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;E&quot;,&quot;5&quot;,&quot;F&quot;,&quot;G&quot;,&quot;6&quot;,&quot;7&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;L&quot;,&quot;M&quot;]
<strong>Output: </strong>[&quot;A&quot;,&quot;1&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;E&quot;,&quot;5&quot;,&quot;F&quot;,&quot;G&quot;,&quot;6&quot;,&quot;7&quot;]
</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1,14 +1,14 @@
<p>Write a method to count the number of 2s that appear in all the numbers between 0&nbsp;and n (inclusive).</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input: </strong>25
<strong>Output: </strong>9
<strong>Explanation: </strong>(2, 12, 20, 21, 22, 23, 24, 25)(Note that 22 counts for two 2s.)</pre>
<p>Note:</p>
<ul>
<li><code>n &lt;= 10^9</code></li>
</ul>
<p>Write a method to count the number of 2s that appear in all the numbers between 0&nbsp;and n (inclusive).</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input: </strong>25
<strong>Output: </strong>9

View File

@@ -1,20 +1,20 @@
<p>The data structure&nbsp;<code>TreeNode</code>&nbsp;is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to convert a binary search tree (implemented with <code>TreeNode</code>) into a single&nbsp;linked list. The values should be kept in order and the operation should be performed in place (that is, on the original data structure).</p>
<p>Return the head node of the linked list after converting.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> [4,2,5,1,3,null,6,0]
<strong>Output: </strong> [0,null,1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li>The number of nodes will not exceed&nbsp;100000.</li>
</ul>
<p>The data structure&nbsp;<code>TreeNode</code>&nbsp;is used for binary tree, but it can also used to represent a single linked list (where left is null, and right is the next node in the list). Implement a method to convert a binary search tree (implemented with <code>TreeNode</code>) into a single&nbsp;linked list. The values should be kept in order and the operation should be performed in place (that is, on the original data structure).</p>
<p>Return the head node of the linked list after converting.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>

View File

@@ -1,22 +1,22 @@
<p>Design and build a &quot;least recently used&quot; cache, which evicts the least recently used item. The cache should map from keys to values (allowing you to insert and retrieve a value associ&shy;ated with a particular key) and be initialized with a max size. When it is full, it should evict the least recently used item.</p>
<p>You should implement following operations:&nbsp;&nbsp;<code>get</code>&nbsp;and <code>put</code>.</p>
<p>Get a value by key:&nbsp;<code>get(key)</code> - If key is in the cache, return the value, otherwise return -1.<br />
Write a key-value pair to the cache:&nbsp;<code>put(key, value)</code> - If the key is not in the cache, then write its value to the cache. Evict the least recently used item before writing if necessary.</p>
<p><strong>Example:</strong></p>
<pre>
LRUCache cache = new LRUCache( 2 /* capacity */ );
cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
</pre>
<p>Design and build a &quot;least recently used&quot; cache, which evicts the least recently used item. The cache should map from keys to values (allowing you to insert and retrieve a value associ&shy;ated with a particular key) and be initialized with a max size. When it is full, it should evict the least recently used item.</p>
<p>You should implement following operations:&nbsp;&nbsp;<code>get</code>&nbsp;and <code>put</code>.</p>
<p>Get a value by key:&nbsp;<code>get(key)</code> - If key is in the cache, return the value, otherwise return -1.<br />
Write a key-value pair to the cache:&nbsp;<code>put(key, value)</code> - If the key is not in the cache, then write its value to the cache. Evict the least recently used item before writing if necessary.</p>
<p><strong>Example:</strong></p>
<pre>
LRUCache cache = new LRUCache( 2 /* capacity */ );

View File

@@ -1,25 +1,25 @@
<p>On old cell phones, users typed on a numeric keypad and the phone would provide a list of words that matched these numbers. Each digit mapped to a set of 0&nbsp;- 4 letters. Implement an algo&shy;rithm to return a list of matching words, given a sequence of digits. You are provided a list of valid words. The mapping is shown in the diagram below:</p>
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/17_telephone_keypad.png" style="width: 200px;" /></p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;8733&quot;, words = [&quot;tree&quot;, &quot;used&quot;]
<strong>Output:</strong> [&quot;tree&quot;, &quot;used&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;2&quot;, words = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]
<strong>Output:</strong> [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</pre>
<p>Note:</p>
<ul>
<li><code>num.length &lt;= 1000</code></li>
<li><code>words.length &lt;= 500</code></li>
<li><code>words[i].length == num.length</code></li>
<li><code>There are no number 0 and 1 in num</code>.</li>
</ul>
<p>On old cell phones, users typed on a numeric keypad and the phone would provide a list of words that matched these numbers. Each digit mapped to a set of 0&nbsp;- 4 letters. Implement an algo&shy;rithm to return a list of matching words, given a sequence of digits. You are provided a list of valid words. The mapping is shown in the diagram below:</p>
<p><img src="https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/17_telephone_keypad.png" style="width: 200px;" /></p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = &quot;8733&quot;, words = [&quot;tree&quot;, &quot;used&quot;]
<strong>Output:</strong> [&quot;tree&quot;, &quot;used&quot;]
</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1,23 +1,23 @@
<p>Write a method to replace all spaces in a string with &#39;%20&#39;. You may assume that the string has sufficient space at the end to hold the additional characters,and that you are given the &quot;true&quot; length of the string. (Note: If implementing in Java,please use a character array so that you can perform this operation in place.)</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>&quot;Mr John Smith &quot;, 13
<strong>Output: </strong>&quot;Mr%20John%20Smith&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>&quot; &quot;, 5
<strong>Output: </strong>&quot;%20%20%20%20%20&quot;
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= S.length &lt;= 500000</code></li>
</ol>
<p>Write a method to replace all spaces in a string with &#39;%20&#39;. You may assume that the string has sufficient space at the end to hold the additional characters,and that you are given the &quot;true&quot; length of the string. (Note: If implementing in Java,please use a character array so that you can perform this operation in place.)</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>&quot;Mr John Smith &quot;, 13
<strong>Output: </strong>&quot;Mr%20John%20Smith&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>

View File

@@ -1,6 +1,6 @@
<p>Given an integer array <code>data</code> representing the data, return whether it is a valid <strong>UTF-8</strong> encoding.</p>
<p>Given an integer array <code>data</code> representing the data, return whether it is a valid <strong>UTF-8</strong> encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).</p>
<p>A character in <strong>UTF8</strong> can be from <b>1 to 4 bytes</b> long, subjected to the following rules:</p>
<p>A character in <strong>UTF8</strong> can be from <strong>1 to 4 bytes</strong> long, subjected to the following rules:</p>
<ol>
<li>For a <strong>1-byte</strong> character, the first bit is a <code>0</code>, followed by its Unicode code.</li>
@@ -10,16 +10,18 @@
<p>This is how the UTF-8 encoding would work:</p>
<pre>
<code> Char. number range | UTF-8 octet sequence
(hexadecimal) | (binary)
--------------------+---------------------------------------------
0000 0000-0000 007F | 0xxxxxxx
0000 0080-0000 07FF | 110xxxxx 10xxxxxx
0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx</code>
Number of Bytes | UTF-8 Octet Sequence
| (binary)
--------------------+-----------------------------------------
1 | 0xxxxxxx
2 | 110xxxxx 10xxxxxx
3 | 1110xxxx 10xxxxxx 10xxxxxx
4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
</pre>
<p><b>Note: </b>The input is an array of integers. Only the <b>least significant 8 bits</b> of each integer is used to store the data. This means each integer represents only 1 byte of data.</p>
<p><code>x</code> denotes a bit in the binary form of a byte that may be either <code>0</code> or <code>1</code>.</p>
<p><strong>Note: </strong>The input is an array of integers. Only the <strong>least significant 8 bits</strong> of each integer is used to store the data. This means each integer represents only 1 byte of data.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

View File

@@ -1,21 +1,21 @@
<p>There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.</p>
<p>&nbsp;</p>
<p><strong>Example&nbsp;1:</strong></p>
<pre>
<strong>Input:</strong>
first = &quot;pale&quot;
second = &quot;ple&quot;
<strong>Output:</strong> True
</pre>
<p><strong>Example&nbsp;2:</strong></p>
<pre>
<strong>Input:</strong>
first = &quot;pales&quot;
second = &quot;pal&quot;
<strong>Output:</strong> False
</pre>
<p>There are three types of edits that can be performed on strings: insert a character, remove a character, or replace a character. Given two strings, write a function to check if they are one edit (or zero edits) away.</p>
<p>&nbsp;</p>
<p><strong>Example&nbsp;1:</strong></p>
<pre>
<strong>Input:</strong>
first = &quot;pale&quot;
second = &quot;ple&quot;
<strong>Output:</strong> True

View File

@@ -1,26 +1,26 @@
<p>Describe how you could use a single array to implement three stacks.</p>
<p>You&nbsp;should implement&nbsp;<code>push(stackNum, value)</code><code>pop(stackNum)</code><code>isEmpty(stackNum)</code><code>peek(stackNum)</code>&nbsp;methods.&nbsp;<code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS">&nbsp;</font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack.&nbsp;</font><code>value</code>&nbsp;is the value that pushed to the stack.</p>
<p>The constructor requires a&nbsp;<code>stackSize</code>&nbsp;parameter, which represents the size of each stack.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
<strong> Output</strong>:
[null, null, null, 1, -1, -1, true]
<b>Explanation</b>: When the stack is empty, `pop, peek` return -1. When the stack is full, `push` does nothing.
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;peek&quot;]
[[2], [0, 1], [0, 2], [0, 3], [0], [0], [0], [0]]
<strong> Output</strong>:
[null, null, null, null, 2, 1, -1, -1]
</pre>
<p>Describe how you could use a single array to implement three stacks.</p>
<p>You&nbsp;should implement&nbsp;<code>push(stackNum, value)</code><code>pop(stackNum)</code><code>isEmpty(stackNum)</code><code>peek(stackNum)</code>&nbsp;methods.&nbsp;<code>stackNum<font face="sans-serif, Arial, Verdana, Trebuchet MS">&nbsp;</font></code><font face="sans-serif, Arial, Verdana, Trebuchet MS">is the index of the stack.&nbsp;</font><code>value</code>&nbsp;is the value that pushed to the stack.</p>
<p>The constructor requires a&nbsp;<code>stackSize</code>&nbsp;parameter, which represents the size of each stack.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;TripleInOne&quot;, &quot;push&quot;, &quot;push&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;pop&quot;, &quot;isEmpty&quot;]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
<strong> Output</strong>:

View File

@@ -27,6 +27,14 @@ We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [2,1,3], target = 6
<strong>Output:</strong> 1
<strong>Explanation:</strong> (1, 2, 3) occured one time in the array so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

View File

@@ -1,21 +1,21 @@
<p>A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.&nbsp;The result may be large, so return it modulo 1000000007.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: n = 3
<strong> Output</strong>: 4
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: n = 5
<strong> Output</strong>: 13
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= n &lt;= 1000000</code></li>
</ol>
<p>A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the child can run up the stairs.&nbsp;The result may be large, so return it modulo 1000000007.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: n = 3
<strong> Output</strong>: 4
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1,22 +1,22 @@
<p>Given a positive integer, print the next smallest and the next largest number that have the same number of 1 bits in their binary representation.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: num = 2 (0b10)
<strong> Output</strong>: [4, 1] ([0b100, 0b1])
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: num = 1
<strong> Output</strong>: [2, -1]
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= num &lt;=&nbsp;2147483647</code></li>
<li>If there is no next smallest or next largest number, output -1.</li>
</ol>
<p>Given a positive integer, print the next smallest and the next largest number that have the same number of 1 bits in their binary representation.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: num = 2 (0b10)
<strong> Output</strong>: [4, 1] ([0b100, 0b1])
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1,10 +1,10 @@
<p>A robot is located at the top-left corner of a <code>m x n</code> grid (marked &#39;Start&#39; in the diagram below).</p>
<p>You are given an <code>m x n</code> integer array <code>grid</code>. There is a robot initially located at the <b>top-left corner</b> (i.e., <code>grid[0][0]</code>). The robot tries to move to the <strong>bottom-right corner</strong> (i.e., <code>grid[m-1][n-1]</code>). The robot can only move either down or right at any point in time.</p>
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#39;Finish&#39; in the diagram below).</p>
<p>An obstacle and space are marked as <code>1</code> or <code>0</code> respectively in <code>grid</code>. A path that the robot takes cannot include <strong>any</strong> square that is an obstacle.</p>
<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p>
<p>Return <em>the number of possible unique paths that the robot can take to reach the bottom-right corner</em>.</p>
<p>An obstacle and space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p>
<p>The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
@@ -29,8 +29,8 @@ There are two ways to reach the bottom-right corner:
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m ==&nbsp;obstacleGrid.length</code></li>
<li><code>n ==&nbsp;obstacleGrid[i].length</code></li>
<li><code>m == obstacleGrid.length</code></li>
<li><code>n == obstacleGrid[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 100</code></li>
<li><code>obstacleGrid[i][j]</code> is <code>0</code> or <code>1</code>.</li>
</ul>

View File

@@ -1,16 +1,16 @@
<p>Write a function that adds two numbers. You should not use + or any arithmetic operators.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> a = 1, b = 1
<strong>Output:</strong> 2</pre>
<p>&nbsp;</p>
<p><strong>Note: </strong></p>
<ul>
<li><code>a</code>&nbsp;and&nbsp;<code>b</code>&nbsp;may be 0 or negative.</li>
<li>The result fits in 32-bit integer.</li>
</ul>
<p>Write a function that adds two numbers. You should not use + or any arithmetic operators.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> a = 1, b = 1
<strong>Output:</strong> 2</pre>

View File

@@ -1,36 +1,36 @@
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p>
<ul>
<li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li>
</ul>
<p>Given an integer array <code>nums</code>, choose four <strong>distinct</strong> indices <code>w</code>, <code>x</code>, <code>y</code>, and <code>z</code> such that the <strong>product difference</strong> between pairs <code>(nums[w], nums[x])</code> and <code>(nums[y], nums[z])</code> is <strong>maximized</strong>.</p>
<p>Return <em>the <strong>maximum</strong> such product difference</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,2,7,4]
<strong>Output:</strong> 34
<strong>Explanation:</strong> We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) - (2 * 4) = 34.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,2,5,9,7,4,8]
<strong>Output:</strong> 64
<strong>Explanation:</strong> We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
The product difference is (9 * 8) - (2 * 4) = 64.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>4 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<p>The <strong>product difference</strong> between two pairs <code>(a, b)</code> and <code>(c, d)</code> is defined as <code>(a * b) - (c * d)</code>.</p>
<ul>
<li>For example, the product difference between <code>(5, 6)</code> and <code>(2, 7)</code> is <code>(5 * 6) - (2 * 7) = 16</code>.</li>
</ul>
<p>Given an integer array <code>nums</code>, choose four <strong>distinct</strong> indices <code>w</code>, <code>x</code>, <code>y</code>, and <code>z</code> such that the <strong>product difference</strong> between pairs <code>(nums[w], nums[x])</code> and <code>(nums[y], nums[z])</code> is <strong>maximized</strong>.</p>
<p>Return <em>the <strong>maximum</strong> such product difference</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,2,7,4]
<strong>Output:</strong> 34
<strong>Explanation:</strong> We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) - (2 * 4) = 34.

View File

@@ -1,24 +1,24 @@
<p>A majority element is an element that makes up more than half of the items in an array. Given a&nbsp;integers array, find the majority element. If there is no majority element, return -1. Do this in O(N) time and O(1) space.</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>[1,2,5,9,5,9,5,5,5]
<strong>Output: </strong>5</pre>
<p>&nbsp;</p>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong>[3,2]
<strong>Output: </strong>-1</pre>
<p>&nbsp;</p>
<p><strong>Example 3: </strong></p>
<pre>
<strong>Input: </strong>[2,2,1,1,1,2,2]
<strong>Output: </strong>2
</pre>
<p>A majority element is an element that makes up more than half of the items in an array. Given a&nbsp;integers array, find the majority element. If there is no majority element, return -1. Do this in O(N) time and O(1) space.</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>[1,2,5,9,5,9,5,5,5]
<strong>Output: </strong>5</pre>
<p>&nbsp;</p>
<p><strong>Example 2: </strong></p>

View File

@@ -1,19 +1,19 @@
<p>A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.</p>
<p><strong>Example:</strong><br />
Given the following tree:</p>
<pre>
2
/ \
1 3
</pre>
<p>Output:</p>
<pre>
[
[2,1,3],
[2,3,1]
]
</pre>
<p>A binary search tree was created by traversing through an array from left to right and inserting each element. Given a binary search tree with distinct elements, print all possible arrays that could have led to this tree.</p>
<p><strong>Example:</strong><br />
Given the following tree:</p>
<pre>
2
/ \
1 3
</pre>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,22 +1,23 @@
<p>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print &quot;ERROR&quot;.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: 0.625
<strong> Output</strong>: &quot;0.101&quot;
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: 0.1
<strong> Output</strong>: &quot;ERROR&quot;
<strong> Note</strong>: 0.1 cannot be represented accurately in binary.
</pre>
<p><strong>Note: </strong></p>
<ol>
<li>This two charaters &quot;0.&quot; should be counted into 32 characters.</li>
</ol>
<p>Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print &quot;ERROR&quot;.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: 0.625
<strong> Output</strong>: &quot;0.101&quot;
</pre>
<p><strong>Example2:</strong></p>
<pre>

View File

@@ -1,44 +1,44 @@
<p>Design an algorithm to figure out if someone has won a game of tic-tac-toe.&nbsp;Input is a string array&nbsp;of size N x N, including characters &quot; &quot;, &quot;X&quot; and &quot;O&quot;, where &quot; &quot; represents a empty grid.</p>
<p>The rules of tic-tac-toe are as follows:</p>
<ul>
<li>Players place characters into an empty grid(&quot; &quot;) in turn.</li>
<li>The first player always place character &quot;O&quot;, and the second one place &quot;X&quot;.</li>
<li>Players are only allowed to place characters in empty grid. Replacing a character is not allowed.</li>
<li>If there is any row, column or diagonal filled with N&nbsp;same characters, the game ends. The player who place the last charater wins.</li>
<li>When there is no empty grid, the game ends.</li>
<li>If the game ends, players cannot place any character further.</li>
</ul>
<p>If there is any winner, return the character that the winner used. If there&#39;s a draw, return &quot;Draw&quot;. If the game doesn&#39;t end and there is no winner, return &quot;Pending&quot;.</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong> board = [&quot;O X&quot;,&quot; XO&quot;,&quot;X O&quot;]
<strong>Output: </strong> &quot;X&quot;
</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong> board = [&quot;OOX&quot;,&quot;XXO&quot;,&quot;OXO&quot;]
<strong>Output: </strong> &quot;Draw&quot;
<strong>Explanation: </strong> no player wins and no empty grid left
</pre>
<p><strong>Example 3: </strong></p>
<pre>
<strong>Input: </strong> board = [&quot;OOX&quot;,&quot;XXO&quot;,&quot;OX &quot;]
<strong>Output: </strong> &quot;Pending&quot;
<strong>Explanation: </strong> no player wins but there is still a empty grid
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>1 &lt;= board.length == board[i].length &lt;= 100</code></li>
<li>Input follows the rules.</li>
</ul>
<p>Design an algorithm to figure out if someone has won a game of tic-tac-toe.&nbsp;Input is a string array&nbsp;of size N x N, including characters &quot; &quot;, &quot;X&quot; and &quot;O&quot;, where &quot; &quot; represents a empty grid.</p>
<p>The rules of tic-tac-toe are as follows:</p>
<ul>
<li>Players place characters into an empty grid(&quot; &quot;) in turn.</li>
<li>The first player always place character &quot;O&quot;, and the second one place &quot;X&quot;.</li>
<li>Players are only allowed to place characters in empty grid. Replacing a character is not allowed.</li>
<li>If there is any row, column or diagonal filled with N&nbsp;same characters, the game ends. The player who place the last charater wins.</li>
<li>When there is no empty grid, the game ends.</li>
<li>If the game ends, players cannot place any character further.</li>
</ul>
<p>If there is any winner, return the character that the winner used. If there&#39;s a draw, return &quot;Draw&quot;. If the game doesn&#39;t end and there is no winner, return &quot;Pending&quot;.</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong> board = [&quot;O X&quot;,&quot; XO&quot;,&quot;X O&quot;]
<strong>Output: </strong> &quot;X&quot;
</pre>

View File

@@ -1,22 +1,22 @@
<p>Given two arrays of integers, find a pair of values (one value from each array) that you can swap to give the two arrays the same sum.</p>
<p>Return an array, where the first element is the element in the first array that will be swapped, and the second element is another one in the second array. If there are more than one answers, return any one of them. If there is no answer, return an empty array.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> array1 = [4, 1, 2, 1, 1, 2], array2 = [3, 6, 3, 3]
<strong>Output:</strong> [1, 3]
</pre>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> array1 = <code>[1, 2, 3], array2 = [4, 5, 6]</code>
<strong>Output: </strong>[]</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>1 &lt;= array1.length, array2.length &lt;= 100000</code></li>
</ul>
<p>Given two arrays of integers, find a pair of values (one value from each array) that you can swap to give the two arrays the same sum.</p>
<p>Return an array, where the first element is the element in the first array that will be swapped, and the second element is another one in the second array. If there are more than one answers, return any one of them. If there is no answer, return an empty array.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> array1 = [4, 1, 2, 1, 1, 2], array2 = [3, 6, 3, 3]
<strong>Output:</strong> [1, 3]
</pre>

View File

@@ -1,15 +1,15 @@
<p>Write a function to swap a number in place (that is, without temporary variables).</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input:</strong> numbers = [1,2]
<strong>Output:</strong> [2,1]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>numbers.length == 2</code></li>
<li><code>-2147483647 &lt;= numbers[i] &lt;=&nbsp;2147483647</code></li>
</ul>
<p>Write a function to swap a number in place (that is, without temporary variables).</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input:</strong> numbers = [1,2]
<strong>Output:</strong> [2,1]
</pre>

View File

@@ -1,45 +1,45 @@
<p>You are given two strings <code>word1</code> and <code>word2</code>. Merge the strings by adding letters in alternating order, starting with <code>word1</code>. If a string is longer than the other, append the additional letters onto the end of the merged string.</p>
<p>Return <em>the merged string.</em></p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;pqr&quot;
<strong>Output:</strong> &quot;apbqcr&quot;
<strong>Explanation:</strong>&nbsp;The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> word1 = &quot;ab&quot;, word2 = &quot;pqrs&quot;
<strong>Output:</strong> &quot;apbqrs&quot;
<strong>Explanation:</strong>&nbsp;Notice that as word2 is longer, &quot;rs&quot; is appended to the end.
word1: a b
word2: p q r s
merged: a p b q r s
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> word1 = &quot;abcd&quot;, word2 = &quot;pq&quot;
<strong>Output:</strong> &quot;apbqcd&quot;
<strong>Explanation:</strong>&nbsp;Notice that as word1 is longer, &quot;cd&quot; is appended to the end.
word1: a b c d
word2: p q
merged: a p b q c d
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= word1.length, word2.length &lt;= 100</code></li>
<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
<p>You are given two strings <code>word1</code> and <code>word2</code>. Merge the strings by adding letters in alternating order, starting with <code>word1</code>. If a string is longer than the other, append the additional letters onto the end of the merged string.</p>
<p>Return <em>the merged string.</em></p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;pqr&quot;
<strong>Output:</strong> &quot;apbqcr&quot;
<strong>Explanation:</strong>&nbsp;The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> word1 = &quot;ab&quot;, word2 = &quot;pqrs&quot;
<strong>Output:</strong> &quot;apbqrs&quot;
<strong>Explanation:</strong>&nbsp;Notice that as word2 is longer, &quot;rs&quot; is appended to the end.
word1: a b

View File

@@ -1,36 +1,36 @@
<p>Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any. If there&#39;s no intersection, return an empty array.</p>
The absolute error should not exceed 10^-6. If there are more than one intersections, return the one with smallest X axis value. If there are more than one intersections that have same X axis value, return the one with smallest Y axis value.
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>
line1 = {0, 0}, {1, 0}
line2 = {1, 1}, {0, -1}
<strong>Output: </strong> {0.5, 0}
</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong>
line1 = {0, 0}, {3, 3}
line2 = {1, 1}, {2, 2}
<strong>Output: </strong> {1, 1}
</pre>
<p><strong>Example 3: </strong></p>
<pre>
<strong>Input: </strong>
line1 = {0, 0}, {1, 1}
line2 = {1, 0}, {2, 1}
<strong>Output: </strong> {} (no intersection)
</pre>
<p><strong>Note: </strong></p>
<ul>
<li>The absolute value of coordinate value will not exceed 2^7.</li>
<li>All coordinates are valid 2D coordinates.</li>
</ul>
<p>Given two straight line segments (represented as a start point and an end point), compute the point of intersection, if any. If there&#39;s no intersection, return an empty array.</p>
The absolute error should not exceed 10^-6. If there are more than one intersections, return the one with smallest X axis value. If there are more than one intersections that have same X axis value, return the one with smallest Y axis value.
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>
line1 = {0, 0}, {1, 0}
line2 = {1, 1}, {0, -1}
<strong>Output: </strong> {0.5, 0}
</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong>
line1 = {0, 0}, {3, 3}
line2 = {1, 1}, {2, 2}

View File

@@ -1,22 +1,22 @@
<p>Write an algorithm to print all ways of arranging n queens on an n x n&nbsp;chess board so that none of them share the same row, column, or diagonal. In this case, &quot;diagonal&quot; means all diagonals, not just the two that bisect the board.</p>
<p><strong>Notes: </strong>This&nbsp;problem is a generalization of the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong> Input</strong>: 4
<strong> Output</strong>: [[&quot;.Q..&quot;,&quot;...Q&quot;,&quot;Q...&quot;,&quot;..Q.&quot;],[&quot;..Q.&quot;,&quot;Q...&quot;,&quot;...Q&quot;,&quot;.Q..&quot;]]
<strong> Explanation</strong>: 4 queens has following two solutions
[
&nbsp;[&quot;.Q..&quot;, &nbsp;// solution 1
&nbsp; &quot;...Q&quot;,
&nbsp; &quot;Q...&quot;,
&nbsp; &quot;..Q.&quot;],
&nbsp;[&quot;..Q.&quot;, &nbsp;// solution 2
&nbsp; &quot;Q...&quot;,
&nbsp; &quot;...Q&quot;,
&nbsp; &quot;.Q..&quot;]
]
</pre>
<p>Write an algorithm to print all ways of arranging n queens on an n x n&nbsp;chess board so that none of them share the same row, column, or diagonal. In this case, &quot;diagonal&quot; means all diagonals, not just the two that bisect the board.</p>
<p><strong>Notes: </strong>This&nbsp;problem is a generalization of the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong> Input</strong>: 4
<strong> Output</strong>: [[&quot;.Q..&quot;,&quot;...Q&quot;,&quot;Q...&quot;,&quot;..Q.&quot;],[&quot;..Q.&quot;,&quot;Q...&quot;,&quot;...Q&quot;,&quot;.Q..&quot;]]
<strong> Explanation</strong>: 4 queens has following two solutions
[

View File

@@ -1,47 +1,47 @@
<p>An ant is sitting on an infinite grid of white and black squares. It initially faces right. All squares are white initially.</p>
<p>At each step, it does the following:</p>
<p>(1) At a white square, flip the color of the square, turn 90 degrees right (clockwise), and move forward one unit.</p>
<p>(2) At a black square, flip the color of the square, turn 90 degrees left (counter-clockwise), and move forward one unit.</p>
<p>Write a program to simulate the first K moves that the ant makes and print the final board as a grid.</p>
<p>The grid should be represented as an array of strings, where each element represents one row in the grid. The black square is represented as <code>&#39;X&#39;</code>, and the white square is represented as <code>&#39;_&#39;</code>, the square which is occupied by the ant is represented as <code>&#39;L&#39;</code>, <code>&#39;U&#39;</code>, <code>&#39;R&#39;</code>, <code>&#39;D&#39;</code>, which means the left, up, right and down orientations respectively. You only need to return the minimum matrix that is able to contain all squares that are passed through by the ant.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> 0
<strong>Output: </strong>[&quot;R&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> 2
<strong>Output:
</strong>[
&nbsp; &quot;_X&quot;,
&nbsp; &quot;LX&quot;
]
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> 5
<strong>Output:
</strong>[
&nbsp; &quot;_U&quot;,
&nbsp; &quot;X_&quot;,
&nbsp; &quot;XX&quot;
]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>K &lt;= 100000</code></li>
</ul>
<p>An ant is sitting on an infinite grid of white and black squares. It initially faces right. All squares are white initially.</p>
<p>At each step, it does the following:</p>
<p>(1) At a white square, flip the color of the square, turn 90 degrees right (clockwise), and move forward one unit.</p>
<p>(2) At a black square, flip the color of the square, turn 90 degrees left (counter-clockwise), and move forward one unit.</p>
<p>Write a program to simulate the first K moves that the ant makes and print the final board as a grid.</p>
<p>The grid should be represented as an array of strings, where each element represents one row in the grid. The black square is represented as <code>&#39;X&#39;</code>, and the white square is represented as <code>&#39;_&#39;</code>, the square which is occupied by the ant is represented as <code>&#39;L&#39;</code>, <code>&#39;U&#39;</code>, <code>&#39;R&#39;</code>, <code>&#39;D&#39;</code>, which means the left, up, right and down orientations respectively. You only need to return the minimum matrix that is able to contain all squares that are passed through by the ant.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> 0
<strong>Output: </strong>[&quot;R&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> 2
<strong>Output:

View File

@@ -1,8 +1,8 @@
<p>Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the &quot;right partition&quot;; it does not need to appear between the left and right partitions.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> head = 3-&gt;5-&gt;8-&gt;5-&gt;10-&gt;2-&gt;1, <em>x</em> = 5
<strong>Output:</strong> 3-&gt;1-&gt;2-&gt;10-&gt;5-&gt;5-&gt;8
</pre>
<p>Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. If x is contained within the list, the values of x only need to be after the elements less than x (see below). The partition element x can appear anywhere in the &quot;right partition&quot;; it does not need to appear between the left and right partitions.</p>
<p><strong>Example:</strong></p>

View File

@@ -1,2 +1,2 @@
<p>English description is not available for the problem. Please switch to Chinese.<br />
&nbsp;</p>
<p>English description is not available for the problem. Please switch to Chinese.<br />

View File

@@ -1,10 +1,10 @@
<p>Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>the node c from the linked list a-&gt;b-&gt;c-&gt;d-&gt;e-&gt;f
<strong>Output: </strong>nothing is returned, but the new linked list looks like a-&gt;b-&gt;d-&gt;e-&gt;f
</pre>
<p>Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>

View File

@@ -1,23 +1,23 @@
<p>Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><code>s</code> = &quot;leetcode&quot;
<strong>Output: </strong>false
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong><code>s</code> = &quot;abc&quot;
<strong>Output: </strong>true
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ul>
<li><code>0 &lt;= len(s) &lt;= 100 </code></li>
</ul>
<p>Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><code>s</code> = &quot;leetcode&quot;
<strong>Output: </strong>false
</pre>
<p><strong>Example 2:</strong></p>
<pre>

View File

@@ -1,22 +1,22 @@
<p>Given two strings,write a method to decide if one is a permutation of the other.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bca&quot;
<strong>Output: </strong>true
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong><code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bad&quot;
<strong>Output: </strong>false
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= len(s1) &lt;= 100 </code></li>
<li><code>0 &lt;= len(s2) &lt;= 100</code></li>
</ol>
<p>Given two strings,write a method to decide if one is a permutation of the other.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><code>s1</code> = &quot;abc&quot;, <code>s2</code> = &quot;bca&quot;
<strong>Output: </strong>true
</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -7,6 +7,8 @@
<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>
</ol>
<p>Note that in case a character of the string is <strong>not present</strong> in <code>keys</code>, the encryption process cannot be carried out, and an empty string <code>&quot;&quot;</code> is returned.</p>
<p>A string is <strong>decrypted</strong> with the following process:</p>
<ol>

View File

@@ -1,31 +1,31 @@
<p>An animal shelter, which holds only dogs and cats, operates on a strictly&quot;first in, first out&quot; basis. People must adopt either the&quot;oldest&quot; (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as <code>enqueue</code>, <code>dequeueAny</code>, <code>dequeueDog</code>, and <code>dequeueCat</code>. You may use the built-in Linked list data structure.</p>
<p><code>enqueue</code> method has a <code>animal</code> parameter, <code>animal[0]</code> represents the number of the animal, <code>animal[1]</code> represents the type of the animal, 0 for cat and 1 for dog.</p>
<p><code>dequeue*</code> method returns <code>[animal number, animal type]</code>, if there&#39;s no animal that can be adopted, return <code>[-1, -1]</code>.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueCat&quot;, &quot;dequeueDog&quot;, &quot;dequeueAny&quot;]
[[], [[0, 0]], [[1, 0]], [], [], []]
<strong> Output</strong>:
[null,null,null,[0,0],[-1,-1],[1,0]]
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueDog&quot;, &quot;dequeueCat&quot;, &quot;dequeueAny&quot;]
[[], [[0, 0]], [[1, 0]], [[2, 1]], [], [], []]
<strong> Output</strong>:
[null,null,null,null,[2,1],[0,0],[1,0]]
</pre>
<p><strong>Note:</strong></p>
<ol>
<li>The number of animals in the shelter will not exceed 20000.</li>
</ol>
<p>An animal shelter, which holds only dogs and cats, operates on a strictly&quot;first in, first out&quot; basis. People must adopt either the&quot;oldest&quot; (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal they would like. Create the data structures to maintain this system and implement operations such as <code>enqueue</code>, <code>dequeueAny</code>, <code>dequeueDog</code>, and <code>dequeueCat</code>. You may use the built-in Linked list data structure.</p>
<p><code>enqueue</code> method has a <code>animal</code> parameter, <code>animal[0]</code> represents the number of the animal, <code>animal[1]</code> represents the type of the animal, 0 for cat and 1 for dog.</p>
<p><code>dequeue*</code> method returns <code>[animal number, animal type]</code>, if there&#39;s no animal that can be adopted, return <code>[-1, -1]</code>.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;AnimalShelf&quot;, &quot;enqueue&quot;, &quot;enqueue&quot;, &quot;dequeueCat&quot;, &quot;dequeueDog&quot;, &quot;dequeueAny&quot;]
[[], [[0, 0]], [[1, 0]], [], [], []]
<strong> Output</strong>:
[null,null,null,[0,0],[-1,-1],[1,0]]
</pre>

View File

@@ -1,25 +1,25 @@
<p>Implement a MyQueue class which implements a queue using two stacks.</p>
&nbsp;
<p><strong>Example: </strong></p>
<pre>
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // return 1
queue.pop(); // return 1
queue.empty(); // return false</pre>
<p>&nbsp;</p>
<p><b>Notes:</b></p>
<ul>
<li>You must use&nbsp;<i>only</i>&nbsp;standard operations of a stack -- which means only&nbsp;<code>push to top</code>,&nbsp;<code>peek/pop from top</code>,&nbsp;<code>size</code>, and&nbsp;<code>is empty</code>&nbsp;operations are valid.</li>
<li>Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.</li>
<li>You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).</li>
</ul>
<p>&nbsp;</p>
<p>Implement a MyQueue class which implements a queue using two stacks.</p>
&nbsp;
<p><strong>Example: </strong></p>
<pre>
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // return 1
queue.pop(); // return 1
queue.empty(); // return false</pre>

View File

@@ -1,28 +1,28 @@
<p>Given a list of millions of words, design an algorithm to create the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). The words need not be chosen consecutively from the list but all rows must be the same length and all columns must be the same height.</p>
<p>If there are more than one answer, return any one of them. A word can be used more than once.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> <code>[&quot;this&quot;, &quot;real&quot;, &quot;hard&quot;, &quot;trh&quot;, &quot;hea&quot;, &quot;iar&quot;, &quot;sld&quot;]</code>
<strong>Output:
</strong><code>[
&nbsp; &quot;this&quot;,
&nbsp; &quot;real&quot;,
&nbsp; &quot;hard&quot;</code>
]</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> <code>[&quot;aa&quot;]</code>
<strong>Output: </strong>[&quot;aa&quot;,&quot;aa&quot;]</pre>
<p><strong>Notes: </strong></p>
<ul>
<li><code>words.length &lt;= 1000</code></li>
<li><code>words[i].length &lt;= 100</code></li>
<li>It&#39;s guaranteed that&nbsp;all the words are randomly generated.</li>
</ul>
<p>Given a list of millions of words, design an algorithm to create the largest possible rectangle of letters such that every row forms a word (reading left to right) and every column forms a word (reading top to bottom). The words need not be chosen consecutively from the list but all rows must be the same length and all columns must be the same height.</p>
<p>If there are more than one answer, return any one of them. A word can be used more than once.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> <code>[&quot;this&quot;, &quot;real&quot;, &quot;hard&quot;, &quot;trh&quot;, &quot;hea&quot;, &quot;iar&quot;, &quot;sld&quot;]</code>
<strong>Output:
</strong><code>[
&nbsp; &quot;this&quot;,
&nbsp; &quot;real&quot;,
&nbsp; &quot;hard&quot;</code>
]</pre>

View File

@@ -1,13 +1,13 @@
<p>You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>words = [&quot;I&quot;,&quot;am&quot;,&quot;a&quot;,&quot;student&quot;,&quot;from&quot;,&quot;a&quot;,&quot;university&quot;,&quot;in&quot;,&quot;a&quot;,&quot;city&quot;], word1 = &quot;a&quot;, word2 = &quot;student&quot;
<strong>Output: </strong>1</pre>
<p>Note:</p>
<ul>
<li><code>words.length &lt;= 100000</code></li>
</ul>
<p>You have a large text file containing words. Given any two different words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>words = [&quot;I&quot;,&quot;am&quot;,&quot;a&quot;,&quot;student&quot;,&quot;from&quot;,&quot;a&quot;,&quot;university&quot;,&quot;in&quot;,&quot;a&quot;,&quot;city&quot;], word1 = &quot;a&quot;, word2 = &quot;student&quot;
<strong>Output: </strong>1</pre>

View File

@@ -1,27 +1,27 @@
<p>Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary.</p>
<p>Write code to return a possible transforming sequence. If there is more than one sequence, return any of them.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong>
beginWord = &quot;hit&quot;,
endWord = &quot;cog&quot;,
wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]
<strong>Output:</strong>
[&quot;hit&quot;,&quot;hot&quot;,&quot;dot&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong>
beginWord = &quot;hit&quot;
endWord = &quot;cog&quot;
wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;]
<strong>Output: </strong>[]
<strong>Explanation:</strong>&nbsp;<em>endWord</em> &quot;cog&quot; is not in the dictionary, so there&#39;s no possible transforming sequence.</pre>
<p>Given two words of equal length that are in a dictionary, write a method to transform one word into another word by changing only one letter at a time. The new word you get in each step must be in the dictionary.</p>
<p>Write code to return a possible transforming sequence. If there is more than one sequence, return any of them.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong>
beginWord = &quot;hit&quot;,
endWord = &quot;cog&quot;,
wordList = [&quot;hot&quot;,&quot;dot&quot;,&quot;dog&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]
<strong>Output:</strong>
[&quot;hit&quot;,&quot;hot&quot;,&quot;dot&quot;,&quot;lot&quot;,&quot;log&quot;,&quot;cog&quot;]

View File

@@ -1,28 +1,28 @@
<p>Design a method to find the frequency of occurrences of any given word in a book. What if we were running this algorithm multiple times?</p>
<p>You should implement following methods:</p>
<ul>
<li><code>WordsFrequency(book)</code> constructor, parameter is a array of strings, representing the book.</li>
<li><code>get(word)</code>&nbsp;get the frequency of <code>word</code> in the book.&nbsp;</li>
</ul>
<p><strong>Example: </strong></p>
<pre>
WordsFrequency wordsFrequency = new WordsFrequency({&quot;i&quot;, &quot;have&quot;, &quot;an&quot;, &quot;apple&quot;, &quot;he&quot;, &quot;have&quot;, &quot;a&quot;, &quot;pen&quot;});
wordsFrequency.get(&quot;you&quot;); //returns 0&quot;you&quot; is not in the book
wordsFrequency.get(&quot;have&quot;); //returns 2&quot;have&quot; occurs twice in the book
wordsFrequency.get(&quot;an&quot;); //returns 1
wordsFrequency.get(&quot;apple&quot;); //returns 1
wordsFrequency.get(&quot;pen&quot;); //returns 1
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>There are only lowercase letters in book[i].</code></li>
<li><code>1 &lt;= book.length &lt;= 100000</code></li>
<li><code>1 &lt;= book[i].length &lt;= 10</code></li>
<li><code>get</code>&nbsp;function will not be called more than&nbsp;100000 times.</li>
</ul>
<p>Design a method to find the frequency of occurrences of any given word in a book. What if we were running this algorithm multiple times?</p>
<p>You should implement following methods:</p>
<ul>
<li><code>WordsFrequency(book)</code> constructor, parameter is a array of strings, representing the book.</li>
<li><code>get(word)</code>&nbsp;get the frequency of <code>word</code> in the book.&nbsp;</li>
</ul>
<p><strong>Example: </strong></p>
<pre>
WordsFrequency wordsFrequency = new WordsFrequency({&quot;i&quot;, &quot;have&quot;, &quot;an&quot;, &quot;apple&quot;, &quot;he&quot;, &quot;have&quot;, &quot;a&quot;, &quot;pen&quot;});
wordsFrequency.get(&quot;you&quot;); //returns 0&quot;you&quot; is not in the book

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,21 +1,21 @@
<p>Write a method to sort an array of strings so that all the anagrams are in the same group.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> <code>[&quot;eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]</code>,
<strong>Output:</strong>
[
[&quot;ate&quot;,&quot;eat&quot;,&quot;tea&quot;],
[&quot;nat&quot;,&quot;tan&quot;],
[&quot;bat&quot;]
]</pre>
<p><strong>Notes: </strong></p>
<ul>
<li>All inputs will be in lowercase.</li>
<li>The order of your output does not&nbsp;matter.</li>
</ul>
<p>Write a method to sort an array of strings so that all the anagrams are in the same group.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong> <code>[&quot;eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]</code>,
<strong>Output:</strong>
[
[&quot;ate&quot;,&quot;eat&quot;,&quot;tea&quot;],

View File

@@ -1,4 +1,4 @@
<p>There is only one character <code>&#39;A&#39;</code> on the screen of a notepad. You can perform two operations on this notepad for each step:</p>
<p>There is only one character <code>&#39;A&#39;</code> on the screen of a notepad. You can perform one of two operations on this notepad for each step:</p>
<ul>
<li>Copy All: You can copy all the characters present on the screen (a partial copy is not allowed).</li>
@@ -13,7 +13,7 @@
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Intitally, we have one character &#39;A&#39;.
<strong>Explanation:</strong> Initially, we have one character &#39;A&#39;.
In step 1, we use Copy All operation.
In step 2, we use Paste operation to get &#39;AA&#39;.
In step 3, we use Paste operation to get &#39;AAA&#39;.

View File

@@ -1,36 +1,36 @@
<p>Given a<strong>&nbsp;directed acyclic graph</strong>,&nbsp;with&nbsp;<code>n</code>&nbsp;vertices numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>,&nbsp;and an array&nbsp;<code>edges</code>&nbsp;where&nbsp;<code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code>&nbsp;represents a directed edge from node&nbsp;<code>from<sub>i</sub></code>&nbsp;to node&nbsp;<code>to<sub>i</sub></code>.</p>
<p>Find <em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It&#39;s guaranteed that a unique solution exists.</p>
<p>Notice that you can return the vertices in any order.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" style="width: 231px; height: 181px;" /></p>
<pre>
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
<strong>Output:</strong> [0,3]
<b>Explanation: </b>It&#39;s not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</pre>
<p><strong>Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled.png" style="width: 201px; height: 201px;" /></p>
<pre>
<strong>Input:</strong> n = 5, edges = [[0,1],[2,1],[3,1],[1,4],[2,4]]
<strong>Output:</strong> [0,2,3]
<strong>Explanation: </strong>Notice that vertices 0, 3 and 2 are not reachable from any other node, so we must include them. Also any of these vertices can reach nodes 1 and 4.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= n &lt;= 10^5</code></li>
<li><code>1 &lt;= edges.length &lt;= min(10^5, n * (n - 1) / 2)</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 &lt;= from<sub>i,</sub>&nbsp;to<sub>i</sub> &lt; n</code></li>
<li>All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.</li>
<p>Given a<strong>&nbsp;directed acyclic graph</strong>,&nbsp;with&nbsp;<code>n</code>&nbsp;vertices numbered from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>n-1</code>,&nbsp;and an array&nbsp;<code>edges</code>&nbsp;where&nbsp;<code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code>&nbsp;represents a directed edge from node&nbsp;<code>from<sub>i</sub></code>&nbsp;to node&nbsp;<code>to<sub>i</sub></code>.</p>
<p>Find <em>the smallest set of vertices from which all nodes in the graph are reachable</em>. It&#39;s guaranteed that a unique solution exists.</p>
<p>Notice that you can return the vertices in any order.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/07/07/untitled22.png" style="width: 231px; height: 181px;" /></p>
<pre>
<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,5],[3,4],[4,2]]
<strong>Output:</strong> [0,3]
<b>Explanation: </b>It&#39;s not possible to reach all the nodes from a single vertex. From 0 we can reach [0,1,2,5]. From 3 we can reach [3,4,2,5]. So we output [0,3].</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1,34 +1,34 @@
<p>You are given an array <code>rectangles</code> where <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> rectangle of length <code>l<sub>i</sub></code> and width <code>w<sub>i</sub></code>.</p>
<p>You can cut the <code>i<sup>th</sup></code> rectangle to form a square with a side length of <code>k</code> if both <code>k &lt;= l<sub>i</sub></code> and <code>k &lt;= w<sub>i</sub></code>. For example, if you have a rectangle <code>[4,6]</code>, you can cut it to get a square with a side length of at most <code>4</code>.</p>
<p>Let <code>maxLen</code> be the side length of the <strong>largest</strong> square you can obtain from any of the given rectangles.</p>
<p>Return <em>the <strong>number</strong> of rectangles that can make a square with a side length of </em><code>maxLen</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> rectangles = [[5,8],[3,9],[5,12],[16,5]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The largest squares you can get from each rectangle are of lengths [5,3,5,5].
The largest possible square is of length 5, and you can get it out of 3 rectangles.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> rectangles = [[2,3],[3,7],[4,3],[3,7]]
<strong>Output:</strong> 3
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= rectangles.length &lt;= 1000</code></li>
<li><code>rectangles[i].length == 2</code></li>
<li><code>1 &lt;= l<sub>i</sub>, w<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li><code>l<sub>i</sub> != w<sub>i</sub></code></li>
<p>You are given an array <code>rectangles</code> where <code>rectangles[i] = [l<sub>i</sub>, w<sub>i</sub>]</code> represents the <code>i<sup>th</sup></code> rectangle of length <code>l<sub>i</sub></code> and width <code>w<sub>i</sub></code>.</p>
<p>You can cut the <code>i<sup>th</sup></code> rectangle to form a square with a side length of <code>k</code> if both <code>k &lt;= l<sub>i</sub></code> and <code>k &lt;= w<sub>i</sub></code>. For example, if you have a rectangle <code>[4,6]</code>, you can cut it to get a square with a side length of at most <code>4</code>.</p>
<p>Let <code>maxLen</code> be the side length of the <strong>largest</strong> square you can obtain from any of the given rectangles.</p>
<p>Return <em>the <strong>number</strong> of rectangles that can make a square with a side length of </em><code>maxLen</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> rectangles = [[5,8],[3,9],[5,12],[16,5]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The largest squares you can get from each rectangle are of lengths [5,3,5,5].
The largest possible square is of length 5, and you can get it out of 3 rectangles.
</pre>

View File

@@ -1,18 +1,18 @@
<p>You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.</p>
<p>Initially the number of elements in A and B are&nbsp;<em>m</em>&nbsp;and&nbsp;<em>n</em> respectively.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong>
A = [1,2,3,0,0,0], m = 3
B = [2,5,6], n = 3
<strong>Output:</strong>&nbsp;[1,2,2,3,5,6]</pre>
<p><strong>Note:</strong></p>
<ul>
<li><code>A.length == n + m</code></li>
</ul>
<p>You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.</p>
<p>Initially the number of elements in A and B are&nbsp;<em>m</em>&nbsp;and&nbsp;<em>n</em> respectively.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong>
A = [1,2,3,0,0,0], m = 3

View File

@@ -1,24 +1,24 @@
<p>Implement a function to check if a binary tree is a binary search tree.</p>
<p><strong>Example&nbsp;1:</strong></p>
<pre>
<strong>Input:</strong>
2
/ \
1 3
<strong>Output:</strong> true
</pre>
<p><strong>Example&nbsp;2:</strong></p>
<pre>
<strong>Input:</strong>
5
/ \
1 4
&nbsp; / \
&nbsp; 3 6
<strong>Output:</strong> false
<strong>Explanation:</strong> Input: [5,1,4,null,null,3,6].
&nbsp; the value of root node is 5, but its right child has value 4.</pre>
<p>Implement a function to check if a binary tree is a binary search tree.</p>
<p><strong>Example&nbsp;1:</strong></p>
<pre>
<strong>Input:</strong>
2
/ \
1 3
<strong>Output:</strong> true
</pre>

View File

@@ -1,29 +1,29 @@
<p>Write an algorithm to find the &quot;next&quot; node (i.e., in-order successor) of a given node in a binary search tree.</p>
<p>Return <code>null</code> if there&#39;s no &quot;next&quot; node for the given node.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> root = <code>[2,1,3], p = 1
2
/ \
1 3
</code>
<strong>Output:</strong> 2</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = <code>[5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/ \
2 4
/
1
</code>
<strong>Output:</strong> null</pre>
<p>Write an algorithm to find the &quot;next&quot; node (i.e., in-order successor) of a given node in a binary search tree.</p>
<p>Return <code>null</code> if there&#39;s no &quot;next&quot; node for the given node.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> root = <code>[2,1,3], p = 1
2
/ \
1 3
</code>
<strong>Output:</strong> 2</pre>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,12 +1,12 @@
<p>Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.</p>
<p>&nbsp;</p>
<p><strong>Example1: </strong></p>
<pre>
<strong>Input: &quot;</strong>tactcoa&quot;
<strong>Output: </strong>truepermutations: &quot;tacocat&quot;&quot;atcocta&quot;, etc.
</pre>
<p>&nbsp;</p>
<p>Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.</p>
<p>&nbsp;</p>
<p><strong>Example1: </strong></p>

View File

@@ -1,22 +1,22 @@
<p>Implement a function to check if a linked list is a palindrome.</p>
<p>&nbsp;</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>1-&gt;2
<strong>Output: </strong> false
</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong>1-&gt;2-&gt;2-&gt;1
<strong>Output: </strong> true
</pre>
<p>&nbsp;</p>
<p><b>Follow up:</b><br />
Could you do it in O(n) time and O(1) space?</p>
<p>Implement a function to check if a linked list is a palindrome.</p>
<p>&nbsp;</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong>1-&gt;2
<strong>Output: </strong> false
</pre>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -36,5 +36,5 @@
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= x, y &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= startValue, target &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -1,24 +1,24 @@
<p>Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure <code>SetOfStacks</code> that mimics this.&nbsp;<code>SetOfStacks</code> should be composed of several stacks and should create a new stack once the previous one exceeds capacity. <code>SetOfStacks.push()</code> and <code>SetOfStacks.pop()</code> should behave identically to a single stack (that is, <code>pop()</code> should return the same values as it would if there were just a single stack). Follow Up: Implement a function <code>popAt(int index)</code> which performs a pop operation on a specific sub-stack.</p>
<p>You should delete the sub-stack when it becomes empty. <code>pop</code>, <code>popAt</code> should return -1 when there&#39;s no element to pop.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;StackOfPlates&quot;, &quot;push&quot;, &quot;push&quot;, &quot;popAt&quot;, &quot;pop&quot;, &quot;pop&quot;]
[[1], [1], [2], [1], [], []]
<strong> Output</strong>:
[null, null, null, 2, 1, -1]
<strong> Explanation</strong>:
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;StackOfPlates&quot;, &quot;push&quot;, &quot;push&quot;, &quot;push&quot;, &quot;popAt&quot;, &quot;popAt&quot;, &quot;popAt&quot;]
[[2], [1], [2], [3], [0], [0], [0]]
<strong> Output</strong>:
[null, null, null, null, 2, 1, 3]
</pre>
<p>Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold. Implement a data structure <code>SetOfStacks</code> that mimics this.&nbsp;<code>SetOfStacks</code> should be composed of several stacks and should create a new stack once the previous one exceeds capacity. <code>SetOfStacks.push()</code> and <code>SetOfStacks.pop()</code> should behave identically to a single stack (that is, <code>pop()</code> should return the same values as it would if there were just a single stack). Follow Up: Implement a function <code>popAt(int index)</code> which performs a pop operation on a specific sub-stack.</p>
<p>You should delete the sub-stack when it becomes empty. <code>pop</code>, <code>popAt</code> should return -1 when there&#39;s no element to pop.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>:
[&quot;StackOfPlates&quot;, &quot;push&quot;, &quot;push&quot;, &quot;popAt&quot;, &quot;pop&quot;, &quot;pop&quot;]
[[1], [1], [2], [1], [], []]
<strong> Output</strong>:
[null, null, null, 2, 1, -1]

View File

@@ -1,23 +1,23 @@
<p>You have a stack of n boxes, with widths wi, depths di, and heights hi. The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Implement a method to compute the height of the tallest possible stack. The height of a stack is the sum of the heights of each box.</p>
<p>The input use <code>[wi, di, hi]</code>&nbsp;to represents each box.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: box = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
<strong> Output</strong>: 6
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: box = [[1, 1, 1], [2, 3, 4], [2, 6, 7], [3, 4, 5]]
<strong> Output</strong>: 10
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>box.length &lt;= 3000</code></li>
</ol>
<p>You have a stack of n boxes, with widths wi, depths di, and heights hi. The boxes cannot be rotated and can only be stacked on top of one another if each box in the stack is strictly larger than the box above it in width, height, and depth. Implement a method to compute the height of the tallest possible stack. The height of a stack is the sum of the heights of each box.</p>
<p>The input use <code>[wi, di, hi]</code>&nbsp;to represents each box.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: box = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
<strong> Output</strong>: 6
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1,20 +1,20 @@
<p>Given a string band an array of smaller strings T, design a method to search b for each small string in T. Output&nbsp;<code>positions</code> of all strings in&nbsp;<code>smalls</code>&nbsp;that appear in <code>big</code>,&nbsp;where <code>positions[i]</code> is all positions of <code>smalls[i]</code>.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
big = &quot;mississippi&quot;
smalls = [&quot;is&quot;,&quot;ppi&quot;,&quot;hi&quot;,&quot;sis&quot;,&quot;i&quot;,&quot;ssippi&quot;]
<strong>Output: </strong> [[1,4],[8],[],[3],[1,4,7,10],[5]]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>0 &lt;= len(big) &lt;= 1000</code></li>
<li><code>0 &lt;= len(smalls[i]) &lt;= 1000</code></li>
<li>The total number of characters in&nbsp;<code>smalls</code>&nbsp;will not exceed 100000.</li>
<li>No duplicated strings in&nbsp;<code>smalls</code>.</li>
<li>All characters are lowercase letters.</li>
</ul>
<p>Given a string band an array of smaller strings T, design a method to search b for each small string in T. Output&nbsp;<code>positions</code> of all strings in&nbsp;<code>smalls</code>&nbsp;that appear in <code>big</code>,&nbsp;where <code>positions[i]</code> is all positions of <code>smalls[i]</code>.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
big = &quot;mississippi&quot;
smalls = [&quot;is&quot;,&quot;ppi&quot;,&quot;hi&quot;,&quot;sis&quot;,&quot;i&quot;,&quot;ssippi&quot;]
<strong>Output: </strong> [[1,4],[8],[],[3],[1,4,7,10],[5]]
</pre>

View File

@@ -1,35 +1,35 @@
<p>You are given a positive integer <code>primeFactors</code>. You are asked to construct a positive integer <code>n</code> that satisfies the following conditions:</p>
<ul>
<li>The number of prime factors of <code>n</code> (not necessarily distinct) is <strong>at most</strong> <code>primeFactors</code>.</li>
<li>The number of nice divisors of <code>n</code> is maximized. Note that a divisor of <code>n</code> is <strong>nice</strong> if it is divisible by every prime factor of <code>n</code>. For example, if <code>n = 12</code>, then its prime factors are <code>[2,2,3]</code>, then <code>6</code> and <code>12</code> are nice divisors, while <code>3</code> and <code>4</code> are not.</li>
</ul>
<p>Return <em>the number of nice divisors of</em> <code>n</code>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>Note that a prime number is a natural number greater than <code>1</code> that is not a product of two smaller natural numbers. The prime factors of a number <code>n</code> is a list of prime numbers such that their product equals <code>n</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> primeFactors = 5
<strong>Output:</strong> 6
<strong>Explanation:</strong> 200 is a valid value of n.
It has 5 prime factors: [2,2,2,5,5], and it has 6 nice divisors: [10,20,40,50,100,200].
There is not other value of n that has at most 5 prime factors and more nice divisors.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> primeFactors = 8
<strong>Output:</strong> 18
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= primeFactors &lt;= 10<sup>9</sup></code></li>
<p>You are given a positive integer <code>primeFactors</code>. You are asked to construct a positive integer <code>n</code> that satisfies the following conditions:</p>
<ul>
<li>The number of prime factors of <code>n</code> (not necessarily distinct) is <strong>at most</strong> <code>primeFactors</code>.</li>
<li>The number of nice divisors of <code>n</code> is maximized. Note that a divisor of <code>n</code> is <strong>nice</strong> if it is divisible by every prime factor of <code>n</code>. For example, if <code>n = 12</code>, then its prime factors are <code>[2,2,3]</code>, then <code>6</code> and <code>12</code> are nice divisors, while <code>3</code> and <code>4</code> are not.</li>
</ul>
<p>Return <em>the number of nice divisors of</em> <code>n</code>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>Note that a prime number is a natural number greater than <code>1</code> that is not a product of two smaller natural numbers. The prime factors of a number <code>n</code> is a list of prime numbers such that their product equals <code>n</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> primeFactors = 5
<strong>Output:</strong> 6
<strong>Explanation:</strong> 200 is a valid value of n.

View File

@@ -1,13 +1,13 @@
<p>Each year, the government releases a list of the 10000 most common baby names and their frequencies (the number of babies with that name). The only problem with this is that some names have multiple spellings. For example,&quot;John&quot; and &#39;&#39;Jon&quot; are essentially the same name but would be listed separately in the list. Given two lists, one of names/frequencies and the other of pairs of equivalent names, write an algorithm to print a new list of the true frequency of each name. Note that if John and Jon are synonyms, and Jon and Johnny are synonyms, then John and Johnny are synonyms. (It is both transitive and symmetric.) In the final list, choose the name that are <strong>lexicographically smallest</strong> as the &quot;real&quot; name.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>names = [&quot;John(15)&quot;,&quot;Jon(12)&quot;,&quot;Chris(13)&quot;,&quot;Kris(4)&quot;,&quot;Christopher(19)&quot;], synonyms = [&quot;(Jon,John)&quot;,&quot;(John,Johnny)&quot;,&quot;(Chris,Kris)&quot;,&quot;(Chris,Christopher)&quot;]
<strong>Output: </strong>[&quot;John(27)&quot;,&quot;Chris(36)&quot;]</pre>
<p>Note:</p>
<ul>
<li><code>names.length &lt;= 100000</code></li>
</ul>
<p>Each year, the government releases a list of the 10000 most common baby names and their frequencies (the number of babies with that name). The only problem with this is that some names have multiple spellings. For example,&quot;John&quot; and &#39;&#39;Jon&quot; are essentially the same name but would be listed separately in the list. Given two lists, one of names/frequencies and the other of pairs of equivalent names, write an algorithm to print a new list of the true frequency of each name. Note that if John and Jon are synonyms, and Jon and Johnny are synonyms, then John and Johnny are synonyms. (It is both transitive and symmetric.) In the final list, choose the name that are <strong>lexicographically smallest</strong> as the &quot;real&quot; name.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>names = [&quot;John(15)&quot;,&quot;Jon(12)&quot;,&quot;Chris(13)&quot;,&quot;Kris(4)&quot;,&quot;Christopher(19)&quot;], synonyms = [&quot;(Jon,John)&quot;,&quot;(John,Johnny)&quot;,&quot;(Chris,Kris)&quot;,&quot;(Chris,Christopher)&quot;]
<strong>Output: </strong>[&quot;John(27)&quot;,&quot;Chris(36)&quot;]</pre>

View File

@@ -1,25 +1,25 @@
<p>Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the &quot;compressed&quot; string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>&quot;aabcccccaaa&quot;
<strong>Output: </strong>&quot;a2b1c5a3&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>&quot;abbccd&quot;
<strong>Output: </strong>&quot;abbccd&quot;
<strong>Explanation: </strong>
The compressed string is &quot;a1b2c2d1&quot;, which is longer than the original string.
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 &lt;= S.length &lt;= 50000</code></li>
</ol>
<p>Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the &quot;compressed&quot; string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>&quot;aabcccccaaa&quot;
<strong>Output: </strong>&quot;a2b1c5a3&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>&quot;abbccd&quot;

View File

@@ -1,23 +1,23 @@
<p>Given two strings, <code>s1</code>&nbsp;and <code>s2</code>, write code to check if <code>s2</code> is a rotation of <code>s1</code> (e.g.,&quot;waterbottle&quot; is a rotation of&quot;erbottlewat&quot;).&nbsp;Can you use&nbsp;only one call to the method that&nbsp;checks if one word is a substring of another?</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>s1 = <span id="example-input-1-1">&quot;waterbottle&quot;</span>, s2 = <span id="example-input-1-2">&quot;</span>erbottlewat<span>&quot;</span>
<strong>Output: </strong><span id="example-output-1">True</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>s1 = &quot;aa&quot;, s2 = &quot;aba&quot;
<strong>Output: </strong>False
</pre>
<p>&nbsp;</p>
<p><strong>Note:</strong></p>
<ol>
<li><code><font face="monospace">0 &lt;= s1.length, s2.length &lt;=&nbsp;</font>100000</code></li>
</ol>
<p>Given two strings, <code>s1</code>&nbsp;and <code>s2</code>, write code to check if <code>s2</code> is a rotation of <code>s1</code> (e.g.,&quot;waterbottle&quot; is a rotation of&quot;erbottlewat&quot;).&nbsp;Can you use&nbsp;only one call to the method that&nbsp;checks if one word is a substring of another?</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>s1 = <span id="example-input-1-1">&quot;waterbottle&quot;</span>, s2 = <span id="example-input-1-2">&quot;</span>erbottlewat<span>&quot;</span>
<strong>Output: </strong><span id="example-output-1">True</span>
</pre>
<p><strong>Example 2:</strong></p>
<pre>

View File

@@ -1,28 +1,32 @@
<p>Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strStr()</a>.</p>
<p>Return the index of the first occurrence of needle in haystack, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
<p>Given two strings <code>needle</code> and <code>haystack</code>, return the index of the first occurrence of <code>needle</code> in <code>haystack</code>, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
<p><strong>Clarification:</strong></p>
<p>What should we return when <code>needle</code> is an empty string? This is a great question to ask during an interview.</p>
<p>For the purpose of this problem, we will return 0 when <code>needle</code> is an empty string. This is consistent to C&#39;s&nbsp;<a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strstr()</a> and Java&#39;s&nbsp;<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_blank">indexOf()</a>.</p>
<p>For the purpose of this problem, we will return 0 when <code>needle</code> is an empty string. This is consistent to C&#39;s <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strstr()</a> and Java&#39;s <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_blank">indexOf()</a>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre><strong>Input:</strong> haystack = "hello", needle = "ll"
<pre>
<strong>Input:</strong> haystack = &quot;hello&quot;, needle = &quot;ll&quot;
<strong>Output:</strong> 2
</pre><p><strong>Example 2:</strong></p>
<pre><strong>Input:</strong> haystack = "aaaaa", needle = "bba"
<strong>Output:</strong> -1
</pre><p><strong>Example 3:</strong></p>
<pre><strong>Input:</strong> haystack = "", needle = ""
<strong>Output:</strong> 0
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> haystack = &quot;aaaaa&quot;, needle = &quot;bba&quot;
<strong>Output:</strong> -1
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 &lt;= haystack.length, needle.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>haystack</code> and&nbsp;<code>needle</code> consist of only lower-case English characters.</li>
<li><code>1 &lt;= haystack.length, needle.length &lt;= 10<sup>4</sup></code></li>
<li><code>haystack</code> and <code>needle</code> consist of only lowercase English characters.</li>
</ul>

View File

@@ -1,37 +1,37 @@
<p>A <strong>sentence</strong> is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.</p>
<p>A sentence can be <strong>shuffled</strong> by appending the <strong>1-indexed word position</strong> to each word then rearranging the words in the sentence.</p>
<ul>
<li>For example, the sentence <code>&quot;This is a sentence&quot;</code> can be shuffled as <code>&quot;sentence4 a3 is2 This1&quot;</code> or <code>&quot;is2 sentence4 This1 a3&quot;</code>.</li>
</ul>
<p>Given a <strong>shuffled sentence</strong> <code>s</code> containing no more than <code>9</code> words, reconstruct and return <em>the original sentence</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;is2 sentence4 This1 a3&quot;
<strong>Output:</strong> &quot;This is a sentence&quot;
<strong>Explanation:</strong> Sort the words in s to their original positions &quot;This1 is2 a3 sentence4&quot;, then remove the numbers.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;Myself2 Me1 I4 and3&quot;
<strong>Output:</strong> &quot;Me Myself and I&quot;
<strong>Explanation:</strong> Sort the words in s to their original positions &quot;Me1 Myself2 and3 I4&quot;, then remove the numbers.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= s.length &lt;= 200</code></li>
<li><code>s</code> consists of lowercase and uppercase English letters, spaces, and digits from <code>1</code> to <code>9</code>.</li>
<li>The number of words in <code>s</code> is between <code>1</code> and <code>9</code>.</li>
<li>The words in <code>s</code> are separated by a single space.</li>
<li><code>s</code> contains no leading or trailing spaces.</li>
<p>A <strong>sentence</strong> is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.</p>
<p>A sentence can be <strong>shuffled</strong> by appending the <strong>1-indexed word position</strong> to each word then rearranging the words in the sentence.</p>
<ul>
<li>For example, the sentence <code>&quot;This is a sentence&quot;</code> can be shuffled as <code>&quot;sentence4 a3 is2 This1&quot;</code> or <code>&quot;is2 sentence4 This1 a3&quot;</code>.</li>
</ul>
<p>Given a <strong>shuffled sentence</strong> <code>s</code> containing no more than <code>9</code> words, reconstruct and return <em>the original sentence</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = &quot;is2 sentence4 This1 a3&quot;
<strong>Output:</strong> &quot;This is a sentence&quot;
<strong>Explanation:</strong> Sort the words in s to their original positions &quot;This1 is2 a3 sentence4&quot;, then remove the numbers.
</pre>

View File

@@ -1,42 +1,42 @@
<p>A split of an integer array is <strong>good</strong> if:</p>
<ul>
<li>The array is split into three <strong>non-empty</strong> contiguous subarrays - named <code>left</code>, <code>mid</code>, <code>right</code> respectively from left to right.</li>
<li>The sum of the elements in <code>left</code> is less than or equal to the sum of the elements in <code>mid</code>, and the sum of the elements in <code>mid</code> is less than or equal to the sum of the elements in <code>right</code>.</li>
</ul>
<p>Given <code>nums</code>, an array of <strong>non-negative</strong> integers, return <em>the number of <strong>good</strong> ways to split</em> <code>nums</code>. As the number may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good way to split nums is [1] [1] [1].</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,2,2,5,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are three good ways of splitting nums:
[1] [2] [2,2,5,0]
[1] [2,2] [2,5,0]
[1,2] [2,2] [5,0]
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,2,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no good way to split nums.</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<p>A split of an integer array is <strong>good</strong> if:</p>
<ul>
<li>The array is split into three <strong>non-empty</strong> contiguous subarrays - named <code>left</code>, <code>mid</code>, <code>right</code> respectively from left to right.</li>
<li>The sum of the elements in <code>left</code> is less than or equal to the sum of the elements in <code>mid</code>, and the sum of the elements in <code>mid</code> is less than or equal to the sum of the elements in <code>right</code>.</li>
</ul>
<p>Given <code>nums</code>, an array of <strong>non-negative</strong> integers, return <em>the number of <strong>good</strong> ways to split</em> <code>nums</code>. As the number may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup>+ 7</code>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The only good way to split nums is [1] [1] [1].</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,2,2,5,0]

View File

@@ -1,14 +1,14 @@
<p>In an array of integers, a &quot;peak&quot; is an element which is greater than or equal to the adjacent integers and a &quot;valley&quot; is an element which is less than or equal to the adjacent inte&shy;gers. For example, in the array {5, 8, 4, 2, 3, 4, 6}, {8, 6} are peaks and {5, 2} are valleys. Given an array of integers, sort the array into an alternating sequence of peaks and valleys.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input: </strong>[5, 3, 1, 2, 3]
<strong>Output:</strong>&nbsp;[5, 1, 3, 2, 3]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>nums.length &lt;= 10000</code></li>
</ul>
<p>In an array of integers, a &quot;peak&quot; is an element which is greater than or equal to the adjacent integers and a &quot;valley&quot; is an element which is less than or equal to the adjacent inte&shy;gers. For example, in the array {5, 8, 4, 2, 3, 4, 6}, {8, 6} are peaks and {5, 2} are valleys. Given an array of integers, sort the array into an alternating sequence of peaks and valleys.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input: </strong>[5, 3, 1, 2, 3]
<strong>Output:</strong>&nbsp;[5, 1, 3, 2, 3]

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,25 +1,25 @@
<p>Given a boolean expression consisting of the symbols <code>0</code> (false), <code>1</code> (true), <code>&amp;</code> (AND), <code>|</code> (OR), and <code>^</code>&nbsp;(XOR), and a desired boolean result value result, implement a function to count the number of ways of parenthesizing the expression such that it evaluates to result.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>s = &quot;1^0|0|1&quot;, result = 0
<strong>Output: </strong>2
<strong>Explanation:</strong>&nbsp;Two possible parenthesizing ways are:
1^(0|(0|1))
1^((0|0)|1)
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong>s = &quot;0&amp;0&amp;0&amp;1^1|0&quot;, result = 1
<strong>Output: </strong>10</pre>
<p><strong>Note: </strong></p>
<ul>
<li>There are no more than&nbsp;19 operators in <code>s</code>.</li>
</ul>
<p>Given a boolean expression consisting of the symbols <code>0</code> (false), <code>1</code> (true), <code>&amp;</code> (AND), <code>|</code> (OR), and <code>^</code>&nbsp;(XOR), and a desired boolean result value result, implement a function to count the number of ways of parenthesizing the expression such that it evaluates to result.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong>s = &quot;1^0|0|1&quot;, result = 0
<strong>Output: </strong>2
<strong>Explanation:</strong>&nbsp;Two possible parenthesizing ways are:
1^(0|(0|1))
1^((0|0)|1)
</pre>

View File

@@ -1,20 +1,20 @@
<p>Write a method to return all subsets of a set. The elements in a set are&nbsp;pairwise distinct.</p>
<p>Note: The result set should not contain duplicated subsets.</p>
<p><strong>Example:</strong></p>
<pre>
<strong> Input</strong>: nums = [1,2,3]
<strong> Output</strong>:
[
[3],
&nbsp; [1],
&nbsp; [2],
&nbsp; [1,2,3],
&nbsp; [1,3],
&nbsp; [2,3],
&nbsp; [1,2],
&nbsp; []
]
</pre>
<p>Write a method to return all subsets of a set. The elements in a set are&nbsp;pairwise distinct.</p>
<p>Note: The result set should not contain duplicated subsets.</p>
<p><strong>Example:</strong></p>
<pre>
<strong> Input</strong>: nums = [1,2,3]
<strong> Output</strong>:
[

View File

@@ -1,22 +1,22 @@
<p>Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that the top and the bottom sides of the square run parallel to the x-axis.</p>
<p>Each square consists of three values,&nbsp;the coordinate of bottom left corner&nbsp;<code>[X,Y] = [square[0],square[1]]</code>, and the side length&nbsp;of the square <code>square[2]</code>. The line will intersect to the two squares in four points. Return the coordinates of two intersection points <code>[X<sub>1</sub>,Y<sub>1</sub>]</code>&nbsp;and&nbsp;<code>[X<sub>2</sub>,Y<sub>2</sub>]</code>&nbsp;that the forming segment covers the other two intersection points in format of <code>{X<sub>1</sub>,Y<sub>1</sub>,X<sub>2</sub>,Y<sub>2</sub>}</code>. If <code>X<sub>1</sub> != X<sub>2</sub></code>, there should be&nbsp;<code>X<sub>1</sub> &lt; X<sub>2</sub></code>, otherwise there should be&nbsp;<code>Y<sub>1</sub> &lt;= Y<sub>2</sub></code>.</p>
<p>If there are more than one line that can cut these two squares in half, return the one that has biggest slope (slope of a line parallel to the y-axis is considered as infinity).</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
square1 = {-1, -1, 2}
square2 = {0, -1, 2}
<strong>Output:</strong> {-1,0,2,0}
<strong>Explanation:</strong> y = 0 is the line that can cut these two squares in half.
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>square.length == 3</code></li>
<li><code>square[2] &gt; 0</code></li>
</ul>
<p>Given two squares on a two-dimensional plane, find a line that would cut these two squares in half. Assume that the top and the bottom sides of the square run parallel to the x-axis.</p>
<p>Each square consists of three values,&nbsp;the coordinate of bottom left corner&nbsp;<code>[X,Y] = [square[0],square[1]]</code>, and the side length&nbsp;of the square <code>square[2]</code>. The line will intersect to the two squares in four points. Return the coordinates of two intersection points <code>[X<sub>1</sub>,Y<sub>1</sub>]</code>&nbsp;and&nbsp;<code>[X<sub>2</sub>,Y<sub>2</sub>]</code>&nbsp;that the forming segment covers the other two intersection points in format of <code>{X<sub>1</sub>,Y<sub>1</sub>,X<sub>2</sub>,Y<sub>2</sub>}</code>. If <code>X<sub>1</sub> != X<sub>2</sub></code>, there should be&nbsp;<code>X<sub>1</sub> &lt; X<sub>2</sub></code>, otherwise there should be&nbsp;<code>Y<sub>1</sub> &lt;= Y<sub>2</sub></code>.</p>
<p>If there are more than one line that can cut these two squares in half, return the one that has biggest slope (slope of a line parallel to the y-axis is considered as infinity).</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
square1 = {-1, -1, 2}

View File

@@ -1,39 +1,39 @@
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, where <code>m</code> and <code>n</code> are both <strong>even</strong> integers, and an integer <code>k</code>.</p>
<p>The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png" style="width: 231px; height: 258px;" /></p>
<p>A cyclic rotation of the matrix is done by cyclically rotating <strong>each layer</strong> in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the <strong>counter-clockwise</strong> direction. An example rotation is shown below:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg" style="width: 500px; height: 268px;" />
<p>Return <em>the matrix after applying </em><code>k</code> <em>cyclic rotations to it</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/rod2.png" style="width: 421px; height: 191px;" />
<pre>
<strong>Input:</strong> grid = [[40,10],[30,20]], k = 1
<strong>Output:</strong> [[10,20],[40,30]]
<strong>Explanation:</strong> The figures above represent the grid at every state.
</pre>
<p><strong>Example 2:</strong></p>
<strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid5.png" style="width: 231px; height: 262px;" /></strong> <strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid6.png" style="width: 231px; height: 262px;" /></strong> <strong><img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid7.png" style="width: 231px; height: 262px;" /></strong>
<pre>
<strong>Input:</strong> grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]], k = 2
<strong>Output:</strong> [[3,4,8,12],[2,11,10,16],[1,7,6,15],[5,9,13,14]]
<strong>Explanation:</strong> The figures above represent the grid at every state.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>2 &lt;= m, n &lt;= 50</code></li>
<li>Both <code>m</code> and <code>n</code> are <strong>even</strong> integers.</li>
<li><code>1 &lt;= grid[i][j] &lt;=<sup> </sup>5000</code></li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>, where <code>m</code> and <code>n</code> are both <strong>even</strong> integers, and an integer <code>k</code>.</p>
<p>The matrix is composed of several layers, which is shown in the below image, where each color is its own layer:</p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/06/10/ringofgrid.png" style="width: 231px; height: 258px;" /></p>
<p>A cyclic rotation of the matrix is done by cyclically rotating <strong>each layer</strong> in the matrix. To cyclically rotate a layer once, each element in the layer will take the place of the adjacent element in the <strong>counter-clockwise</strong> direction. An example rotation is shown below:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/22/explanation_grid.jpg" style="width: 500px; height: 268px;" />
<p>Return <em>the matrix after applying </em><code>k</code> <em>cyclic rotations to it</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/19/rod2.png" style="width: 421px; height: 191px;" />
<pre>
<strong>Input:</strong> grid = [[40,10],[30,20]], k = 1
<strong>Output:</strong> [[10,20],[40,30]]
<strong>Explanation:</strong> The figures above represent the grid at every state.
</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1,23 +1,23 @@
<p>Oh, no! You have accidentally removed all spaces, punctuation, and capitalization in a lengthy document. A sentence like &quot;I reset the computer. It still didn&#39;t boot!&quot; became &quot;iresetthecomputeritstilldidntboot&#39;&#39;. You&#39;ll deal with the punctuation and capi&shy;talization later; right now you need to re-insert the spaces. Most of the words are in a dictionary but a few are not. Given a dictionary (a list of strings) and the document (a string), design an algorithm to unconcatenate the document in a way that minimizes the number of unrecognized characters. Return the number of unrecognized characters.</p>
<p><strong>Note: </strong>This&nbsp;problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
dictionary = [&quot;looked&quot;,&quot;just&quot;,&quot;like&quot;,&quot;her&quot;,&quot;brother&quot;]
sentence = &quot;jesslookedjustliketimherbrother&quot;
<strong>Output: </strong> 7
<strong>Explanation: </strong> After unconcatenating, we got &quot;<strong>jess</strong> looked just like <strong>tim</strong> her brother&quot;, which containing 7 unrecognized characters.
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>0 &lt;= len(sentence) &lt;= 1000</code></li>
<li><code><font face="sans-serif, Arial, Verdana, Trebuchet MS">The total number of characters in&nbsp;</font>dictionary</code>&nbsp;is less than or equal to 150000.</li>
<li>There are only lowercase letters in&nbsp;<code>dictionary</code>&nbsp;and&nbsp;<code>sentence</code>.</li>
</ul>
<p>Oh, no! You have accidentally removed all spaces, punctuation, and capitalization in a lengthy document. A sentence like &quot;I reset the computer. It still didn&#39;t boot!&quot; became &quot;iresetthecomputeritstilldidntboot&#39;&#39;. You&#39;ll deal with the punctuation and capi&shy;talization later; right now you need to re-insert the spaces. Most of the words are in a dictionary but a few are not. Given a dictionary (a list of strings) and the document (a string), design an algorithm to unconcatenate the document in a way that minimizes the number of unrecognized characters. Return the number of unrecognized characters.</p>
<p><strong>Note: </strong>This&nbsp;problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>
dictionary = [&quot;looked&quot;,&quot;just&quot;,&quot;like&quot;,&quot;her&quot;,&quot;brother&quot;]
sentence = &quot;jesslookedjustliketimherbrother&quot;

View File

@@ -32,8 +32,8 @@ solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 200</code></li>
<li><code>1 &lt;= nums.length &lt;= 50</code></li>
<li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li>All the elements of <code>nums</code> are <strong>unique</strong>.</li>
<li>At most <code>5 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li>
<li>At most <code>10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>reset</code> and <code>shuffle</code>.</li>
</ul>

View File

@@ -1,45 +1,45 @@
<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 &lt;= i &lt; n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.
3) Friend 2 leaves the circle. Next start is friend 3.
4) Count 2 friends clockwise, which are friends 3 and 4.
5) Friend 4 leaves the circle. Next start is friend 5.
6) Count 2 friends clockwise, which are friends 5 and 1.
7) Friend 1 leaves the circle. Next start is friend 3.
8) Count 2 friends clockwise, which are friends 3 and 5.
9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 6, k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= n &lt;= 500</code></li>
<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 &lt;= i &lt; n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
<p>The rules of the game are as follows:</p>
<ol>
<li><strong>Start</strong> at the <code>1<sup>st</sup></code> friend.</li>
<li>Count the next <code>k</code> friends in the clockwise direction <strong>including</strong> the friend you started at. The counting wraps around the circle and may count some friends more than once.</li>
<li>The last friend you counted leaves the circle and loses the game.</li>
<li>If there is still more than one friend in the circle, go back to step <code>2</code> <strong>starting</strong> from the friend <strong>immediately clockwise</strong> of the friend who just lost and repeat.</li>
<li>Else, the last friend in the circle wins the game.</li>
</ol>
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the winner of the game</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/25/ic234-q2-ex11.png" style="width: 500px; height: 345px;" />
<pre>
<strong>Input:</strong> n = 5, k = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> Here are the steps of the game:
1) Start at friend 1.
2) Count 2 friends clockwise, which are friends 1 and 2.

View File

@@ -10,7 +10,7 @@
<strong>Output:</strong> [101,111,121,131,141,999]
<strong>Explanation:</strong>
The first few palindromes of length 3 are:
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 201, ...
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ...
The 90<sup>th</sup> palindrome of length 3 is 999.
</pre>

View File

@@ -1,15 +1,15 @@
<p>Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n pairs of parentheses.</p>
<p>Note: The result set should not contain duplicated subsets.</p>
<p>For example, given&nbsp;n = 3, the result should be:</p>
<pre>
[
&quot;((()))&quot;,
&quot;(()())&quot;,
&quot;(())()&quot;,
&quot;()(())&quot;,
&quot;()()()&quot;
]
</pre>
<p>Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n pairs of parentheses.</p>
<p>Note: The result set should not contain duplicated subsets.</p>
<p>For example, given&nbsp;n = 3, the result should be:</p>
<pre>
[

View File

@@ -1,29 +1,29 @@
<p>A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any adjacent requests. Given a sequence of back-to-back appoint&shy; ment requests, find the optimal (highest total booked minutes) set the masseuse can honor. Return the number of minutes.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong> [1,2,3,1]
<strong>Output: </strong> 4
<strong>Explanation: </strong> Accept request 1 and 3, total minutes = 1 + 3 = 4
</pre>
<p><strong>Example 2: </strong></p>
<pre>
<strong>Input: </strong> [2,7,9,3,1]
<strong>Output: </strong> 12
<strong>Explanation: </strong> Accept request 1, 3 and 5, total minutes = 2 + 9 + 1 = 12
</pre>
<p><strong>Example 3: </strong></p>
<pre>
<strong>Input: </strong> [2,1,4,5,3,1,1,3]
<strong>Output: </strong> 12
<strong>Explanation: </strong> Accept request 1, 3, 5 and 8, total minutes = 2 + 4 + 3 + 3 = 12
</pre>
<p>A popular masseuse receives a sequence of back-to-back appointment requests and is debating which ones to accept. She needs a break between appointments and therefore she cannot accept any adjacent requests. Given a sequence of back-to-back appoint&shy; ment requests, find the optimal (highest total booked minutes) set the masseuse can honor. Return the number of minutes.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p>&nbsp;</p>
<p><strong>Example 1: </strong></p>
<pre>
<strong>Input: </strong> [1,2,3,1]
<strong>Output: </strong> 4
<strong>Explanation: </strong> Accept request 1 and 3, total minutes = 1 + 3 = 4
</pre>
<p><strong>Example 2: </strong></p>

View File

@@ -1,19 +1,19 @@
<p>Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.</p>
<p><strong>Example:</strong></p>
<p>Given matrix:</p>
<pre>
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
</pre>
<p>Given target&nbsp;=&nbsp;5,&nbsp;return&nbsp;<code>true.</code></p>
<p>Given target&nbsp;=&nbsp;20, return&nbsp;<code>false.</code></p>
<p>Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.</p>
<p><strong>Example:</strong></p>
<p>Given matrix:</p>
<pre>
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],

View File

@@ -1,15 +1,15 @@
<p>You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because M could not fully fit between bit 3 and bit 2.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: N = 10000000000, M = 10011, i = 2, j = 6
<strong> Output</strong>: N = 10001001100
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: N = 0, M = 11111, i = 0, j = 4
<strong> Output</strong>: N = 11111
</pre>
<p>You are given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. That is, if M = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because M could not fully fit between bit 3 and bit 2.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: N = 10000000000, M = 10011, i = 2, j = 6
<strong> Output</strong>: N = 10001001100
</pre>

View File

@@ -1,21 +1,21 @@
<p>Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. If there are more than one target elements in the array, return the smallest index.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 5
<strong> Output</strong>: 8 (the index of 5 in the array)
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 11
<strong> Output</strong>: -1 (not found)
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>1 &lt;= arr.length &lt;= 1000000</code></li>
</ol>
<p>Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. If there are more than one target elements in the array, return the smallest index.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: arr = [15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], target = 5
<strong> Output</strong>: 8 (the index of 5 in the array)
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1,20 +1,20 @@
<p>Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up the rank of a number <code>x</code> (the number of values less than or equal to <code>x</code>). lmplement the data structures and algorithms to support these operations. That is, implement the method <code>track (int x)</code>, which is called when each number is generated, and the method <code>getRankOfNumber(int x)</code>, which returns the number of values less than or equal to <code>x</code>.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong>
[&quot;StreamRank&quot;, &quot;getRankOfNumber&quot;, &quot;track&quot;, &quot;getRankOfNumber&quot;]
[[], [1], [0], [0]]
<strong>Output:
</strong>[null,0,null,1]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>x &lt;= 50000</code></li>
<li>The number of calls of both&nbsp;<code>track</code>&nbsp;and&nbsp;<code>getRankOfNumber</code>&nbsp;methods are less than or equal to 2000.</li>
</ul>
<p>Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up the rank of a number <code>x</code> (the number of values less than or equal to <code>x</code>). lmplement the data structures and algorithms to support these operations. That is, implement the method <code>track (int x)</code>, which is called when each number is generated, and the method <code>getRankOfNumber(int x)</code>, which returns the number of values less than or equal to <code>x</code>.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:</strong>
[&quot;StreamRank&quot;, &quot;getRankOfNumber&quot;, &quot;track&quot;, &quot;getRankOfNumber&quot;]
[[], [1], [0], [0]]

View File

@@ -1,19 +1,19 @@
<p>Design an algorithm to find all pairs of integers within an array which sum to a specified value.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,5], target = 11
<strong>Output: </strong>[[5,6]]</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,5,6], target = 11
<strong>Output: </strong>[[5,6],[5,6]]</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>nums.length &lt;= 100000</code></li>
</ul>
<p>Design an algorithm to find all pairs of integers within an array which sum to a specified value.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,5], target = 11
<strong>Output: </strong>[[5,6]]</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,43 +1,43 @@
<p>The <strong>pair sum</strong> of a pair <code>(a,b)</code> is equal to <code>a + b</code>. The <strong>maximum pair sum</strong> is the largest <strong>pair sum</strong> in a list of pairs.</p>
<ul>
<li>For example, if we have pairs <code>(1,5)</code>, <code>(2,3)</code>, and <code>(4,4)</code>, the <strong>maximum pair sum</strong> would be <code>max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8</code>.</li>
</ul>
<p>Given an array <code>nums</code> of <strong>even</strong> length <code>n</code>, pair up the elements of <code>nums</code> into <code>n / 2</code> pairs such that:</p>
<ul>
<li>Each element of <code>nums</code> is in <strong>exactly one</strong> pair, and</li>
<li>The <strong>maximum pair sum </strong>is <strong>minimized</strong>.</li>
</ul>
<p>Return <em>the minimized <strong>maximum pair sum</strong> after optimally pairing up the elements</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The elements can be paired up into pairs (3,3) and (5,2).
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,4,2,4,6]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The elements can be paired up into pairs (3,5), (4,4), and (6,2).
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>n</code> is <strong>even</strong>.</li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<p>The <strong>pair sum</strong> of a pair <code>(a,b)</code> is equal to <code>a + b</code>. The <strong>maximum pair sum</strong> is the largest <strong>pair sum</strong> in a list of pairs.</p>
<ul>
<li>For example, if we have pairs <code>(1,5)</code>, <code>(2,3)</code>, and <code>(4,4)</code>, the <strong>maximum pair sum</strong> would be <code>max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8</code>.</li>
</ul>
<p>Given an array <code>nums</code> of <strong>even</strong> length <code>n</code>, pair up the elements of <code>nums</code> into <code>n / 2</code> pairs such that:</p>
<ul>
<li>Each element of <code>nums</code> is in <strong>exactly one</strong> pair, and</li>
<li>The <strong>maximum pair sum </strong>is <strong>minimized</strong>.</li>
</ul>
<p>Return <em>the minimized <strong>maximum pair sum</strong> after optimally pairing up the elements</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,5,2,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The elements can be paired up into pairs (3,3) and (5,2).

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,26 +1,26 @@
<p>Given any integer, print an English phrase that describes the integer (e.g., &quot;One Thousand Two Hundred Thirty Four&quot;).</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> 123
<strong>Output:</strong> &quot;One Hundred Twenty Three&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> 12345
<strong>Output:</strong> &quot;Twelve Thousand Three Hundred Forty Five&quot;</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> 1234567
<strong>Output:</strong> &quot;One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven&quot;</pre>
<p><strong>Example 4:</strong></p>
<pre>
<strong>Input:</strong> 1234567891
<strong>Output:</strong> &quot;One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One&quot;</pre>
<p>Given any integer, print an English phrase that describes the integer (e.g., &quot;One Thousand Two Hundred Thirty Four&quot;).</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> 123
<strong>Output:</strong> &quot;One Hundred Twenty Three&quot;
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> 12345

View File

@@ -1,21 +1,21 @@
<p>Write a function to determine the number of bits you would need to flip to convert integer A to integer B.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: A = 29 (0b11101), B = 15 (0b01111)
<strong> Output</strong>: 2
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: A = 1B = 2
<strong> Output</strong>: 2
</pre>
<p><strong>Note:</strong></p>
<ol>
<li><code>-2147483648 &lt;= A, B &lt;= 2147483647</code></li>
</ol>
<p>Write a function to determine the number of bits you would need to flip to convert integer A to integer B.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: A = 29 (0b11101), B = 15 (0b01111)
<strong> Output</strong>: 2
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1,64 +1,64 @@
<p>You are given an <code>m x n</code> matrix of characters <code>box</code> representing a side-view of a box. Each cell of the box is one of the following:</p>
<ul>
<li>A stone <code>&#39;#&#39;</code></li>
<li>A stationary obstacle <code>&#39;*&#39;</code></li>
<li>Empty <code>&#39;.&#39;</code></li>
</ul>
<p>The box is rotated <strong>90 degrees clockwise</strong>, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity <strong>does not</strong> affect the obstacles&#39; positions, and the inertia from the box&#39;s rotation <strong>does not </strong>affect the stones&#39; horizontal positions.</p>
<p>It is <strong>guaranteed</strong> that each stone in <code>box</code> rests on an obstacle, another stone, or the bottom of the box.</p>
<p>Return <em>an </em><code>n x m</code><em> matrix representing the box after the rotation described above</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcodewithstones.png" style="width: 300px; height: 150px;" /></p>
<pre>
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;#&quot;]]
<strong>Output:</strong> [[&quot;.&quot;],
&nbsp; [&quot;#&quot;],
&nbsp; [&quot;#&quot;]]
</pre>
<p><strong>Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode2withstones.png" style="width: 375px; height: 195px;" /></p>
<pre>
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;]]
<strong>Output:</strong> [[&quot;#&quot;,&quot;.&quot;],
&nbsp; [&quot;#&quot;,&quot;#&quot;],
&nbsp; [&quot;*&quot;,&quot;*&quot;],
&nbsp; [&quot;.&quot;,&quot;.&quot;]]
</pre>
<p><strong>Example 3:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode3withstone.png" style="width: 400px; height: 218px;" /></p>
<pre>
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;*&quot;,&quot;.&quot;,&quot;.&quot;],
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;.&quot;]]
<strong>Output:</strong> [[&quot;.&quot;,&quot;#&quot;,&quot;#&quot;],
&nbsp; [&quot;.&quot;,&quot;#&quot;,&quot;#&quot;],
&nbsp; [&quot;#&quot;,&quot;#&quot;,&quot;*&quot;],
&nbsp; [&quot;#&quot;,&quot;*&quot;,&quot;.&quot;],
&nbsp; [&quot;#&quot;,&quot;.&quot;,&quot;*&quot;],
&nbsp; [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;]]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == box.length</code></li>
<li><code>n == box[i].length</code></li>
<li><code>1 &lt;= m, n &lt;= 500</code></li>
<li><code>box[i][j]</code> is either <code>&#39;#&#39;</code>, <code>&#39;*&#39;</code>, or <code>&#39;.&#39;</code>.</li>
<p>You are given an <code>m x n</code> matrix of characters <code>box</code> representing a side-view of a box. Each cell of the box is one of the following:</p>
<ul>
<li>A stone <code>&#39;#&#39;</code></li>
<li>A stationary obstacle <code>&#39;*&#39;</code></li>
<li>Empty <code>&#39;.&#39;</code></li>
</ul>
<p>The box is rotated <strong>90 degrees clockwise</strong>, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity <strong>does not</strong> affect the obstacles&#39; positions, and the inertia from the box&#39;s rotation <strong>does not </strong>affect the stones&#39; horizontal positions.</p>
<p>It is <strong>guaranteed</strong> that each stone in <code>box</code> rests on an obstacle, another stone, or the bottom of the box.</p>
<p>Return <em>an </em><code>n x m</code><em> matrix representing the box after the rotation described above</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcodewithstones.png" style="width: 300px; height: 150px;" /></p>
<pre>
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;#&quot;]]
<strong>Output:</strong> [[&quot;.&quot;],
&nbsp; [&quot;#&quot;],
&nbsp; [&quot;#&quot;]]
</pre>
<p><strong>Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/08/rotatingtheboxleetcode2withstones.png" style="width: 375px; height: 195px;" /></p>
<pre>
<strong>Input:</strong> box = [[&quot;#&quot;,&quot;.&quot;,&quot;*&quot;,&quot;.&quot;],

View File

@@ -1,41 +1,41 @@
<p>Given an image represented by an N x N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
Given <strong>matrix</strong> =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
Rotate the matrix <strong>in place. </strong>It becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
</pre>
<p><strong>Example 2:</strong></p>
<pre>
Given <strong>matrix</strong> =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
Rotate the matrix <strong>in place. </strong>It becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
</pre>
<p>Given an image represented by an N x N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
Given <strong>matrix</strong> =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
Rotate the matrix <strong>in place. </strong>It becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
</pre>

View File

@@ -1,22 +1,22 @@
<p>Write a method to compute all permutations of a string of unique characters.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: S = &quot;qwe&quot;
<strong> Output</strong>: [&quot;qwe&quot;, &quot;qew&quot;, &quot;wqe&quot;, &quot;weq&quot;, &quot;ewq&quot;, &quot;eqw&quot;]
</pre>
<p><strong>Example2:</strong></p>
<pre>
<strong> Input</strong>: S = &quot;ab&quot;
<strong> Output</strong>: [&quot;ab&quot;, &quot;ba&quot;]
</pre>
<p><strong>Note:</strong></p>
<ol>
<li>All charaters are English letters.</li>
<li><code>1 &lt;= S.length &lt;= 9</code></li>
</ol>
<p>Write a method to compute all permutations of a string of unique characters.</p>
<p><strong>Example1:</strong></p>
<pre>
<strong> Input</strong>: S = &quot;qwe&quot;
<strong> Output</strong>: [&quot;qwe&quot;, &quot;qew&quot;, &quot;wqe&quot;, &quot;weq&quot;, &quot;ewq&quot;, &quot;eqw&quot;]
</pre>
<p><strong>Example2:</strong></p>

View File

@@ -1 +1 @@
<p>English description is not available for the problem. Please switch to Chinese.</p>
<p>English description is not available for the problem. Please switch to Chinese.</p>

View File

@@ -1,18 +1,18 @@
<p>Given a two-dimensional graph with points on it, find a line which passes the most number of points.</p>
<p>Assume all the points that passed by the line are stored in list <code>S</code>&nbsp;sorted by their number. You need to return <code>[S[0], S[1]]</code>, that is , two points that have smallest number. If there are more than one line that passes the most number of points, choose the one that has the smallest <code>S[0].</code>&nbsp;If there are more that one line that has the same <code>S[0]</code>, choose the one that has smallest <code>S[1]</code>.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> [[0,0],[1,1],[1,0],[2,0]]
<strong>Output: </strong> [0,2]
<strong>Explanation: </strong> The numbers of points passed by the line are [0,2,3].
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>2 &lt;= len(Points) &lt;= 300</code></li>
<li><code>len(Points[i]) = 2</code></li>
</ul>
<p>Given a two-dimensional graph with points on it, find a line which passes the most number of points.</p>
<p>Assume all the points that passed by the line are stored in list <code>S</code>&nbsp;sorted by their number. You need to return <code>[S[0], S[1]]</code>, that is , two points that have smallest number. If there are more than one line that passes the most number of points, choose the one that has the smallest <code>S[0].</code>&nbsp;If there are more that one line that has the same <code>S[0]</code>, choose the one that has smallest <code>S[1]</code>.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> [[0,0],[1,1],[1,0],[2,0]]
<strong>Output: </strong> [0,2]

View File

@@ -1,45 +1,45 @@
<p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</p>
<ul>
<li>For example, the alternating sum of <code>[4,2,5,3]</code> is <code>(4 + 5) - (2 + 3) = 4</code>.</li>
</ul>
<p>Given an array <code>nums</code>, return <em>the <strong>maximum alternating sum</strong> of any subsequence of </em><code>nums</code><em> (after <strong>reindexing</strong> the elements of the subsequence)</em>.</p>
<ul>
</ul>
<p>A <strong>subsequence</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements&#39; relative order. For example, <code>[2,7,4]</code> is a subsequence of <code>[4,<u>2</u>,3,<u>7</u>,2,1,<u>4</u>]</code> (the underlined elements), while <code>[2,4,2]</code> is not.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>4</u>,<u>2</u>,<u>5</u>,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6,7,<u>8</u>]
<strong>Output:</strong> 8
<strong>Explanation:</strong> It is optimal to choose the subsequence [8] with alternating sum 8.
</pre>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>6</u>,2,<u>1</u>,2,4,<u>5</u>]
<strong>Output:</strong> 10
<strong>Explanation:</strong> It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
<p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</p>
<ul>
<li>For example, the alternating sum of <code>[4,2,5,3]</code> is <code>(4 + 5) - (2 + 3) = 4</code>.</li>
</ul>
<p>Given an array <code>nums</code>, return <em>the <strong>maximum alternating sum</strong> of any subsequence of </em><code>nums</code><em> (after <strong>reindexing</strong> the elements of the subsequence)</em>.</p>
<ul>
</ul>
<p>A <strong>subsequence</strong> of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements&#39; relative order. For example, <code>[2,7,4]</code> is a subsequence of <code>[4,<u>2</u>,3,<u>7</u>,2,1,<u>4</u>]</code> (the underlined elements), while <code>[2,4,2]</code> is not.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [<u>4</u>,<u>2</u>,<u>5</u>,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong> It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
</pre>
<p><strong>Example 2:</strong></p>

View File

@@ -1,21 +1,21 @@
<p>Given an NxM matrix of positive and negative integers, write code to find the submatrix with the largest possible sum.</p>
<p>Return an array&nbsp;<code>[r1, c1, r2, c2]</code>, where&nbsp;<code>r1</code>, <code>c1</code> are the row number and the column number of the submatrix&#39;s upper left corner respectively, and&nbsp;<code>r2</code>, <code>c2</code> are the row number of and the column number of lower right corner. If there are more than one answers, return any one of them.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:
</strong><code>[
&nbsp; [-1,<strong>0</strong>],
&nbsp; [0,-1]
]</code>
<strong>Output: </strong>[0,1,0,1]</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>1 &lt;= matrix.length, matrix[0].length &lt;= 200</code></li>
</ul>
<p>Given an NxM matrix of positive and negative integers, write code to find the submatrix with the largest possible sum.</p>
<p>Return an array&nbsp;<code>[r1, c1, r2, c2]</code>, where&nbsp;<code>r1</code>, <code>c1</code> are the row number and the column number of the submatrix&#39;s upper left corner respectively, and&nbsp;<code>r2</code>, <code>c2</code> are the row number of and the column number of lower right corner. If there are more than one answers, return any one of them.</p>
<p><b>Note:&nbsp;</b>This problem is slightly different from the original one in the book.</p>
<p><strong>Example:</strong></p>
<pre>
<strong>Input:
</strong><code>[

View File

@@ -1,8 +1,8 @@
<p>Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> a = 1, b = 2
<strong>Output: </strong> 2
</pre>
<p>Write a method that finds the maximum of two numbers. You should not use if-else or any other comparison operator.</p>
<p><strong>Example: </strong></p>

View File

@@ -1,34 +1,34 @@
<p>Imagine you have a square matrix, where each cell (pixel) is either black or white Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.</p>
<p>Return an array&nbsp;<code>[r, c, size]</code>, where&nbsp;<code>r</code>,&nbsp;<code>c</code>&nbsp;are the row number and the column number of the subsquare&#39;s upper left corner respectively, and <code>size</code>&nbsp;is the side length of the subsquare. If there are more than one answers, return the one that has smallest <code>r</code>. If there are more than one answers that have the same <code>r</code>, return the one that has smallest <code>c</code>. If there&#39;s no answer, return an empty array.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:
</strong>[
&nbsp; [1,0,1],
&nbsp; [<strong>0,0</strong>,1],
&nbsp; [<strong>0,0</strong>,1]
]
<strong>Output: </strong>[1,0,2]
<strong>Explanation:</strong> 0 represents black, and 1 represents white, bold elements in the input is the answer.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:
</strong>[
&nbsp; [<strong>0</strong>,1,1],
&nbsp; [1,0,1],
&nbsp; [1,1,0]
]
<strong>Output: </strong>[0,0,1]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>matrix.length == matrix[0].length &lt;= 200</code></li>
</ul>
<p>Imagine you have a square matrix, where each cell (pixel) is either black or white Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.</p>
<p>Return an array&nbsp;<code>[r, c, size]</code>, where&nbsp;<code>r</code>,&nbsp;<code>c</code>&nbsp;are the row number and the column number of the subsquare&#39;s upper left corner respectively, and <code>size</code>&nbsp;is the side length of the subsquare. If there are more than one answers, return the one that has smallest <code>r</code>. If there are more than one answers that have the same <code>r</code>, return the one that has smallest <code>c</code>. If there&#39;s no answer, return an empty array.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:
</strong>[
&nbsp; [1,0,1],
&nbsp; [<strong>0,0</strong>,1],
&nbsp; [<strong>0,0</strong>,1]
]
<strong>Output: </strong>[1,0,2]
<strong>Explanation:</strong> 0 represents black, and 1 represents white, bold elements in the input is the answer.
</pre>

View File

@@ -1,15 +1,15 @@
<p>Design an algorithm to find the smallest K numbers in an array.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> arr = [1,3,5,7,2,4,6,8], k = 4
<strong>Output: </strong> [1,2,3,4]
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>0 &lt;= len(arr) &lt;= 100000</code></li>
<li><code>0 &lt;= k &lt;= min(100000, len(arr))</code></li>
</ul>
<p>Design an algorithm to find the smallest K numbers in an array.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong> arr = [1,3,5,7,2,4,6,8], k = 4
<strong>Output: </strong> [1,2,3,4]
</pre>

View File

@@ -1,45 +1,45 @@
<p>You are a hiker preparing for an upcoming hike. You are given <code>heights</code>, a 2D array of size <code>rows x columns</code>, where <code>heights[row][col]</code> represents the height of cell <code>(row, col)</code>. You are situated in the top-left cell, <code>(0, 0)</code>, and you hope to travel to the bottom-right cell, <code>(rows-1, columns-1)</code> (i.e.,&nbsp;<strong>0-indexed</strong>). You can move <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>, and you wish to find a route that requires the minimum <strong>effort</strong>.</p>
<p>A route&#39;s <strong>effort</strong> is the <strong>maximum absolute difference</strong><strong> </strong>in heights between two consecutive cells of the route.</p>
<p>Return <em>the minimum <strong>effort</strong> required to travel from the top-left cell to the bottom-right cell.</em></p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" /></p>
<pre>
<strong>Input:</strong> heights = [[1,2,2],[3,8,2],[5,3,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
</pre>
<p><strong>Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" /></p>
<pre>
<strong>Input:</strong> heights = [[1,2,3],[3,8,4],[5,3,5]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
</pre>
<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex3.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> This route does not require any effort.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>rows == heights.length</code></li>
<li><code>columns == heights[i].length</code></li>
<li><code>1 &lt;= rows, columns &lt;= 100</code></li>
<li><code>1 &lt;= heights[i][j] &lt;= 10<sup>6</sup></code></li>
<p>You are a hiker preparing for an upcoming hike. You are given <code>heights</code>, a 2D array of size <code>rows x columns</code>, where <code>heights[row][col]</code> represents the height of cell <code>(row, col)</code>. You are situated in the top-left cell, <code>(0, 0)</code>, and you hope to travel to the bottom-right cell, <code>(rows-1, columns-1)</code> (i.e.,&nbsp;<strong>0-indexed</strong>). You can move <strong>up</strong>, <strong>down</strong>, <strong>left</strong>, or <strong>right</strong>, and you wish to find a route that requires the minimum <strong>effort</strong>.</p>
<p>A route&#39;s <strong>effort</strong> is the <strong>maximum absolute difference</strong><strong> </strong>in heights between two consecutive cells of the route.</p>
<p>Return <em>the minimum <strong>effort</strong> required to travel from the top-left cell to the bottom-right cell.</em></p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex1.png" style="width: 300px; height: 300px;" /></p>
<pre>
<strong>Input:</strong> heights = [[1,2,2],[3,8,2],[5,3,5]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
</pre>
<p><strong>Example 2:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/04/ex2.png" style="width: 300px; height: 300px;" /></p>
<pre>

View File

@@ -1,16 +1,16 @@
<p>Given two arrays of integers, compute the pair of values (one value in each array) with the smallest (non-negative) difference. Return the difference.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
<strong>Output: </strong> 3, the pair (11, 8)
</pre>
<p><strong>Note: </strong></p>
<ul>
<li><code>1 &lt;= a.length, b.length &lt;= 100000</code></li>
<li><code>-2147483648 &lt;= a[i], b[i] &lt;= 2147483647</code></li>
<li>The result is in the range [0, 2147483647]</li>
</ul>
<p>Given two arrays of integers, compute the pair of values (one value in each array) with the smallest (non-negative) difference. Return the difference.</p>
<p><strong>Example: </strong></p>
<pre>
<strong>Input: </strong>{1, 3, 15, 11, 2}, {23, 127, 235, 19, 8}
<strong>Output: </strong> 3, the pair (11, 8)
</pre>

Some files were not shown because too many files have changed in this diff Show More