因为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#面试问题,要求是将字符串中重复字符从第二个开始都去掉,空格除外。然后显示的时候,从后往前显示。的更多相关文章

  1. Python2.7.3移除字符串中重复字符(一)

    移除重复字符很简单,这里是最笨,也是最简单的一种.问题关键是理解排序的意义: # coding=utf-8 #learning at jeapedu in 2013/10/26 #移除给定字符串中重复 ...

  2. javascript 去除字符串中重复字符

    /** * 去除字符串中重复的字符,以下提供2种方法, * removeRepeat()为自己所想: * removeRepeat2()参考网上思路补充的 * removeRepeat3()敬请期待· ...

  3. c# String.Join 和 Distinct 方法 去除字符串中重复字符

    1.在写程序中经常操作字符串,需要去重,以前我的用方式利用List集合和 contains去重复数据代码如下: string test="123,123,32,125,68,9565,432 ...

  4. c# 去除字符串中重复字符

    String.Join 和 Distinct 方法 https://www.cnblogs.com/louby/p/6224960.html 1.在写程序中经常操作字符串,需要去重,以前我的用方式利用 ...

  5. String.Join 和 Distinct 方法 去除字符串中重复字符

    Qualys项目中写到将ServerIP以“,”分割后插入数据库并将重复的IP去除后发送到服务器进行Scan,于是我写了下面的一段用来剔除重复IP: //CR#1796870 modify by v- ...

  6. python去掉字符串中重复字符的方法

      If order does not matter, you can use   foo = "mppmt" "".join(set(foo)) set() ...

  7. jst通用删除数组中重复的值和删除字符串中重复的字符

    以下内容属于个人原创,转载请注明出处,非常感谢! 删除数组中重复的值或者删除字符串重复的字符,是我们前端开发人员碰到很多这样的场景.还有求职者在被面试时也会碰到这样的问题!比如:问删除字符串重复的字符 ...

  8. 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数

    有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...

  9. Java统计一个字符串中各个字符出现的次数

    相信很多人在工作的时候都会遇到这样一个,如何统计一个字符串中各个字符出现的次数呢,这种需求一把用在数据分析方面,比如根据特定的条件去查找某个字符出现的次数.那么如何实现呢,其实也很简单,下面我贴上代码 ...

随机推荐

  1. php 编码和解码的函数

    URL: urlencode(); //编码 urldecode(); //解码 URL与数组互转: parse_url () http_build_query() base64: base64_en ...

  2. matlab的pdist函数详解

    Pairwise distance between pairs of object(Pdist函数用于各种距离的生成) 语法: D=pdist(x) D=pdist(x,distance) 解释: D ...

  3. linux 检测服务器端口工具

    #nmp# nmap 127.0.0.1 #netstat# netstat -anlp | grep 22 #telnet# 服务器端口即使处于监听状态,但是防火墙iptables屏蔽了该端口,是无 ...

  4. 洛谷 P4568 [JLOI2011]飞行路线

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...

  5. 江西理工大学南昌校区排名赛 E: 单身狗的种树游戏

    题目描述 萌樱花是一只单身狗. 萌樱花今天决定种树,于是他来到了自己家的后院. 萌樱花的后院有n个树坑,所有树坑排列在一行上,每一个树坑都可以种一棵树,相邻树坑间的距离为1,现在所有的树坑都是空着的. ...

  6. C++ GUI Qt4编程(09)-3.3spreadsheet-toolbar

    1. C++ GUI Qt4编程第三章,增加工具栏.状态栏和快捷键. 2. mainwindow.h /**/ #ifndef MAINWINDOW_H #define MAINWINDOW_H #i ...

  7. 计算两个日期相差的天数 js php日期 减一年

    计算两个日期相差的天数 //sDate1和sDate2是yyyy-MM-dd格式 function dateDiff(sDate1, sDate2) { var aDate, oDate1, oDat ...

  8. mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)

    1.使用相同索引键值的冲突 由于mysql 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但如果是使用相同的索引键,是会出现锁冲突的.设计时要注意 例如:city表city_ ...

  9. oracle 基础知识(七)----动态性能视图

    一,动态性能视图介绍 动态性能视图属于数据字典,系统管理员用户 SYS 可以访问它们.在数据库打开和使用时不断进行更新,而且它们的内容主要与性能有关.虽然这些视图很像普通的数据库表,但它们不允许用户直 ...

  10. mysql 流程函数

    一 , 流程函数 函数 功能 if(value,t f) 如果value是真,返回t;否则返回f ifnull(value1,value2) 如果valve1不为空返回value1,否则返回value ...