微软实习机试题 String reorder 的 Java 实现
2014-04-12 22:17
288 查看
【Description】
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.
【样例输入】
【样例输出】
【AC代码】
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.
【样例输入】
aabbccdd 007799aabbccddeeff113355zz 1234.89898 abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
【样例输出】
abcdabcd 013579abcdefz013579abcdefz <invalid input string> abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa
【AC代码】
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str, re; while (in.hasNextLine()) { str = in.nextLine(); re = process(str); System.out.println(re); } } private static String process(String str) { char[] chs = str.toCharArray(); for (char c : chs) { if (!(Character.isDigit(c) || Character.isLowerCase(c))) { return "<invalid input string>"; } } List<Character> li = new ArrayList<Character>(); for (char c : chs) { li.add(c); } String re = ""; Set<Character> se = new HashSet<Character>(); while (li.size() > 0) { for (int i = li.size() - 1; i >= 0; i--) { if (se.add(li.get(i))) { li.remove(i); } } String re2=""; for (Character ttt : se) { re2 += ttt; } char[] ttt = re2.toCharArray(); Arrays.sort(ttt); String tss = new String(ttt); se.clear(); re += tss; } return re; } }
相关文章推荐
- 微软2014实习生及秋令营技术类职位在线测试 String reorder java
- 微软2014年4月 实习生招聘机试题 1.String reorder
- 【微软2014实习生】 String reorder
- 微软2014校招笔试题-String reorder
- 微软2014校招笔试题-String reorder
- 微软2014实习生在线测试之String reorder .
- 关于一道J笔试或者机试题的Java实现:从键盘输入一串字符,翻转后输出(要求不使用string相关类即对象)
- 【微软2014实习生及秋令营技术类职位在线测试】题目1 : String reorder
- 微软2014实习生在线测试之String reorder
- 微软2014实习生及秋令营技术类职位在线测试--String reorder
- 微软2014实习生及校招秋令营技术类职位在线测试:1.String reorder
- 微软2014实习生及秋令营技术类职位在线测试:String reorder
- LeetCode:Reorder List C++与Java实现
- 微软2014实习生及秋令营技术类职位在线测试第一题:String reorder
- leetcode reorder-list(java实现)
- 微软2014实习生招聘在线测试第1题——string reorder
- 微软2014实习生及秋令营技术类职位在线测试 题目1 : String reorder
- 微软2014实习生及秋令营技术类职位在线测试-1_String reorder
- 关于一道J笔试或者机试题的Java实现:从键盘输入一串字符,翻转后输出(要求不使用string相关类即对象)
- 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)