微软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;
}
}
}
微软2014校招笔试题-String reorder的更多相关文章
- 微软2017校招笔试题3 registration day
题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...
- 微软2017校招笔试题2 composition
题目 Alice writes an English composition with a length of N characters. However, her teacher requires ...
- 阿里2014校招笔试题(南大)——利用thread和sleep生成字符串的伪随机序列
引言:题目具体描述记不大清了,大概是:Linux平台,利用线程调度的随机性和sleep的不准确性,生成一个各位均不相同的字符数组的伪随机序列.不得使用任何库函数.(这句记得清楚,当时在想线程库算不算, ...
- 剑指Offer——腾讯+360+搜狗校招笔试题+知识点总结
剑指Offer--腾讯+360+搜狗校招笔试题+知识点总结 9.11晚7:00,腾讯笔试.选择题与编程.设计题单独计时. 栈是不是顺序存储的线性结构啊? 首先弄明白两个概念:存储结构和逻辑结构. 数据 ...
- 剑指Offer——美团内推+校招笔试题+知识点总结
剑指Offer--美团内推+校招笔试题+知识点总结 前言 美团9.9内推笔试.9.11校招笔试,反正就是各种虐,笔试内容如下: 知识点:图的遍历(DFS.BFS).进程间通信.二叉查找树节点的删除及中 ...
- 剑指Offer——CVTE校招笔试题+知识点总结(Java岗)
剑指Offer(Java岗)--CVTE校招笔试题+知识点总结 2016.9.3 19:00参加CVTE笔试,笔试内容如下: 需要掌握的知识:Linux基本命令.网络协议.数据库.数据结构. 选择题 ...
- 剑指Offer——京东校招笔试题+知识点总结
剑指Offer--京东校招笔试题+知识点总结 笔试感言 经过一系列的笔试,发觉自己的基础知识还是比较薄弱的,尤其是数据结构和网络,还有操作系统.工作量还是很大的.做到精确制导的好方法就是在网上刷题,包 ...
- hiho #1288 微软2016.4校招笔试题 Font Size
#1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...
- 2016京东Android研发校招笔试题
一.选择题汇总,具体的记不住啦.. 1.计网:ip的网络前缀.SNMP(报文组成):http://blog.csdn.net/shanzhizi/article/details/11606767 参考 ...
随机推荐
- 移动端过禁止输入emoji表情实现方案
最近手头上的项目有一个需求就是输入框不能输入表情,然后就各种在网上找资料,网上好多人给的方案是: str = str.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uD ...
- Lua中面向对象
一.Lua中类的简单实现: (1)版本——摘自 Cocos2.0中的: --Create an class. function class(classname, super) local superT ...
- gitlab 备份与恢复
1. gitlab 备份命令:# gitlab-rake gitlab:backup:create 1.1 查看备份文件(默认备份路径:/var/opt/gitlab/backups)# ls /va ...
- java简单的文件读写工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- 数据库-1055报错-把only_full_group_by去掉
[Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...
- Spring MVC请求处理流程
从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发 Spring MVC工作流程图 图一 图二 Spring工作流程描述 ...
- sql-多表查询
一:外连接 1.左外连接(left join) select * from A left join B on A.id=B.a_id 结果如下 很明显,A表的所有数据都显示出来了 ...
- spring入门——applicationContext与BeanFactory的区别
我们知道从applicationContext容器对象中如何获取Bean了,其实spring框架还有另外一种获取bean的方法:BeanFactory代码如下: BeanFactory factory ...
- spring入门--spring入门案例
spring是一个框架,这个框架可以干很多很多的事情.感觉特别吊.但是,对于初学者来说,很难理解spring到底是干什么的.我刚开始的时候也不懂,后来就跟着敲,在后来虽然懂了,但是依然说不明白它到底是 ...
- docker容器的使用
Docker客户端 docker客户端非常简单,我们可以直接输入docker命令来查看到Docker客户端的所有命令选项. runoob@ docker 可以通过命令docker command -- ...