首页 > 其他分享 >[LeetCode] 2864. Maximum Odd Binary Number

[LeetCode] 2864. Maximum Odd Binary Number

时间:2024-03-01 11:22:08浏览次数:27  
标签:Binary 2864 binary int Number number 二进制 字符串 sb

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'.

最大二进制奇数。

给你一个 二进制 字符串 s ,其中至少包含一个 '1' 。
你必须按某种方式 重新排列 字符串中的位,使得到的二进制数字是可以由该组合生成的 最大二进制奇数 。
以字符串形式,表示并返回可以由给定组合生成的最大二进制奇数。
注意 返回的结果字符串 可以 含前导零。

思路

因为需要保证最后的输出是一个奇数,所以需要预留一个 1 放在最低位,其他的 1 统统放在最高位。
具体做法是先扫描一遍 input 字符串,记录一下 0 的个数和 1 的个数,在保证最后一个位置是 1 的情况下优先放 1,其余位置放 0 即可。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public String maximumOddBinaryNumber(String s) {
        int n = s.length();
        int one = 0;
        for (char c : s.toCharArray()) {
            if (c == '1') {
                one++;
            }
        }
        int zero = n - one;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < one - 1; i++) {
            sb.append('1');
        }
        for (int i = 0; i < zero; i++) {
            sb.append('0');
        }
        sb.append('1');
        return sb.toString();
    }
}

标签:Binary,2864,binary,int,Number,number,二进制,字符串,sb
From: https://www.cnblogs.com/cnoodle/p/18046583

相关文章

  • Codeforces 441E Valera and Number
    首先看到\(\times2\)\(+1\)和最后答案的计算方式,能想到看成二进制来处理。考虑到\(\times2\)就是在最后加了一个\(0\)。不妨倒过来看,\(\times2\)就相当于舍弃了最低位。于是可以考虑\(\text{DP}\),\(f_{i,j}\)为考虑后面的\(i\)个操作,目前\(+\)的值为\(j\)的......
  • [LeetCode] 2583. Kth Largest Sum in a Binary Tree
    Youaregiventherootofabinarytreeandapositiveintegerk.Thelevelsuminthetreeisthesumofthevaluesofthenodesthatareonthesamelevel.Returnthekthlargestlevelsuminthetree(notnecessarilydistinct).Iftherearefewerthan......
  • [USACO11NOV]Binary Sudoku G 题解
    定义一个\(3\times3\)的表格\(a\),表示每个小九宫格内1的个数的奇偶状态。再定义两个长为\(9\)的数组\(c0,c1\),表示每行每列上1的个数的奇偶状态。当\(d_{i,j}\)取反时,会将\(a_{[\frac{i}{3}],[\frac{j}{3}]},c0_i,c1_j\)各取反一次。要将\(a_{i,j}\)全部变为0......
  • Nginx添加第三方模块,出现“is not binary compatible in”错误的解决方案
    动态编译好第三方模块:ngx_http_ts_module.so 检测nignx配置,异常sudo/usr/local/openresty/nginx/sbin/nginx-tnginx:[emerg]module"/usr/local/openresty/nginx/modules/ngx_http_ts_module.so"isnotbinarycompatiblein/usr/local/openresty/nginx/conf/nginx.conf......
  • react错误:Uncaught Error: Too many re-renders. React limits the number of renders
    react错误:UncaughtError:Toomanyre-renders.Reactlimitsthenumberofrenderstopreventaninfiniteloop. 信铁寒胜:更改页面数据时未放到useEffect方法内,导致页面一直在刷新。  原因1:错误写法:<divclassName='article_item'onClick={toArticleDetail......
  • 机器学习策略篇:详解单一数字评估指标(Single number evaluation metric)
    单一数字评估指标无论是调整超参数,或者是尝试不同的学习算法,或者在搭建机器学习系统时尝试不同手段,会发现,如果有一个单实数评估指标,进展会快得多,它可以快速告诉,新尝试的手段比之前的手段好还是差。所以当团队开始进行机器学习项目时,经常推荐他们为问题设置一个单实数评估指标。......
  • LeetCode] 2476. Closest Nodes Queries in a Binary Search Tree
    Youaregiventherootofabinarysearchtreeandanarrayqueriesofsizenconsistingofpositiveintegers.Finda2Darrayanswerofsizenwhereanswer[i]=[mini,maxi]:miniisthelargestvalueinthetreethatissmallerthanorequaltoqueries[......
  • MySQL备份恢复数据--binary-mode is enabled and mysql is run in non-interactive...
    使用mysqldump;MySQL自带的逻辑备份工具。mysqldump[选项]数据库名[表名]>脚本名mysqldump[选项]--数据库名[选项表名]>脚本名mysqldump[选项]--all-databases[选项]>脚本名备份mysqldump-hlocalhost-uwordpress-pwordpress_20200104>c......
  • SciTech-Mathmatics-automatic equation Numbering / \tag{E} / \label{E} / \ref{
    https://discourse.devontechnologies.com/t/numbering-and-tagging-equations-in-mathjax/69524/2IwentaroundincirclestryingtofigureouthowtonumberandtagequationsinMathJaxinDT3.TheMathJaxDocsshowshowtodoautomaticequationnumbering21......
  • Number【数字】
    定义:数字类型是顾名思义是用来存储数值的,需要记住的是,如果改变了数字数据类型的值,将重新分配内存空间。Python支持三种不同的数值类型:整型(int)-通常被称为是整型或整数,是正或负整数,不带小数点。Python3整型是没有限制大小的,可以当作Long类型使用,所以Python3没有Long......