微软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 参考 ...
随机推荐
- hbase--知识点总结2
--用java操作hbase 1.配置jar包环境 创建hbase项目 --> 新建folder文件夹 --> 将hbase相关jar包全部导入到java项目之中 --> add b ...
- JAVA中的配置文件XML
一:概念 1.XML Extensible markup Language 可拓展标记语言 2.功能:存储数据(配置文件,在网络中传输数据) 3.html和xml的区别 3.1xml标记全是自定义的 ...
- DJango 基础 (1)
django基础 知识点: 基本认知 工具准备 新建项目 目录及文件说明 开发服务器 创建视图函数 新建应用(app) 1.基本认知 Django是用Python开发的一个免费开源的Web框架,可以用 ...
- 735. Asteroid Collision彗星相撞后的消失数组
[抄题]: We are given an array asteroids of integers representing asteroids in a row. For each asteroid ...
- 65. Valid Number 判断字符串是不是数字
[抄题]: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " ...
- thinkphp 视图(一)
视图 View <?php namespace app\index\controller; class Index{ public function index(){ return view() ...
- Solidity的三种合约间的调用方式 call、delegatecall 和 callcode
0x00 前言 Solidity(http://solidity.readthedocs.io/en/v0.4.24/) 是一种用与编写以太坊智能合约的高级语言,语法类似于 JavaScript. S ...
- [转载] ul li css 做横向菜单
原文地址: http://www.cnblogs.com/amylis_chen/archive/2011/09/24/2188398.html 第一步:建立一个无序列表 我们先建立一个无序列表,来建 ...
- java项目中VO和DTO以及Entity,各自是在什么情况下应用
1.entity里的每一个字段,与数据库相对应, 2.dto里的每一个字段,是和你前台页面相对应, 3.VO,这是用来转换从entity到dto,或者从dto到entity的中间的东西. 举个例子 ...
- java多线程系列6 synchronized 加强版 ReentrantLock
ReentrantLock类是可重入.互斥.实现了Lock接口的锁,它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力.ReenreantLock类的常用方法有: Re ...