From 492feb4415f3e69af80703dc3909eaf0d0af678f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: 2021年11月17日 20:37:58 +0200 Subject: [PATCH] Added description 8-10. --- .../s0008_string_to_integer_atoi/readme.md | 64 +++++++++++++++++++ .../s0009_palindrome_number/readme.md | 41 ++++++++++++ .../readme.md | 56 ++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md create mode 100644 src/main/java/g0001_0100/s0009_palindrome_number/readme.md create mode 100644 src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md diff --git a/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md b/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md new file mode 100644 index 000000000..16800af6c --- /dev/null +++ b/src/main/java/g0001_0100/s0008_string_to_integer_atoi/readme.md @@ -0,0 +1,64 @@ +8\. String to Integer (atoi) + +Medium + +Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function). + +The algorithm for `myAtoi(string s)` is as follows: + +1. Read in and ignore any leading whitespace. +2. Check if the next character (if not already at the end of the string) is `'-'` or `'+'`. 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. +3. 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. +4. Convert these digits into an integer (i.e. `"123" -> 123`, `"0032" -> 32`). If no digits were read, then the integer is `0`. Change the sign as necessary (from step 2). +5. If the integer is out of the 32-bit signed integer range `[-231, 231 - 1]`, then clamp the integer so that it remains in the range. Specifically, integers less than `-231` should be clamped to `-231`, and integers greater than `231 - 1` should be clamped to `231 - 1`. +6. Return the integer as the final result. + +**Note:** + +* Only the space character `' '` is considered a whitespace character. +* **Do not ignore** any characters other than the leading whitespace or the rest of the string after the digits. + +**Example 1:** + +**Input:** s = "42" + +**Output:** 42 + +**Explanation:** 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: "42" ("42" is read in) ^ The parsed integer is 42. Since 42 is in the range \[-231, 231 - 1\], the final result is 42. + +**Example 2:** + +**Input:** s = " -42" + +**Output:** -42 + +**Explanation:** Step 1: " \-42" (leading whitespace is read and ignored) ^ Step 2: " \-42" ('-' is read, so the result should be negative) ^ Step 3: " -42" ("42" is read in) ^ The parsed integer is -42. Since -42 is in the range \[-231, 231 - 1\], the final result is -42. + +**Example 3:** + +**Input:** s = "4193 with words" + +**Output:** 4193 + +**Explanation:** 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: "4193 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 \[-231, 231 - 1\], the final result is 4193. + +**Example 4:** + +**Input:** s = "words and 987" + +**Output:** 0 + +**Explanation:** Step 1: "words and 987" (no characters read because there is no leading whitespace) ^ Step 2: "words and 987" (no characters read because there is neither a '-' nor '+') ^ Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w') ^ The parsed integer is 0 because no digits were read. Since 0 is in the range \[-231, 231 - 1\], the final result is 0. + +**Example 5:** + +**Input:** s = "-91283472332" + +**Output:** -2147483648 + +**Explanation:** Step 1: "-91283472332" (no characters read because there is no leading whitespace) ^ Step 2: "\-91283472332" ('-' is read, so the result should be negative) ^ Step 3: "-91283472332" ("91283472332" is read in) ^ The parsed integer is -91283472332. Since -91283472332 is less than the lower bound of the range \[-231, 231 - 1\], the final result is clamped to -231 = -ひく2147483648. + +**Constraints:** + +* `0 <= s.length <= 200` +* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`. \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0009_palindrome_number/readme.md b/src/main/java/g0001_0100/s0009_palindrome_number/readme.md new file mode 100644 index 000000000..ee1673b7e --- /dev/null +++ b/src/main/java/g0001_0100/s0009_palindrome_number/readme.md @@ -0,0 +1,41 @@ +9\. Palindrome Number + +Easy + +Given an integer `x`, return `true` if `x` is palindrome integer. + +An integer is a **palindrome** when it reads the same backward as forward. For example, `121` is palindrome while `123` is not. + +**Example 1:** + +**Input:** x = 121 + +**Output:** true + +**Example 2:** + +**Input:** x = -121 + +**Output:** false + +**Explanation:** From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. + +**Example 3:** + +**Input:** x = 10 + +**Output:** false + +**Explanation:** Reads 01 from right to left. Therefore it is not a palindrome. + +**Example 4:** + +**Input:** x = -101 + +**Output:** false + +**Constraints:** + +* `-231 <= x <= 231 - 1` + +**Follow up:** Could you solve it without converting the integer to a string? \ No newline at end of file diff --git a/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md b/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md new file mode 100644 index 000000000..88edfba85 --- /dev/null +++ b/src/main/java/g0001_0100/s0010_regular_expression_matching/readme.md @@ -0,0 +1,56 @@ +10\. Regular Expression Matching + +Hard + +Given an input string `s` and a pattern `p`, implement regular expression matching with support for `'.'` and `'*'` where: + +* `'.'` Matches any single character. +* `'*'` Matches zero or more of the preceding element. + +The matching should cover the **entire** input string (not partial). + +**Example 1:** + +**Input:** s = "aa", p = "a" + +**Output:** false + +**Explanation:** "a" does not match the entire string "aa". + +**Example 2:** + +**Input:** s = "aa", p = "a\*" + +**Output:** true + +**Explanation:** '\*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa". + +**Example 3:** + +**Input:** s = "ab", p = ".\*" + +**Output:** true + +**Explanation:** ".\*" means "zero or more (\*) of any character (.)". + +**Example 4:** + +**Input:** s = "aab", p = "c\*a\*b" + +**Output:** true + +**Explanation:** c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab". + +**Example 5:** + +**Input:** s = "mississippi", p = "mis\*is\*p\*." + +**Output:** false + +**Constraints:** + +* `1 <= s.length <= 20` +* `1 <= p.length <= 30` +* `s` contains only lowercase English letters. +* `p` contains only lowercase English letters, `'.'`, and `'*'`. +* It is guaranteed for each appearance of the character `'*'`, there will be a previous valid character to match. \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /