3216. 交换后字典序最小的字符串

class Solution {
    public String getSmallestString(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int number1 = c - '0';
            if (i == s.length() - 1) {
                return s;
            }
            c = s.charAt(i + 1);
                int number2 = c - '0';
            if (((number1 % 2 == 0 && number2 % 2 == 0) || (number1 % 2 != 0 && number2 % 2 != 0)) && number1 > number2) {
                s = swap(i, s);
                return s;
            }
        }
        return s;   
    }

    public String swap(int i, String s) {
        byte[] bytes = s.getBytes();
        byte temp = bytes[i + 1];
        bytes[i + 1] = bytes[i];
        bytes[i] = temp;
        return new String(bytes);

    }
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部