微软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 参考 ...
随机推荐
- php中使用com组件出现"拒绝访问"的处理
php中使用com组件出现"拒绝访问"的处理 2010年05月14日 12:28:00 阅读数:1529 代码如下, // 建立一个指向新COM组件的索引 $word = new ...
- 配置Java运行环境
变量名:JAVA_HOME 变量值:D:\Program Files\Java\jdk1.8.0_151(java安装目录) 变量名:Path 变量值:.;%J ...
- Moment.js 基本用法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- FortiGate外网IPSec链路及运维专线链路到个别网段不通
1.现状: 如图,用户网段有192.168.50.0/24.192.168.51.0/24和192.168.52.0/24.192.168.53.0/24.在防火墙上有静态路由到运维专线的10.160 ...
- 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树
[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...
- [leetcode]156.Binary Tree Upside Down颠倒二叉树
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- Windows7 VS2015 下编译 PythonQt3.2
本文在使用vs2015编译python3.6.7源代码后,编译的PythonQt3.2.如果使用python二进制文件进行安装,注意python的路径即可 本机环境: 1.win7 64 旗舰版 2. ...
- etcd-v2第二集
参考文章:https://github.com/coreos/etcd/blob/master/Documentation/v2/api.mdhttp://www.cnblogs.com/zhengr ...
- css初始
css概念及作用 css即层叠样式表的英文缩写 作用:1 渲染页面 2 页面布局 css语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 格式: selector{ prope ...
- typescript里面调用javasript
index.html 里面加入函数: function tellYou() { egret.log("tell you."); javascript:android.funA(); ...