牛客[编程题] HJ26 字符串排序
中等 通过率:39.52% 时间限制:1秒 空间限制:32M
描述
编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
数据范围:输入的字符串长度满足 1 \le n \le 1000 \
输入描述:
输入字符串
输出描述:
输出字符串
示例1
输入:
A Famous Saying: Much Ado About Nothing (2012/8).
输出:
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
using System.Collections.Generic; using System; public class Program { public static void Main() { string line; while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case List<int> abc = new List<int>(); List<int> others = new List<int>(); char[] res = new char[line.Length]; char c; for (int i = 0; i < line.Length; i++) { c = line[i]; if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) abc.Add(i); else { others.Add(i); res[i] = line[i]; } } List<char> chars = new List<char>(); for (int i = 0; i <= 'Z' - 'A'; i++) { for (int j = 0; j < abc.Count; j++) { c = line[abc[j]]; if (c == 'A' + i || c == 'a' + i) chars.Add(c); } } for (int i = 0; i < chars.Count; i++) { res[abc[i]] = chars[i]; } for (int i = 0; i < res.Length; i++) { Console.Write(res[i]); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix