LeetCode2864. Maximum Odd Binary Number

文章目录

一、题目

You are given a binary string s that contains at least one ‘1’.

You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.

Return a string representing the maximum odd binary number that can be created from the given combination.

Note that the resulting string can have leading zeros.

Example 1:

Input: s = “010”
Output: “001”
Explanation: Because there is just one ‘1’, it must be in the last position. So the answer is “001”.
Example 2:

Input: s = “0101”
Output: “1001”
Explanation: One of the '1’s must be in the last position. The maximum number that can be made with the remaining digits is “100”. So the answer is “1001”.

Constraints:

1 <= s.length <= 100
s consists only of ‘0’ and ‘1’.
s contains at least one ‘1’.

二、题解

class Solution {
public:
    string maximumOddBinaryNumber(string s) {
        int n = s.size();
        int cnt = 0;
        for(int i = 0;i < n;i++){
            if(s[i] == '1') cnt++;
        }
        string res;
        for(int i = 0;i < cnt - 1;i++){
            res += "1";
        }
        for(int i = cnt - 1;i < n - 1;i++){
            res += "0";
        }
        res += "1";
        return res;
    }
};

相关推荐

  1. LeetCode 2884. 修改列

    2024-03-15 03:50:01       44 阅读
  2. LeetCode[题解] 2864. 最大二进制奇数

    2024-03-15 03:50:01       21 阅读
  3. leetcode 2864.最大二进制奇数

    2024-03-15 03:50:01       23 阅读
  4. LeetCode2864. Maximum Odd Binary Number

    2024-03-15 03:50:01       28 阅读
  5. LeetCode 2864.最大二进制奇数

    2024-03-15 03:50:01       22 阅读
  6. leetcode - 284. Peeking Iterator

    2024-03-15 03:50:01       17 阅读
  7. LeetCode 2866. 美丽塔 II

    2024-03-15 03:50:01       42 阅读
  8. LeetCode2865. Beautiful Towers I

    2024-03-15 03:50:01       35 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-15 03:50:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-15 03:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-15 03:50:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-15 03:50:01       20 阅读

热门阅读

  1. 动态规划 Leetcode 494 目标和

    2024-03-15 03:50:01       21 阅读
  2. 缓存穿透和缓存击穿有什么区别?

    2024-03-15 03:50:01       23 阅读
  3. jsonl文件介绍

    2024-03-15 03:50:01       22 阅读
  4. 封装数据请求方法与接口方法

    2024-03-15 03:50:01       25 阅读
  5. C++基础5:自定义类型与字符串

    2024-03-15 03:50:01       18 阅读
  6. Avalonia之ListBox模版设置

    2024-03-15 03:50:01       20 阅读
  7. Crash Course Computer Science2

    2024-03-15 03:50:01       20 阅读
  8. 在哪些领域中最需要使用 OCR 识别技术?

    2024-03-15 03:50:01       20 阅读
  9. @ConfigurationProperties 的基本用法

    2024-03-15 03:50:01       18 阅读
  10. 题目 2656: 刷题统计

    2024-03-15 03:50:01       20 阅读
  11. 数据库(mysql)-新手笔记(触发器,存储过程)

    2024-03-15 03:50:01       16 阅读
  12. Leetcode面试经典150题

    2024-03-15 03:50:01       20 阅读