Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e0a1c1

Browse files
feat: add solutions to lc problems: No.2864~2866 (doocs#1672)
* No.2864.Maximum Odd Binary Number * No.2865.Beautiful Towers I * No.2866.Beautiful Towers II
1 parent 9183edf commit 9e0a1c1

File tree

31 files changed

+2059
-0
lines changed

31 files changed

+2059
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# [2864. 最大二进制奇数](https://leetcode.cn/problems/maximum-odd-binary-number)
2+
3+
[English Version](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个 <strong>二进制</strong> 字符串 <code>s</code> ,其中至少包含一个 <code>'1'</code> 。</p>
10+
11+
<p>你必须按某种方式 <strong>重新排列</strong> 字符串中的位,使得到的二进制数字是可以由该组合生成的 <strong>最大二进制奇数</strong> 。</p>
12+
13+
<p>以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。</p>
14+
15+
<p><strong>注意 </strong>返回的结果字符串 <strong>可以</strong> 含前导零。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong class="example">示例 1:</strong></p>
20+
21+
<pre>
22+
<strong>输入:</strong>s = "010"
23+
<strong>输出:</strong>"001"
24+
<strong>解释:</strong>因为字符串 s 中仅有一个 '1' ,其必须出现在最后一位上。所以答案是 "001" 。
25+
</pre>
26+
27+
<p><strong class="example">示例 2:</strong></p>
28+
29+
<pre>
30+
<strong>输入:</strong>s = "0101"
31+
<strong>输出:</strong>"1001"
32+
<strong>解释:</strong>其中一个 '1' 必须出现在最后一位上。而由剩下的数字可以生产的最大数字是 "100" 。所以答案是 "1001" 。
33+
</pre>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong>提示:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
41+
<li><code>s</code> 仅由 <code>'0'</code> 和 <code>'1'</code> 组成</li>
42+
<li><code>s</code> 中至少包含一个 <code>'1'</code></li>
43+
</ul>
44+
45+
## 解法
46+
47+
<!-- 这里可写通用的实现逻辑 -->
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
<!-- 这里可写当前语言的特殊实现逻辑 -->
54+
55+
```python
56+
class Solution:
57+
def maximumOddBinaryNumber(self, s: str) -> str:
58+
cnt = s.count("1")
59+
return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
60+
```
61+
62+
### **Java**
63+
64+
<!-- 这里可写当前语言的特殊实现逻辑 -->
65+
66+
```java
67+
class Solution {
68+
public String maximumOddBinaryNumber(String s) {
69+
int cnt = 0;
70+
for (char c : s.toCharArray()) {
71+
if (c == '1') {
72+
++cnt;
73+
}
74+
}
75+
return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
string maximumOddBinaryNumber(string s) {
86+
int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
87+
string ans;
88+
for (int i = 1; i < cnt; ++i) {
89+
ans.push_back('1');
90+
}
91+
for (int i = 0; i < s.size() - cnt; ++i) {
92+
ans.push_back('0');
93+
}
94+
ans.push_back('1');
95+
return ans;
96+
}
97+
};
98+
```
99+
100+
### **Go**
101+
102+
```go
103+
func maximumOddBinaryNumber(s string) string {
104+
cnt := strings.Count(s, "1")
105+
return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1"
106+
}
107+
```
108+
109+
### **TypeScript**
110+
111+
```ts
112+
function maximumOddBinaryNumber(s: string): string {
113+
let cnt = 0;
114+
for (const c of s) {
115+
cnt += c === '1' ? 1 : 0;
116+
}
117+
return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1';
118+
}
119+
```
120+
121+
### **...**
122+
123+
```
124+
125+
```
126+
127+
<!-- tabs:end -->
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# [2864. Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number)
2+
3+
[中文文档](/solution/2800-2899/2864.Maximum%20Odd%20Binary%20Number/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>binary</strong> string <code>s</code> that contains at least one <code>&#39;1&#39;</code>.</p>
8+
9+
<p>You have to <strong>rearrange</strong> the bits in such a way that the resulting binary number is the <strong>maximum odd binary number</strong> that can be created from this combination.</p>
10+
11+
<p>Return <em>a string representing the maximum odd binary number that can be created from the given combination.</em></p>
12+
13+
<p><strong>Note </strong>that the resulting string <strong>can</strong> have leading zeros.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;010&quot;
20+
<strong>Output:</strong> &quot;001&quot;
21+
<strong>Explanation:</strong> Because there is just one &#39;1&#39;, it must be in the last position. So the answer is &quot;001&quot;.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> s = &quot;0101&quot;
28+
<strong>Output:</strong> &quot;1001&quot;
29+
<strong>Explanation: </strong>One of the &#39;1&#39;s must be in the last position. The maximum number that can be made with the remaining digits is &quot;100&quot;. So the answer is &quot;1001&quot;.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
37+
<li><code>s</code> consists only of <code>&#39;0&#39;</code> and <code>&#39;1&#39;</code>.</li>
38+
<li><code>s</code> contains at least one <code>&#39;1&#39;</code>.</li>
39+
</ul>
40+
41+
## Solutions
42+
43+
<!-- tabs:start -->
44+
45+
### **Python3**
46+
47+
```python
48+
class Solution:
49+
def maximumOddBinaryNumber(self, s: str) -> str:
50+
cnt = s.count("1")
51+
return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
52+
```
53+
54+
### **Java**
55+
56+
```java
57+
class Solution {
58+
public String maximumOddBinaryNumber(String s) {
59+
int cnt = 0;
60+
for (char c : s.toCharArray()) {
61+
if (c == '1') {
62+
++cnt;
63+
}
64+
}
65+
return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
66+
}
67+
}
68+
```
69+
70+
### **C++**
71+
72+
```cpp
73+
class Solution {
74+
public:
75+
string maximumOddBinaryNumber(string s) {
76+
int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
77+
string ans;
78+
for (int i = 1; i < cnt; ++i) {
79+
ans.push_back('1');
80+
}
81+
for (int i = 0; i < s.size() - cnt; ++i) {
82+
ans.push_back('0');
83+
}
84+
ans.push_back('1');
85+
return ans;
86+
}
87+
};
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
func maximumOddBinaryNumber(s string) string {
94+
cnt := strings.Count(s, "1")
95+
return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1"
96+
}
97+
```
98+
99+
### **TypeScript**
100+
101+
```ts
102+
function maximumOddBinaryNumber(s: string): string {
103+
let cnt = 0;
104+
for (const c of s) {
105+
cnt += c === '1' ? 1 : 0;
106+
}
107+
return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1';
108+
}
109+
```
110+
111+
### **...**
112+
113+
```
114+
115+
```
116+
117+
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string maximumOddBinaryNumber(string s) {
4+
int cnt = count_if(s.begin(), s.end(), [](char c) { return c == '1'; });
5+
string ans;
6+
for (int i = 1; i < cnt; ++i) {
7+
ans.push_back('1');
8+
}
9+
for (int i = 0; i < s.size() - cnt; ++i) {
10+
ans.push_back('0');
11+
}
12+
ans.push_back('1');
13+
return ans;
14+
}
15+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func maximumOddBinaryNumber(s string) string {
2+
cnt := strings.Count(s, "1")
3+
return strings.Repeat("1", cnt-1) + strings.Repeat("0", len(s)-cnt) + "1"
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public String maximumOddBinaryNumber(String s) {
3+
int cnt = 0;
4+
for (char c : s.toCharArray()) {
5+
if (c == '1') {
6+
++cnt;
7+
}
8+
}
9+
return "1".repeat(cnt - 1) + "0".repeat(s.length() - cnt) + "1";
10+
}
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def maximumOddBinaryNumber(self, s: str) -> str:
3+
cnt = s.count("1")
4+
return "1" * (cnt - 1) + (len(s) - cnt) * "0" + "1"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function maximumOddBinaryNumber(s: string): string {
2+
let cnt = 0;
3+
for (const c of s) {
4+
cnt += c === '1' ? 1 : 0;
5+
}
6+
return '1'.repeat(cnt - 1) + '0'.repeat(s.length - cnt) + '1';
7+
}

0 commit comments

Comments
(0)

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