mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of </em><code>a</code><em> and </em><code>b</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The common factors of 12 and 6 are 1, 2, 3, 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 25, b = 30
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The common factors of 25 and 30 are 1, 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>word</code>, consisting of lowercase English letters. You need to select <strong>one</strong> index and <strong>remove</strong> the letter at that index from <code>word</code> so that the <strong>frequency</strong> of every letter present in <code>word</code> is equal.</p>
|
||||
|
||||
<p>Return<em> </em><code>true</code><em> if it is possible to remove one letter so that the frequency of all letters in </em><code>word</code><em> are equal, and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The <b>frequency</b> of a letter <code>x</code> is the number of times it occurs in the string.</li>
|
||||
<li>You <strong>must</strong> remove exactly one letter and cannot chose to do nothing.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "abcc"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word = "aazz"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= word.length <= 100</code></li>
|
||||
<li><code>word</code> consists of lowercase English letters only.</li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given a string <code>s</code> consisting of only lowercase English letters. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Delete <strong>the entire string</strong> <code>s</code>, or</li>
|
||||
<li>Delete the <strong>first</strong> <code>i</code> letters of <code>s</code> if the first <code>i</code> letters of <code>s</code> are <strong>equal</strong> to the following <code>i</code> letters in <code>s</code>, for any <code>i</code> in the range <code>1 <= i <= s.length / 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>For example, if <code>s = "ababc"</code>, then in one operation, you could delete the first two letters of <code>s</code> to get <code>"abc"</code>, since the first two letters of <code>s</code> and the following two letters of <code>s</code> are both equal to <code>"ab"</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of operations needed to delete all of </em><code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabcdabc"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
|
||||
- Delete all the letters.
|
||||
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
|
||||
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaabaab"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
|
||||
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
|
||||
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
|
||||
- Delete all the letters.
|
||||
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aaaaa"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> In each operation, we can delete the first letter of s.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 4000</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given two <strong>0-indexed</strong> arrays, <code>nums1</code> and <code>nums2</code>, consisting of non-negative integers. There exists another array, <code>nums3</code>, which contains the bitwise XOR of <strong>all pairings</strong> of integers between <code>nums1</code> and <code>nums2</code> (every integer in <code>nums1</code> is paired with every integer in <code>nums2</code> <strong>exactly once</strong>).</p>
|
||||
|
||||
<p>Return<em> the <strong>bitwise XOR</strong> of all integers in </em><code>nums3</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [2,1,3], nums2 = [10,2,5,0]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong>
|
||||
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
|
||||
The bitwise XOR of all these numbers is 13, so we return 13.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [3,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
|
||||
and nums1[1] ^ nums2[1].
|
||||
Thus, one possible nums3 array is [2,5,1,6].
|
||||
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given two positive integers <code>num1</code> and <code>num2</code>, find the integer <code>x</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>x</code> has the same number of set bits as <code>num2</code>, and</li>
|
||||
<li>The value <code>x XOR num1</code> is <strong>minimal</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that <code>XOR</code> is the bitwise XOR operation.</p>
|
||||
|
||||
<p>Return <em>the integer </em><code>x</code>. The test cases are generated such that <code>x</code> is <strong>uniquely determined</strong>.</p>
|
||||
|
||||
<p>The number of <strong>set bits</strong> of an integer is the number of <code>1</code>'s in its binary representation.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 3, num2 = 5
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0011 and 0101, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 3 = 0</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num1 = 1, num2 = 12
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The binary representations of num1 and num2 are 0001 and 1100, respectively.
|
||||
The integer <strong>3</strong> has the same number of set bits as num2, and the value <code>3 XOR 1 = 2</code> is minimal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num1, num2 <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a stream of <code>n</code> videos, each represented by a <strong>distinct</strong> number from <code>1</code> to <code>n</code> that you need to "upload" to a server. You need to implement a data structure that calculates the length of the <strong>longest uploaded prefix</strong> at various points in the upload process.</p>
|
||||
|
||||
<p>We consider <code>i</code> to be an uploaded prefix if all videos in the range <code>1</code> to <code>i</code> (<strong>inclusive</strong>) have been uploaded to the server. The longest uploaded prefix is the <strong>maximum </strong>value of <code>i</code> that satisfies this definition.<br />
|
||||
<br />
|
||||
Implement the <code>LUPrefix </code>class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>LUPrefix(int n)</code> Initializes the object for a stream of <code>n</code> videos.</li>
|
||||
<li><code>void upload(int video)</code> Uploads <code>video</code> to the server.</li>
|
||||
<li><code>int longest()</code> Returns the length of the <strong>longest uploaded prefix</strong> defined above.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["LUPrefix", "upload", "longest", "upload", "longest", "upload", "longest"]
|
||||
[[4], [3], [], [1], [], [2], []]
|
||||
<strong>Output</strong>
|
||||
[null, null, 0, null, 1, null, 3]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
LUPrefix server = new LUPrefix(4); // Initialize a stream of 4 videos.
|
||||
server.upload(3); // Upload video 3.
|
||||
server.longest(); // Since video 1 has not been uploaded yet, there is no prefix.
|
||||
// So, we return 0.
|
||||
server.upload(1); // Upload video 1.
|
||||
server.longest(); // The prefix [1] is the longest uploaded prefix, so we return 1.
|
||||
server.upload(2); // Upload video 2.
|
||||
server.longest(); // The prefix [1,2,3] is the longest uploaded prefix, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= video <= n</code></li>
|
||||
<li>All values of <code>video</code> are <strong>distinct</strong>.</li>
|
||||
<li>At most <code>2 * 10<sup>5</sup></code> calls <strong>in total</strong> will be made to <code>upload</code> and <code>longest</code>.</li>
|
||||
<li>At least one call will be made to <code>longest</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,34 @@
|
||||
<p>You are given an <code>m x n</code> integer matrix <code>grid</code>.</p>
|
||||
|
||||
<p>We define an <strong>hourglass</strong> as a part of the matrix with the following form:</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/img.jpg" style="width: 243px; height: 243px;" />
|
||||
<p>Return <em>the <strong>maximum</strong> sum of the elements of an hourglass</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that an hourglass cannot be rotated and must be entirely contained within the matrix.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/1.jpg" style="width: 323px; height: 323px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/21/2.jpg" style="width: 243px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
<strong>Output:</strong> 35
|
||||
<strong>Explanation:</strong> There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>3 <= m, n <= 150</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, each of size <code>n</code>, and an integer <code>diff</code>. Find the number of <strong>pairs</strong> <code>(i, j)</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= i < j <= n - 1</code> <strong>and</strong></li>
|
||||
<li><code>nums1[i] - nums1[j] <= nums2[i] - nums2[j] + diff</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>number of pairs</strong> that satisfy the conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,2,5], nums2 = [2,2,1], diff = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
There are 3 pairs that satisfy the conditions:
|
||||
1. i = 0, j = 1: 3 - 2 <= 2 - 2 + 1. Since i < j and 1 <= 1, this pair satisfies the conditions.
|
||||
2. i = 0, j = 2: 3 - 5 <= 2 - 1 + 1. Since i < j and -2 <= 2, this pair satisfies the conditions.
|
||||
3. i = 1, j = 2: 2 - 5 <= 2 - 1 + 1. Since i < j and -3 <= 2, this pair satisfies the conditions.
|
||||
Therefore, we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,-1], nums2 = [-2,2], diff = -1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
Since there does not exist any pair that satisfies the conditions, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums1[i], nums2[i] <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>4</sup> <= diff <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user