Skip to content

Commit 129b1fe

Browse files
committed
feat: add solutions to lc problems: No.1903,1904,1905,1906
1 parent 4a29be2 commit 129b1fe

31 files changed

+1724
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [1903. 字符串中的最大奇数](https://leetcode-cn.com/problems/largest-odd-number-in-string)
2+
3+
[English Version](/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个字符串 <code>num</code> ,表示一个大整数。请你在字符串 <code>num</code> 的所有 <strong>非空子字符串</strong> 中找出 <strong>值最大的奇数</strong> ,并以字符串形式返回。如果不存在奇数,则返回一个空字符串<em> </em><code>""</code><em> </em>。</p>
10+
11+
<p><strong>子字符串</strong> 是字符串中的一个连续的字符序列。</p>
12+
13+
<p> </p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre>
18+
<strong>输入:</strong>num = "52"
19+
<strong>输出:</strong>"5"
20+
<strong>解释:</strong>非空子字符串仅有 "5"、"2" 和 "52" 。"5" 是其中唯一的奇数。
21+
</pre>
22+
23+
<p><strong>示例 2:</strong></p>
24+
25+
<pre>
26+
<strong>输入:</strong>num = "4206"
27+
<strong>输出:</strong>""
28+
<strong>解释:</strong>在 "4206" 中不存在奇数。
29+
</pre>
30+
31+
<p><strong>示例 3:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>num = "35427"
35+
<strong>输出:</strong>"35427"
36+
<strong>解释:</strong>"35427" 本身就是一个奇数。
37+
</pre>
38+
39+
<p> </p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 <= num.length <= 10<sup>5</sup></code></li>
45+
<li><code>num</code> 仅由数字组成且不含前导零</li>
46+
</ul>
47+
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
从后往前遍历字符串中的每个数字,遇到奇数则直接返回结果。若遍历结束仍未遇到奇数,返回空字符串。
54+
55+
<!-- tabs:start -->
56+
57+
### **Python3**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```python
62+
class Solution:
63+
def largestOddNumber(self, num: str) -> str:
64+
for i in range(len(num) - 1, -1, -1):
65+
if (int(num[i]) & 1) == 1:
66+
return num[:i + 1]
67+
return ''
68+
```
69+
70+
### **Java**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```java
75+
class Solution {
76+
public String largestOddNumber(String num) {
77+
for (int i = num.length() - 1; i >= 0; --i) {
78+
int c = num.charAt(i) - '0';
79+
if ((c & 1) == 1) {
80+
return num.substring(0, i + 1);
81+
}
82+
}
83+
return "";
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
string largestOddNumber(string num) {
94+
for (int i = num.size() - 1; i >= 0; --i) {
95+
int c = num[i] - '0';
96+
if ((c & 1) == 1) {
97+
return num.substr(0, i + 1);
98+
}
99+
}
100+
return "";
101+
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func largestOddNumber(num string) string {
109+
for i := len(num) - 1; i >= 0; i-- {
110+
c := num[i] - '0'
111+
if (c & 1) == 1 {
112+
return num[:i+1]
113+
}
114+
}
115+
return ""
116+
}
117+
```
118+
119+
### **...**
120+
121+
```
122+
123+
```
124+
125+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [1903. Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string)
2+
3+
[中文文档](/solution/1900-1999/1903.Largest%20Odd%20Number%20in%20String/README.md)
4+
5+
## Description
6+
7+
<p>You are given a string <code>num</code>, representing a large integer. Return <em>the <strong>largest-valued odd</strong> integer (as a string) that is a <strong>non-empty substring</strong> of </em><code>num</code><em>, or an empty string </em><code>&quot;&quot;</code><em> if no odd integer exists</em>.</p>
8+
9+
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> num = &quot;52&quot;
16+
<strong>Output:</strong> &quot;5&quot;
17+
<strong>Explanation:</strong> The only non-empty substrings are &quot;5&quot;, &quot;2&quot;, and &quot;52&quot;. &quot;5&quot; is the only odd number.
18+
</pre>
19+
20+
<p><strong>Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> num = &quot;4206&quot;
24+
<strong>Output:</strong> &quot;&quot;
25+
<strong>Explanation:</strong> There are no odd numbers in &quot;4206&quot;.
26+
</pre>
27+
28+
<p><strong>Example 3:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> num = &quot;35427&quot;
32+
<strong>Output:</strong> &quot;35427&quot;
33+
<strong>Explanation:</strong> &quot;35427&quot; is already an odd number.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= num.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>num</code> only consists of digits and does not contain any leading zeros.</li>
42+
</ul>
43+
44+
45+
## Solutions
46+
47+
<!-- tabs:start -->
48+
49+
### **Python3**
50+
51+
```python
52+
class Solution:
53+
def largestOddNumber(self, num: str) -> str:
54+
for i in range(len(num) - 1, -1, -1):
55+
if (int(num[i]) & 1) == 1:
56+
return num[:i + 1]
57+
return ''
58+
```
59+
60+
### **Java**
61+
62+
```java
63+
class Solution {
64+
public String largestOddNumber(String num) {
65+
for (int i = num.length() - 1; i >= 0; --i) {
66+
int c = num.charAt(i) - '0';
67+
if ((c & 1) == 1) {
68+
return num.substring(0, i + 1);
69+
}
70+
}
71+
return "";
72+
}
73+
}
74+
```
75+
76+
### **C++**
77+
78+
```cpp
79+
class Solution {
80+
public:
81+
string largestOddNumber(string num) {
82+
for (int i = num.size() - 1; i >= 0; --i) {
83+
int c = num[i] - '0';
84+
if ((c & 1) == 1) {
85+
return num.substr(0, i + 1);
86+
}
87+
}
88+
return "";
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func largestOddNumber(num string) string {
97+
for i := len(num) - 1; i >= 0; i-- {
98+
c := num[i] - '0'
99+
if (c & 1) == 1 {
100+
return num[:i+1]
101+
}
102+
}
103+
return ""
104+
}
105+
```
106+
107+
### **...**
108+
109+
```
110+
111+
```
112+
113+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
string largestOddNumber(string num) {
4+
for (int i = num.size() - 1; i >= 0; --i) {
5+
int c = num[i] - '0';
6+
if ((c & 1) == 1) {
7+
return num.substr(0, i + 1);
8+
}
9+
}
10+
return "";
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func largestOddNumber(num string) string {
2+
for i := len(num) - 1; i >= 0; i-- {
3+
c := num[i] - '0'
4+
if (c & 1) == 1 {
5+
return num[:i+1]
6+
}
7+
}
8+
return ""
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public String largestOddNumber(String num) {
3+
for (int i = num.length() - 1; i >= 0; --i) {
4+
int c = num.charAt(i) - '0';
5+
if ((c & 1) == 1) {
6+
return num.substring(0, i + 1);
7+
}
8+
}
9+
return "";
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def largestOddNumber(self, num: str) -> str:
3+
for i in range(len(num) - 1, -1, -1):
4+
if (int(num[i]) & 1) == 1:
5+
return num[:i + 1]
6+
return ''

0 commit comments

Comments
 (0)