微软2014校招笔试题-String reorder
- Sample Input
-
aabbccdd 007799aabbccddeeff113355zz 1234.89898 abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
- Sample Output
-
abcdabcd 013579abcdefz013579abcdefz <invalid input string> abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
Description
For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).
Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.
Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).
Input
Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
Output
For each case, print exactly one line with the reordered string based on the criteria above.
我的水平有限,但最终也解答出来了。下面贴出个人的答案,欢迎大牛批评指正。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StringReorder { class Program { static void Main(string[] args) { string input = string.Empty; input = Console.ReadLine(); GetResults(input); } static void GetResults(string input) { string results = ""; if (MatchRequirement(input)) { List<char> inputCharList = sort(input).ToList(); List<char> tempList = new List<char>(); while (inputCharList.Count > 0) { tempList.Add(inputCharList[0]); inputCharList.RemoveAt(0); int i = 0; while (i < inputCharList.Count) { if (inputCharList[i] > tempList[tempList.Count - 1]) { tempList.Add(inputCharList[i]); inputCharList.RemoveAt(i); } else { i++; } } results += ListToString(tempList); tempList.Clear(); } Console.WriteLine(results); } } static string ListToString(List<char> tempList) { string tempStr = ""; for (int i = 0; i < tempList.Count; i++) { tempStr += tempList[i]; } return tempStr; } static string sort(string inputString) { char[] inputChars = inputString.ToArray(); string tempStr = ""; Array.Sort(inputChars); for (int i = 0; i < inputChars.Length; i++) { tempStr += inputChars[i]; } return tempStr; } static bool MatchRequirement(string inputString) { bool tempValue = false; char[] inputChars = inputString.ToArray(); for (int i = 0; i < inputChars.Length; i++) { if (!match(inputChars[i])) { Console.WriteLine("<invalid input string>"); tempValue = false; break; } else { tempValue = true; } } return tempValue; } static bool match(char c) { if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) { return true; } return false; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?