mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
add problems
This commit is contained in:
parent
c533c20c0a
commit
165d1cd6fe
35
算法题/add-two-numbers.html
Normal file
35
算法题/add-two-numbers.html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>You are given two <strong>non-empty</strong> linked lists representing two non-negative integers. The digits are stored in <strong>reverse order</strong>, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.</p>
|
||||
|
||||
<p>You may assume the two numbers do not contain any leading zero, except the number 0 itself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg" style="width: 483px; height: 342px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4]
|
||||
<strong>Output:</strong> [7,0,8]
|
||||
<strong>Explanation:</strong> 342 + 465 = 807.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [0], l2 = [0]
|
||||
<strong>Output:</strong> [0]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
|
||||
<strong>Output:</strong> [8,9,9,9,0,0,0,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in each linked list is in the range <code>[1, 100]</code>.</li>
|
||||
<li><code>0 <= Node.val <= 9</code></li>
|
||||
<li>It is guaranteed that the list represents a number that does not have leading zeros.</li>
|
||||
</ul>
|
185
算法题/add-two-numbers.json
Normal file
185
算法题/add-two-numbers.json
Normal file
File diff suppressed because one or more lines are too long
25
算法题/longest-palindromic-substring.html
Normal file
25
算法题/longest-palindromic-substring.html
Normal file
@ -0,0 +1,25 @@
|
||||
<p>Given a string <code>s</code>, return <em>the longest palindromic substring</em> in <code>s</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "babad"
|
||||
<strong>Output:</strong> "bab"
|
||||
<strong>Explanation:</strong> "aba" is also a valid answer.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "cbbd"
|
||||
<strong>Output:</strong> "bb"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consist of only digits and English letters.</li>
|
||||
</ul>
|
176
算法题/longest-palindromic-substring.json
Normal file
176
算法题/longest-palindromic-substring.json
Normal file
File diff suppressed because one or more lines are too long
35
算法题/longest-substring-without-repeating-characters.html
Normal file
35
算法题/longest-substring-without-repeating-characters.html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>Given a string <code>s</code>, find the length of the <strong>longest substring</strong> without repeating characters.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcabcbb"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The answer is "abc", with the length of 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bbbbb"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The answer is "b", with the length of 1.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "pwwkew"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The answer is "wke", with the length of 3.
|
||||
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>s</code> consists of English letters, digits, symbols and spaces.</li>
|
||||
</ul>
|
178
算法题/longest-substring-without-repeating-characters.json
Normal file
178
算法题/longest-substring-without-repeating-characters.json
Normal file
File diff suppressed because one or more lines are too long
32
算法题/median-of-two-sorted-arrays.html
Normal file
32
算法题/median-of-two-sorted-arrays.html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>Given two sorted arrays <code>nums1</code> and <code>nums2</code> of size <code>m</code> and <code>n</code> respectively, return <strong>the median</strong> of the two sorted arrays.</p>
|
||||
|
||||
<p>The overall run time complexity should be <code>O(log (m+n))</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,3], nums2 = [2]
|
||||
<strong>Output:</strong> 2.00000
|
||||
<strong>Explanation:</strong> merged array = [1,2,3] and median is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [1,2], nums2 = [3,4]
|
||||
<strong>Output:</strong> 2.50000
|
||||
<strong>Explanation:</strong> merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>nums1.length == m</code></li>
|
||||
<li><code>nums2.length == n</code></li>
|
||||
<li><code>0 <= m <= 1000</code></li>
|
||||
<li><code>0 <= n <= 1000</code></li>
|
||||
<li><code>1 <= m + n <= 2000</code></li>
|
||||
<li><code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
171
算法题/median-of-two-sorted-arrays.json
Normal file
171
算法题/median-of-two-sorted-arrays.json
Normal file
File diff suppressed because one or more lines are too long
42
算法题/palindrome-number.html
Normal file
42
算法题/palindrome-number.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>Given an integer <code>x</code>, return <code>true</code> if <code>x</code> is palindrome integer.</p>
|
||||
|
||||
<p>An integer is a <strong>palindrome</strong> when it reads the same backward as forward.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>121</code> is a palindrome while <code>123</code> is not.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 121
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = -121
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 10
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow up:</strong> Could you solve it without converting the integer to a string?
|
168
算法题/palindrome-number.json
Normal file
168
算法题/palindrome-number.json
Normal file
File diff suppressed because one or more lines are too long
44
算法题/regular-expression-matching.html
Normal file
44
算法题/regular-expression-matching.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>Given an input string <code>s</code> and a pattern <code>p</code>, implement regular expression matching with support for <code>'.'</code> and <code>'*'</code> where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'.'</code> Matches any single character.</li>
|
||||
<li><code>'*'</code> Matches zero or more of the preceding element.</li>
|
||||
</ul>
|
||||
|
||||
<p>The matching should cover the <strong>entire</strong> input string (not partial).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa", p = "a"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> "a" does not match the entire string "aa".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aa", p = "a*"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ab", p = ".*"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> ".*" means "zero or more (*) of any character (.)".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 20</code></li>
|
||||
<li><code>1 <= p.length <= 30</code></li>
|
||||
<li><code>s</code> contains only lowercase English letters.</li>
|
||||
<li><code>p</code> contains only lowercase English letters, <code>'.'</code>, and <code>'*'</code>.</li>
|
||||
<li>It is guaranteed for each appearance of the character <code>'*'</code>, there will be a previous valid character to match.</li>
|
||||
</ul>
|
178
算法题/regular-expression-matching.json
Normal file
178
算法题/regular-expression-matching.json
Normal file
File diff suppressed because one or more lines are too long
32
算法题/reverse-integer.html
Normal file
32
算法题/reverse-integer.html
Normal file
@ -0,0 +1,32 @@
|
||||
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p>
|
||||
|
||||
<p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 123
|
||||
<strong>Output:</strong> 321
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = -123
|
||||
<strong>Output:</strong> -321
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> x = 120
|
||||
<strong>Output:</strong> 21
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
166
算法题/reverse-integer.json
Normal file
166
算法题/reverse-integer.json
Normal file
File diff suppressed because one or more lines are too long
76
算法题/string-to-integer-atoi.html
Normal file
76
算法题/string-to-integer-atoi.html
Normal file
@ -0,0 +1,76 @@
|
||||
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer (similar to C/C++'s <code>atoi</code> function).</p>
|
||||
|
||||
<p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p>
|
||||
|
||||
<ol>
|
||||
<li>Read in and ignore any leading whitespace.</li>
|
||||
<li>Check if the next character (if not already at the end of the string) is <code>'-'</code> or <code>'+'</code>. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.</li>
|
||||
<li>Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.</li>
|
||||
<li>Convert these digits into an integer (i.e. <code>"123" -> 123</code>, <code>"0032" -> 32</code>). If no digits were read, then the integer is <code>0</code>. Change the sign as necessary (from step 2).</li>
|
||||
<li>If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>.</li>
|
||||
<li>Return the integer as the final result.</li>
|
||||
</ol>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Only the space character <code>' '</code> is considered a whitespace character.</li>
|
||||
<li><strong>Do not ignore</strong> any characters other than the leading whitespace or the rest of the string after the digits.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "42"
|
||||
<strong>Output:</strong> 42
|
||||
<strong>Explanation:</strong> The underlined characters are what is read in, the caret is the current reader position.
|
||||
Step 1: "42" (no characters read because there is no leading whitespace)
|
||||
^
|
||||
Step 2: "42" (no characters read because there is neither a '-' nor '+')
|
||||
^
|
||||
Step 3: "<u>42</u>" ("42" is read in)
|
||||
^
|
||||
The parsed integer is 42.
|
||||
Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = " -42"
|
||||
<strong>Output:</strong> -42
|
||||
<strong>Explanation:</strong>
|
||||
Step 1: "<u> </u>-42" (leading whitespace is read and ignored)
|
||||
^
|
||||
Step 2: " <u>-</u>42" ('-' is read, so the result should be negative)
|
||||
^
|
||||
Step 3: " -<u>42</u>" ("42" is read in)
|
||||
^
|
||||
The parsed integer is -42.
|
||||
Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "4193 with words"
|
||||
<strong>Output:</strong> 4193
|
||||
<strong>Explanation:</strong>
|
||||
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
|
||||
^
|
||||
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
|
||||
^
|
||||
Step 3: "<u>4193</u> with words" ("4193" is read in; reading stops because the next character is a non-digit)
|
||||
^
|
||||
The parsed integer is 4193.
|
||||
Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 200</code></li>
|
||||
<li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>' '</code>, <code>'+'</code>, <code>'-'</code>, and <code>'.'</code>.</li>
|
||||
</ul>
|
173
算法题/string-to-integer-atoi.json
Normal file
173
算法题/string-to-integer-atoi.json
Normal file
File diff suppressed because one or more lines are too long
41
算法题/two-sum.html
Normal file
41
算法题/two-sum.html
Normal file
@ -0,0 +1,41 @@
|
||||
<p>Given an array of integers <code>nums</code> and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>
|
||||
|
||||
<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>
|
||||
|
||||
<p>You can return the answer in any order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,7,11,15], target = 9
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,4], target = 6
|
||||
<strong>Output:</strong> [1,2]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3], target = 6
|
||||
<strong>Output:</strong> [0,1]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code></li>
|
||||
<li><strong>Only one valid answer exists.</strong></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<strong>Follow-up: </strong>Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
|
176
算法题/two-sum.json
Normal file
176
算法题/two-sum.json
Normal file
File diff suppressed because one or more lines are too long
51
算法题/zigzag-conversion.html
Normal file
51
算法题/zigzag-conversion.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>The string <code>"PAYPALISHIRING"</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p>
|
||||
|
||||
<pre>
|
||||
P A H N
|
||||
A P L S I I G
|
||||
Y I R
|
||||
</pre>
|
||||
|
||||
<p>And then read line by line: <code>"PAHNAPLSIIGYIR"</code></p>
|
||||
|
||||
<p>Write the code that will take a string and make this conversion given a number of rows:</p>
|
||||
|
||||
<pre>
|
||||
string convert(string s, int numRows);
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 3
|
||||
<strong>Output:</strong> "PAHNAPLSIIGYIR"
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "PAYPALISHIRING", numRows = 4
|
||||
<strong>Output:</strong> "PINALSIGYAHRPI"
|
||||
<strong>Explanation:</strong>
|
||||
P I N
|
||||
A L S I G
|
||||
Y A H R
|
||||
P I
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "A", numRows = 1
|
||||
<strong>Output:</strong> "A"
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of English letters (lower-case and upper-case), <code>','</code> and <code>'.'</code>.</li>
|
||||
<li><code>1 <= numRows <= 1000</code></li>
|
||||
</ul>
|
166
算法题/zigzag-conversion.json
Normal file
166
算法题/zigzag-conversion.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user