题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

简简单单冒泡
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner ss = new Scanner(System.in);
        while (ss.hasNext()) {
            String s = ss.nextLine();
            byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
            for (int i = 0; i < bytes.length; i++) {
                for (int j = 0; j < bytes.length-i-1; j++) {
                    int u=j+1;
                    if (bytes[j] < 65 || (bytes[j] > 90 && bytes[j] < 97) || bytes[j] > 122) {
                        continue;
                    }
                    while(u< bytes.length&&(bytes[u] < 65 || (bytes[u] > 90 && bytes[u] < 97) || bytes[u] > 122)) {
                        u++;
                    }
                    if(u>= bytes.length){
                        continue;
                    }
                    byte x = bytes[j];
                    byte y = bytes[u];
                    if (x >= 65 && x <= 90) {
                        x += 32;
                    }
                    if (y >= 65 && y <= 90) {
                        y += 32;
                    }
                    if (x > y) {
                        byte a = bytes[j];
                        bytes[j] = bytes[u];
                        bytes[u] = a;
                    }
                }
            }
            System.out.println(new String(bytes));
        }
    }
}

#冒泡排序divdivforiinrangen1divdivforjinrangeni1divdivifbjgtbj1divdivbjbj1bj1bjdivdivajaj1aj1ajdivdiv#
全部评论

相关推荐

无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务