diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md index cbd14a9993e21..6fe968a1ad553 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README.md @@ -79,7 +79,13 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0 -### 方法一 +### 方法一:字符串转数字 + +我们定义一个函数 $\textit{f}(s)$,用来计算字符串 $s$ 的数值。对于字符串 $s$ 中的每个字符 $c$,我们将其转换为对应的数字 $x$,然后将 $x$ 依次连接起来,最后转换为整数。 + +最后,我们只需要判断 $\textit{f}(\textit{firstWord}) + \textit{f}(\textit{secondWord})$ 是否等于 $\textit{f}(\textit{targetWord})$ 即可。 + +时间复杂度 $O(L)$,其中 $L$ 为题目中所有字符串的长度之和。空间复杂度 $O(1)$。 @@ -88,11 +94,12 @@ targetWord 的数值为 "aaaa" -> "0000" -> 0 ```python class Solution: def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: - def f(s): - res = 0 - for c in s: - res = res * 10 + (ord(c) - ord('a')) - return res + def f(s: str) -> int: + ans, a = 0, ord("a") + for c in map(ord, s): + x = c - a + ans = ans * 10 + x + return ans return f(firstWord) + f(secondWord) == f(targetWord) ``` @@ -106,11 +113,11 @@ class Solution { } private int f(String s) { - int res = 0; + int ans = 0; for (char c : s.toCharArray()) { - res = res * 10 + (c - 'a'); + ans = ans * 10 + (c - 'a'); } - return res; + return ans; } } ``` @@ -121,14 +128,15 @@ class Solution { class Solution { public: bool isSumEqual(string firstWord, string secondWord, string targetWord) { + auto f = [](string& s) -> int { + int ans = 0; + for (char c : s) { + ans = ans * 10 + (c - 'a'); + } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); } - - int f(string s) { - int res = 0; - for (char c : s) res = res * 10 + (c - 'a'); - return res; - } }; ``` @@ -136,12 +144,11 @@ public: ```go func isSumEqual(firstWord string, secondWord string, targetWord string) bool { - f := func(s string) int { - res := 0 + f := func(s string) (ans int) { for _, c := range s { - res = res*10 + int(c-'a') + ans = ans*10 + int(c-'a') } - return res + return } return f(firstWord)+f(secondWord) == f(targetWord) } @@ -151,14 +158,14 @@ func isSumEqual(firstWord string, secondWord string, targetWord string) bool { ```ts function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean { - const calc = (s: string) => { - let res = 0; + const f = (s: string): number => { + let ans = 0; for (const c of s) { - res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0); + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; + return ans; }; - return calc(firstWord) + calc(secondWord) === calc(targetWord); + return f(firstWord) + f(secondWord) == f(targetWord); } ``` @@ -166,16 +173,17 @@ function isSumEqual(firstWord: string, secondWord: string, targetWord: string): ```rust impl Solution { - fn calc(s: &String) -> i32 { - let mut res = 0; - for c in s.as_bytes() { - res = res * 10 + ((c - b'a') as i32); - } - res - } - pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool { - Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word) + fn f(s: &str) -> i64 { + let mut ans = 0; + let a = 'a' as i64; + for c in s.chars() { + let x = c as i64 - a; + ans = ans * 10 + x; + } + ans + } + f(&first_word) + f(&second_word) == f(&target_word) } } ``` @@ -190,13 +198,13 @@ impl Solution { * @return {boolean} */ var isSumEqual = function (firstWord, secondWord, targetWord) { - function f(s) { - let res = 0; - for (let c of s) { - res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt()); + const f = s => { + let ans = 0; + for (const c of s) { + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; - } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); }; ``` @@ -204,16 +212,17 @@ var isSumEqual = function (firstWord, secondWord, targetWord) { #### C ```c -int calc(char* s) { - int res = 0; - for (int i = 0; s[i]; i++) { - res = res * 10 + s[i] - 'a'; +int f(const char* s) { + int ans = 0; + while (*s) { + ans = ans * 10 + (*s - 'a'); + s++; } - return res; + return ans; } bool isSumEqual(char* firstWord, char* secondWord, char* targetWord) { - return calc(firstWord) + calc(secondWord) == calc(targetWord); + return f(firstWord) + f(secondWord) == f(targetWord); } ``` diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md index 05b49365f3037..ddff890e66c5d 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/README_EN.md @@ -48,7 +48,7 @@ We return true because 21 + 210 == 231.
Input: firstWord = "aaa", secondWord = "a", targetWord = "aab" Output: false -Explanation: +Explanation: The numerical value of firstWord is "aaa" -> "000" -> 0. The numerical value of secondWord is "a" -> "0" -> 0. The numerical value of targetWord is "aab" -> "001" -> 1. @@ -60,7 +60,7 @@ We return false because 0 + 0 != 1.Input: firstWord = "aaa", secondWord = "a", targetWord = "aaaa" Output: true -Explanation: +Explanation: The numerical value of firstWord is "aaa" -> "000" -> 0. The numerical value of secondWord is "a" -> "0" -> 0. The numerical value of targetWord is "aaaa" -> "0000" -> 0. @@ -81,7 +81,13 @@ We return true because 0 + 0 == 0. -### Solution 1 +### Solution 1: String to Number + +We define a function $\textit{f}(s)$ to calculate the numerical value of the string $s$. For each character $c$ in the string $s$, we convert it to the corresponding number $x$, then concatenate $x$ sequentially, and finally convert it to an integer. + +Finally, we just need to check whether $\textit{f}(\textit{firstWord}) + \textit{f}(\textit{secondWord})$ equals $\textit{f}(\textit{targetWord})$. + +The time complexity is $O(L)$, where $L$ is the sum of the lengths of all strings in the problem. The space complexity is $O(1)$. @@ -90,11 +96,12 @@ We return true because 0 + 0 == 0. ```python class Solution: def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: - def f(s): - res = 0 - for c in s: - res = res * 10 + (ord(c) - ord('a')) - return res + def f(s: str) -> int: + ans, a = 0, ord("a") + for c in map(ord, s): + x = c - a + ans = ans * 10 + x + return ans return f(firstWord) + f(secondWord) == f(targetWord) ``` @@ -108,11 +115,11 @@ class Solution { } private int f(String s) { - int res = 0; + int ans = 0; for (char c : s.toCharArray()) { - res = res * 10 + (c - 'a'); + ans = ans * 10 + (c - 'a'); } - return res; + return ans; } } ``` @@ -123,14 +130,15 @@ class Solution { class Solution { public: bool isSumEqual(string firstWord, string secondWord, string targetWord) { + auto f = [](string& s) -> int { + int ans = 0; + for (char c : s) { + ans = ans * 10 + (c - 'a'); + } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); } - - int f(string s) { - int res = 0; - for (char c : s) res = res * 10 + (c - 'a'); - return res; - } }; ``` @@ -138,12 +146,11 @@ public: ```go func isSumEqual(firstWord string, secondWord string, targetWord string) bool { - f := func(s string) int { - res := 0 + f := func(s string) (ans int) { for _, c := range s { - res = res*10 + int(c-'a') + ans = ans*10 + int(c-'a') } - return res + return } return f(firstWord)+f(secondWord) == f(targetWord) } @@ -153,14 +160,14 @@ func isSumEqual(firstWord string, secondWord string, targetWord string) bool { ```ts function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean { - const calc = (s: string) => { - let res = 0; + const f = (s: string): number => { + let ans = 0; for (const c of s) { - res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0); + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; + return ans; }; - return calc(firstWord) + calc(secondWord) === calc(targetWord); + return f(firstWord) + f(secondWord) == f(targetWord); } ``` @@ -168,16 +175,17 @@ function isSumEqual(firstWord: string, secondWord: string, targetWord: string): ```rust impl Solution { - fn calc(s: &String) -> i32 { - let mut res = 0; - for c in s.as_bytes() { - res = res * 10 + ((c - b'a') as i32); - } - res - } - pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool { - Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word) + fn f(s: &str) -> i64 { + let mut ans = 0; + let a = 'a' as i64; + for c in s.chars() { + let x = c as i64 - a; + ans = ans * 10 + x; + } + ans + } + f(&first_word) + f(&second_word) == f(&target_word) } } ``` @@ -192,13 +200,13 @@ impl Solution { * @return {boolean} */ var isSumEqual = function (firstWord, secondWord, targetWord) { - function f(s) { - let res = 0; - for (let c of s) { - res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt()); + const f = s => { + let ans = 0; + for (const c of s) { + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; - } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); }; ``` @@ -206,16 +214,17 @@ var isSumEqual = function (firstWord, secondWord, targetWord) { #### C ```c -int calc(char* s) { - int res = 0; - for (int i = 0; s[i]; i++) { - res = res * 10 + s[i] - 'a'; +int f(const char* s) { + int ans = 0; + while (*s) { + ans = ans * 10 + (*s - 'a'); + s++; } - return res; + return ans; } bool isSumEqual(char* firstWord, char* secondWord, char* targetWord) { - return calc(firstWord) + calc(secondWord) == calc(targetWord); + return f(firstWord) + f(secondWord) == f(targetWord); } ``` diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.c b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.c index a1bb00e6737c4..911d6a023822b 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.c +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.c @@ -1,11 +1,12 @@ -int calc(char* s) { - int res = 0; - for (int i = 0; s[i]; i++) { - res = res * 10 + s[i] - 'a'; +int f(const char* s) { + int ans = 0; + while (*s) { + ans = ans * 10 + (*s - 'a'); + s++; } - return res; + return ans; } bool isSumEqual(char* firstWord, char* secondWord, char* targetWord) { - return calc(firstWord) + calc(secondWord) == calc(targetWord); -} \ No newline at end of file + return f(firstWord) + f(secondWord) == f(targetWord); +} diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.cpp b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.cpp index c99bfea49218e..78fa3fee75a01 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.cpp +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.cpp @@ -1,12 +1,13 @@ class Solution { public: bool isSumEqual(string firstWord, string secondWord, string targetWord) { + auto f = [](string& s) -> int { + int ans = 0; + for (char c : s) { + ans = ans * 10 + (c - 'a'); + } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); } - - int f(string s) { - int res = 0; - for (char c : s) res = res * 10 + (c - 'a'); - return res; - } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.go b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.go index 078fd0acdd22a..e6acff3256983 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.go +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.go @@ -1,10 +1,9 @@ func isSumEqual(firstWord string, secondWord string, targetWord string) bool { - f := func(s string) int { - res := 0 + f := func(s string) (ans int) { for _, c := range s { - res = res*10 + int(c-'a') + ans = ans*10 + int(c-'a') } - return res + return } return f(firstWord)+f(secondWord) == f(targetWord) -} \ No newline at end of file +} diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java index 515830162f97e..13706270f6ee2 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.java @@ -4,10 +4,10 @@ public boolean isSumEqual(String firstWord, String secondWord, String targetWord } private int f(String s) { - int res = 0; + int ans = 0; for (char c : s.toCharArray()) { - res = res * 10 + (c - 'a'); + ans = ans * 10 + (c - 'a'); } - return res; + return ans; } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.js b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.js index 4c318298027d7..71396a6a9a703 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.js +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.js @@ -5,12 +5,12 @@ * @return {boolean} */ var isSumEqual = function (firstWord, secondWord, targetWord) { - function f(s) { - let res = 0; - for (let c of s) { - res = res * 10 + (c.charCodeAt() - 'a'.charCodeAt()); + const f = s => { + let ans = 0; + for (const c of s) { + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; - } + return ans; + }; return f(firstWord) + f(secondWord) == f(targetWord); }; diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.py b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.py index bf6ffbe4a0f02..beb712b5986e6 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.py +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.py @@ -1,9 +1,10 @@ class Solution: def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool: - def f(s): - res = 0 - for c in s: - res = res * 10 + (ord(c) - ord('a')) - return res + def f(s: str) -> int: + ans, a = 0, ord("a") + for c in map(ord, s): + x = c - a + ans = ans * 10 + x + return ans return f(firstWord) + f(secondWord) == f(targetWord) diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs index d7efedab6c5c1..6c992c69f2798 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.rs @@ -1,13 +1,14 @@ impl Solution { - fn calc(s: &String) -> i32 { - let mut res = 0; - for c in s.as_bytes() { - res = res * 10 + ((c - b'a') as i32); - } - res - } - pub fn is_sum_equal(first_word: String, second_word: String, target_word: String) -> bool { - Self::calc(&first_word) + Self::calc(&second_word) == Self::calc(&target_word) + fn f(s: &str) -> i64 { + let mut ans = 0; + let a = 'a' as i64; + for c in s.chars() { + let x = c as i64 - a; + ans = ans * 10 + x; + } + ans + } + f(&first_word) + f(&second_word) == f(&target_word) } } diff --git a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.ts b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.ts index fe0cd1d3eab6b..0ff6002596ae1 100644 --- a/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.ts +++ b/solution/1800-1899/1880.Check if Word Equals Summation of Two Words/Solution.ts @@ -1,10 +1,10 @@ function isSumEqual(firstWord: string, secondWord: string, targetWord: string): boolean { - const calc = (s: string) => { - let res = 0; + const f = (s: string): number => { + let ans = 0; for (const c of s) { - res = res * 10 + c.charCodeAt(0) - 'a'.charCodeAt(0); + ans = ans * 10 + c.charCodeAt(0) - 97; } - return res; + return ans; }; - return calc(firstWord) + calc(secondWord) === calc(targetWord); + return f(firstWord) + f(secondWord) == f(targetWord); } diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/README.md b/solution/1800-1899/1881.Maximum Value after Insertion/README.md index b173c8378f52e..70ad1beeb9aea 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/README.md +++ b/solution/1800-1899/1881.Maximum Value after Insertion/README.md @@ -66,7 +66,11 @@ tags: -### 方法一 +### 方法一:贪心 + +如果 $n$ 是负数,那么我们要找到第一个大于 $x$ 的位置,然后在这个位置插入 $x$;如果 $n$ 是正数,那么我们要找到第一个小于 $x$ 的位置,然后在这个位置插入 $x$。 + +时间复杂度 $O(m)$,其中 $m$ 为 $n$ 的长度。空间复杂度 $O(1)$。 @@ -75,16 +79,15 @@ tags: ```python class Solution: def maxValue(self, n: str, x: int) -> str: - if n[0] != '-': - for i, c in enumerate(n): - if int(c) < x: - return n[:i] + str(x) + n[i:] - return n + str(x) + i = 0 + if n[0] == "-": + i += 1 + while i < len(n) and int(n[i]) <= x: + i += 1 else: - for i, c in enumerate(n[1:]): - if int(c) > x: - return n[: i + 1] + str(x) + n[i + 1 :] - return n + str(x) + while i < len(n) and int(n[i]) >= x: + i += 1 + return n[:i] + str(x) + n[i:] ``` #### Java @@ -93,12 +96,15 @@ class Solution: class Solution { public String maxValue(String n, int x) { int i = 0; - if (n.charAt(0) != '-') { - for (; i < n.length() && n.charAt(i) - '0' >= x; ++i) - ; + if (n.charAt(0) == '-') { + ++i; + while (i < n.length() && n.charAt(i) - '0' <= x) { + ++i; + } } else { - for (i = 1; i < n.length() && n.charAt(i) - '0' <= x; ++i) - ; + while (i < n.length() && n.charAt(i) - '0' >= x) { + ++i; + } } return n.substring(0, i) + x + n.substring(i); } @@ -112,13 +118,18 @@ class Solution { public: string maxValue(string n, int x) { int i = 0; - if (n[0] != '-') - for (; i < n.size() && n[i] - '0' >= x; ++i) - ; - else - for (i = 1; i < n.size() && n[i] - '0' <= x; ++i) - ; - return n.substr(0, i) + to_string(x) + n.substr(i); + if (n[0] == '-') { + ++i; + while (i < n.size() && n[i] - '0' <= x) { + ++i; + } + } else { + while (i < n.size() && n[i] - '0' >= x) { + ++i; + } + } + n.insert(i, 1, x + '0'); + return n; } }; ``` @@ -129,17 +140,65 @@ public: func maxValue(n string, x int) string { i := 0 y := byte('0' + x) - if n[0] != '-' { - for ; i < len(n) && n[i] >= y; i++ { + if n[0] == '-' { + i++ + for i < len(n) && n[i] <= y { + i++ } } else { - for i = 1; i < len(n) && n[i] <= y; i++ { + for i < len(n) && n[i] >= y { + i++ } } return n[:i] + string(y) + n[i:] } ``` +#### TypeScript + +```ts +function maxValue(n: string, x: number): string { + let i = 0; + if (n[0] === '-') { + i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } + } + return n.slice(0, i) + x + n.slice(i); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_value(n: String, x: i32) -> String { + let s = n.as_bytes(); + let mut i = 0; + if n.starts_with('-') { + i += 1; + while i < s.len() && (s[i] - b'0') as i32 <= x { + i += 1; + } + } else { + while i < s.len() && (s[i] - b'0') as i32 >= x { + i += 1; + } + } + let mut ans = String::new(); + ans.push_str(&n[0..i]); + ans.push_str(&x.to_string()); + ans.push_str(&n[i..]); + ans + } +} +``` + #### JavaScript ```js @@ -149,18 +208,18 @@ func maxValue(n string, x int) string { * @return {string} */ var maxValue = function (n, x) { - let nums = [...n]; - let sign = 1, - i = 0; - if (nums[0] == '-') { - sign = -1; - i++; - } - while (i < n.length && (nums[i] - x) * sign >= 0) { + let i = 0; + if (n[0] === '-') { i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } } - nums.splice(i, 0, x); - return nums.join(''); + return n.slice(0, i) + x + n.slice(i); }; ``` diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/README_EN.md b/solution/1800-1899/1881.Maximum Value after Insertion/README_EN.md index 40d9547510c30..aecc263a34960 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/README_EN.md +++ b/solution/1800-1899/1881.Maximum Value after Insertion/README_EN.md @@ -64,7 +64,11 @@ tags: -### Solution 1 +### Solution 1: Greedy + +If $n$ is negative, we need to find the first position greater than $x$ and insert $x$ at that position. If $n$ is positive, we need to find the first position less than $x$ and insert $x$ at that position. + +The time complexity is $O(m)$, where $m$ is the length of $n$. The space complexity is $O(1)$. @@ -73,16 +77,15 @@ tags: ```python class Solution: def maxValue(self, n: str, x: int) -> str: - if n[0] != '-': - for i, c in enumerate(n): - if int(c) < x: - return n[:i] + str(x) + n[i:] - return n + str(x) + i = 0 + if n[0] == "-": + i += 1 + while i < len(n) and int(n[i]) <= x: + i += 1 else: - for i, c in enumerate(n[1:]): - if int(c) > x: - return n[: i + 1] + str(x) + n[i + 1 :] - return n + str(x) + while i < len(n) and int(n[i]) >= x: + i += 1 + return n[:i] + str(x) + n[i:] ``` #### Java @@ -91,12 +94,15 @@ class Solution: class Solution { public String maxValue(String n, int x) { int i = 0; - if (n.charAt(0) != '-') { - for (; i < n.length() && n.charAt(i) - '0' >= x; ++i) - ; + if (n.charAt(0) == '-') { + ++i; + while (i < n.length() && n.charAt(i) - '0' <= x) { + ++i; + } } else { - for (i = 1; i < n.length() && n.charAt(i) - '0' <= x; ++i) - ; + while (i < n.length() && n.charAt(i) - '0' >= x) { + ++i; + } } return n.substring(0, i) + x + n.substring(i); } @@ -110,13 +116,18 @@ class Solution { public: string maxValue(string n, int x) { int i = 0; - if (n[0] != '-') - for (; i < n.size() && n[i] - '0' >= x; ++i) - ; - else - for (i = 1; i < n.size() && n[i] - '0' <= x; ++i) - ; - return n.substr(0, i) + to_string(x) + n.substr(i); + if (n[0] == '-') { + ++i; + while (i < n.size() && n[i] - '0' <= x) { + ++i; + } + } else { + while (i < n.size() && n[i] - '0' >= x) { + ++i; + } + } + n.insert(i, 1, x + '0'); + return n; } }; ``` @@ -127,17 +138,65 @@ public: func maxValue(n string, x int) string { i := 0 y := byte('0' + x) - if n[0] != '-' { - for ; i < len(n) && n[i] >= y; i++ { + if n[0] == '-' { + i++ + for i < len(n) && n[i] <= y { + i++ } } else { - for i = 1; i < len(n) && n[i] <= y; i++ { + for i < len(n) && n[i] >= y { + i++ } } return n[:i] + string(y) + n[i:] } ``` +#### TypeScript + +```ts +function maxValue(n: string, x: number): string { + let i = 0; + if (n[0] === '-') { + i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } + } + return n.slice(0, i) + x + n.slice(i); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn max_value(n: String, x: i32) -> String { + let s = n.as_bytes(); + let mut i = 0; + if n.starts_with('-') { + i += 1; + while i < s.len() && (s[i] - b'0') as i32 <= x { + i += 1; + } + } else { + while i < s.len() && (s[i] - b'0') as i32 >= x { + i += 1; + } + } + let mut ans = String::new(); + ans.push_str(&n[0..i]); + ans.push_str(&x.to_string()); + ans.push_str(&n[i..]); + ans + } +} +``` + #### JavaScript ```js @@ -147,18 +206,18 @@ func maxValue(n string, x int) string { * @return {string} */ var maxValue = function (n, x) { - let nums = [...n]; - let sign = 1, - i = 0; - if (nums[0] == '-') { - sign = -1; - i++; - } - while (i < n.length && (nums[i] - x) * sign >= 0) { + let i = 0; + if (n[0] === '-') { i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } } - nums.splice(i, 0, x); - return nums.join(''); + return n.slice(0, i) + x + n.slice(i); }; ``` diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp index 741a185b3a2fe..288ab7d427dfb 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp @@ -2,12 +2,17 @@ class Solution { public: string maxValue(string n, int x) { int i = 0; - if (n[0] != '-') - for (; i < n.size() && n[i] - '0' >= x; ++i) - ; - else - for (i = 1; i < n.size() && n[i] - '0' <= x; ++i) - ; - return n.substr(0, i) + to_string(x) + n.substr(i); + if (n[0] == '-') { + ++i; + while (i < n.size() && n[i] - '0' <= x) { + ++i; + } + } else { + while (i < n.size() && n[i] - '0' >= x) { + ++i; + } + } + n.insert(i, 1, x + '0'); + return n; } -}; \ No newline at end of file +}; diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.go b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.go index 9495224afd454..c4f2dbabab0e7 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.go +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.go @@ -1,12 +1,15 @@ func maxValue(n string, x int) string { i := 0 y := byte('0' + x) - if n[0] != '-' { - for ; i < len(n) && n[i] >= y; i++ { + if n[0] == '-' { + i++ + for i < len(n) && n[i] <= y { + i++ } } else { - for i = 1; i < len(n) && n[i] <= y; i++ { + for i < len(n) && n[i] >= y { + i++ } } return n[:i] + string(y) + n[i:] -} \ No newline at end of file +} diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.java b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.java index 561382a45fcd9..c12035d645ede 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.java +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.java @@ -1,13 +1,16 @@ class Solution { public String maxValue(String n, int x) { int i = 0; - if (n.charAt(0) != '-') { - for (; i < n.length() && n.charAt(i) - '0' >= x; ++i) - ; + if (n.charAt(0) == '-') { + ++i; + while (i < n.length() && n.charAt(i) - '0' <= x) { + ++i; + } } else { - for (i = 1; i < n.length() && n.charAt(i) - '0' <= x; ++i) - ; + while (i < n.length() && n.charAt(i) - '0' >= x) { + ++i; + } } return n.substring(0, i) + x + n.substring(i); } -} \ No newline at end of file +} diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.js b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.js index 1230394b45ac9..7d7654ceb0533 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.js +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.js @@ -4,16 +4,16 @@ * @return {string} */ var maxValue = function (n, x) { - let nums = [...n]; - let sign = 1, - i = 0; - if (nums[0] == '-') { - sign = -1; + let i = 0; + if (n[0] === '-') { i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } } - while (i < n.length && (nums[i] - x) * sign >= 0) { - i++; - } - nums.splice(i, 0, x); - return nums.join(''); + return n.slice(0, i) + x + n.slice(i); }; diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.py b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.py index e4be323d18bcf..422d93774e73b 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.py +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.py @@ -1,12 +1,11 @@ class Solution: def maxValue(self, n: str, x: int) -> str: - if n[0] != '-': - for i, c in enumerate(n): - if int(c) < x: - return n[:i] + str(x) + n[i:] - return n + str(x) + i = 0 + if n[0] == "-": + i += 1 + while i < len(n) and int(n[i]) <= x: + i += 1 else: - for i, c in enumerate(n[1:]): - if int(c) > x: - return n[: i + 1] + str(x) + n[i + 1 :] - return n + str(x) + while i < len(n) and int(n[i]) >= x: + i += 1 + return n[:i] + str(x) + n[i:] diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.rs b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.rs new file mode 100644 index 0000000000000..f4e69ccb10110 --- /dev/null +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.rs @@ -0,0 +1,21 @@ +impl Solution { + pub fn max_value(n: String, x: i32) -> String { + let s = n.as_bytes(); + let mut i = 0; + if n.starts_with('-') { + i += 1; + while i < s.len() && (s[i] - b'0') as i32 <= x { + i += 1; + } + } else { + while i < s.len() && (s[i] - b'0') as i32 >= x { + i += 1; + } + } + let mut ans = String::new(); + ans.push_str(&n[0..i]); + ans.push_str(&x.to_string()); + ans.push_str(&n[i..]); + ans + } +} diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.ts b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.ts new file mode 100644 index 0000000000000..85ec7156a4c8d --- /dev/null +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.ts @@ -0,0 +1,14 @@ +function maxValue(n: string, x: number): string { + let i = 0; + if (n[0] === '-') { + i++; + while (i < n.length && +n[i] <= x) { + i++; + } + } else { + while (i < n.length && +n[i] >= x) { + i++; + } + } + return n.slice(0, i) + x + n.slice(i); +} diff --git a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md index 47a865c2a7a1e..5bf71a6bf5fed 100644 --- a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md +++ b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README.md @@ -123,26 +123,6 @@ class Solution: return -1 ``` -#### Python3 - -```python -class Solution: - def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int: - n = len(dist) - f = [[inf] * (n + 1) for _ in range(n + 1)] - f[0][0] = 0 - for i, x in enumerate(dist, 1): - for j in range(i + 1): - if j < i: - f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed) - if j: - f[i][j] = min(f[i][j], f[i - 1][j - 1] + x) - for j in range(n + 1): - if f[n][j] <= hoursBefore * speed: - return j - return -1 -``` - #### Java ```java diff --git a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md index 0599c4e55362d..fe31fcf498132 100644 --- a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md +++ b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/README_EN.md @@ -121,26 +121,6 @@ class Solution: return -1 ``` -#### Python3 - -```python -class Solution: - def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int: - n = len(dist) - f = [[inf] * (n + 1) for _ in range(n + 1)] - f[0][0] = 0 - for i, x in enumerate(dist, 1): - for j in range(i + 1): - if j < i: - f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed) - if j: - f[i][j] = min(f[i][j], f[i - 1][j - 1] + x) - for j in range(n + 1): - if f[n][j] <= hoursBefore * speed: - return j - return -1 -``` - #### Java ```java diff --git a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/Solution2.py b/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/Solution2.py deleted file mode 100644 index 06610ad5f6901..0000000000000 --- a/solution/1800-1899/1883.Minimum Skips to Arrive at Meeting On Time/Solution2.py +++ /dev/null @@ -1,15 +0,0 @@ -class Solution: - def minSkips(self, dist: List[int], speed: int, hoursBefore: int) -> int: - n = len(dist) - f = [[inf] * (n + 1) for _ in range(n + 1)] - f[0][0] = 0 - for i, x in enumerate(dist, 1): - for j in range(i + 1): - if j < i: - f[i][j] = min(f[i][j], ((f[i - 1][j] + x - 1) // speed + 1) * speed) - if j: - f[i][j] = min(f[i][j], f[i - 1][j - 1] + x) - for j in range(n + 1): - if f[n][j] <= hoursBefore * speed: - return j - return -1