一个C#面试问题,要求是将字符串中重复字符从第二个开始都去掉,空格除外。然后显示的时候,从后往前显示。
因为C#的code,感觉实现这个还是比较容易,只是SB。所以,在面试时候,以为不会这么容易,所以,我先试着用了Dictionary去实现,发现有困难。然后改回来用StringBuilder实现,这个因为比较简单了,总以为这样会有问题。所以不知道这样是不是对的。
结果当然很不理想,我准备后面学习一下C++,看看这个题目用C++实现有什么值得注意的地方。还是有什么精华所在。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication9
{
class Program
{
public static void DeDupAndReverse(string str)
{
char blank = ' ';
StringBuilder sbDeDup = new StringBuilder();/// SB which will be de dupped.
StringBuilder sbReverse = new StringBuilder(); /// sb which will be used for reverse for (int index = ; index < str.Length; index++)
{
if ((!((sbDeDup.ToString()).Contains(str[index])) || (str[index].Equals(blank)))) /// chars contailed in the SB will be dedupped except for blank spance
{
sbDeDup.Append(str[index]);
}
else
{
continue;
}
} for (int index = sbDeDup.Length -; index >= ; index--)
{
sbReverse = sbReverse.Append(sbDeDup[index]); //reverse the sb
} for (int index = ; index < sbReverse.Length; index++)
{
System.Console.Write(sbReverse[index]); /// write the SB to the console
}
} public static void Main(string[] args)
{
string str = "Hello World"; /// one test string. DeDupAndReverse(str); /// call the method to run a test. Console.ReadLine();
}
}
}
吸收了一些人的评论,此处没有写注释。更新code如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication9
{
class Program
{
public static string ProcessString(string input)
{
StringBuilder sb = new StringBuilder();
HashSet<char> hashSet=new HashSet<char>();
foreach (var c in input)
{
if (c == ' ')
{
sb.Append(c);
}
if (hashSet.Contains(c)==false)
{
sb.Append(c);
hashSet.Add(c);
}
}
return sb.ToString();
} public static string DeDeup_Reverse(string toBeProcessed)
{
string toBeReversed = ProcessString(toBeProcessed);
StringBuilder sbReversed = new StringBuilder(toBeReversed.Length); for (int i = toBeReversed.Length - ; i >= ; i--)
{
sbReversed.Append(toBeReversed[i]);
} return sbReversed.ToString();
} public static void Main(string[] args)
{
string str = "Hello World, hello world"; Console.WriteLine(DeDeup_Reverse(str)); Console.ReadLine();
}
}
}
一个C#面试问题,要求是将字符串中重复字符从第二个开始都去掉,空格除外。然后显示的时候,从后往前显示。的更多相关文章
- Python2.7.3移除字符串中重复字符(一)
移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...
- javascript 去除字符串中重复字符
/** * 去除字符串中重复的字符,以下提供2种方法, * removeRepeat()为自己所想: * removeRepeat2()参考网上思路补充的 * removeRepeat3()敬请期待· ...
- c# String.Join 和 Distinct 方法 去除字符串中重复字符
1.在写程序中经常操作字符串,需要去重,以前我的用方式利用List集合和 contains去重复数据代码如下: string test="123,123,32,125,68,9565,432 ...
- c# 去除字符串中重复字符
String.Join 和 Distinct 方法 https://www.cnblogs.com/louby/p/6224960.html 1.在写程序中经常操作字符串,需要去重,以前我的用方式利用 ...
- String.Join 和 Distinct 方法 去除字符串中重复字符
Qualys项目中写到将ServerIP以“,”分割后插入数据库并将重复的IP去除后发送到服务器进行Scan,于是我写了下面的一段用来剔除重复IP: //CR#1796870 modify by v- ...
- python去掉字符串中重复字符的方法
If order does not matter, you can use foo = "mppmt" "".join(set(foo)) set() ...
- jst通用删除数组中重复的值和删除字符串中重复的字符
以下内容属于个人原创,转载请注明出处,非常感谢! 删除数组中重复的值或者删除字符串重复的字符,是我们前端开发人员碰到很多这样的场景.还有求职者在被面试时也会碰到这样的问题!比如:问删除字符串重复的字符 ...
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...
- Java统计一个字符串中各个字符出现的次数
相信很多人在工作的时候都会遇到这样一个,如何统计一个字符串中各个字符出现的次数呢,这种需求一把用在数据分析方面,比如根据特定的条件去查找某个字符出现的次数.那么如何实现呢,其实也很简单,下面我贴上代码 ...
随机推荐
- python xlwt 与 xlsxwriter 模块差别
Xlwt 模块有一个bug, 就是所用样式过多的话,之后的数据将使用不了样式,相反xlsxwriter 模块 不会有此问题. 用Xlwt模块的同学们,请务必转换用xlsxwriter模块 !!!!!! ...
- oracle数据库导入dmp文件
最近在自己的机子上安装了oracle11g,今天把项目的测试数据库给导入进来了,方便在本地跑起来调试.下面记录一下过程: 1,导出测试数据库的文件; 这个是在公司三楼的一台机子上,用plsql中的工具 ...
- ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
用 SQL*Plus 连接数据库的时候,除了用户名和密码外,还要在口令后面加一个主机字符串.如下: 请输入用户名:sys 口令:ANKoracle123,orcl as sysdba
- POJ_2431 Expedition 【数据结构】
一.题面 POJ2431 二.分析 主要说几个坑 1.给出的点需要根据下标排序. 2.根据不同的方式要把起始点或者终点加进去.我没有转换距离,而是直接从起始点到终点根据距离不断相减判断的,那么起点就是 ...
- python-继承,父类,子类
class Spell(object): def __init__(self, incantation, name): self.name = name self.incantation = inca ...
- [转] open-falcon编写的整个脑洞历程
[From] https://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=400225178&idx=1&sn=c98609a9 ...
- [转] JavaScript中的Truthy和Falsy介绍
[From] http://www.jb51.net/article/59285.htm 与大多数编程语言一样,JavaScript中存在boolean类型,以供逻辑判断使用.不过,和很多其它编程语言 ...
- Heap — 20181120
363. Trapping Rain Water public class Solution { /** * @param heights: a list of integers * @return: ...
- 分享一个js方法
这是一个关于参数合并的方法,这个场景也经常遇到,比如我们现在要对微信小程序的wx.request进行再一次封装,会涉及到一些默认的参数和每次使用自己传递的参数合并问题,分享代码. var extend ...
- GreenPlum 大数据平台--运维(二)
.如何获取查询运行时和已用时间. 例子: Select tstart, tfinish, (tfinish-tstart) as total_time, trim(query_text) from q ...