|
| 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>""</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> </p> |
| 12 | +<p><strong>Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> num = "52" |
| 16 | +<strong>Output:</strong> "5" |
| 17 | +<strong>Explanation:</strong> The only non-empty substrings are "5", "2", and "52". "5" is the only odd number. |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong>Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> num = "4206" |
| 24 | +<strong>Output:</strong> "" |
| 25 | +<strong>Explanation:</strong> There are no odd numbers in "4206". |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong>Example 3:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<strong>Input:</strong> num = "35427" |
| 32 | +<strong>Output:</strong> "35427" |
| 33 | +<strong>Explanation:</strong> "35427" is already an odd number. |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>1 <= num.length <= 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 --> |
0 commit comments