因为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. Android 数据库框架GreenDao使用

    1.GreenDao3介绍 (1)基本概念 (2)GreenDao3工作原理 (3)GreenDao优点 (4)GreenDao3版本的改进 2.GreenDao3的相关配置概念介绍 (1)配置项目( ...

  2. Java 简单的RPC 实现

    借用了网上某大神的例子.... 目录结构是这样的... RpcFramework 主要是两个方法.一个是暴露服务,一个为引用服务.暴露服务的主要作用是声明一个接口的实现类.可以通过socket 远程调 ...

  3. 洛谷 P2056 [ZJOI2007]捉迷藏 题解【点分治】【堆】【图论】

    动态点分治入 门 题? 题目描述 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由 \(N\) 个屋 ...

  4. POJ - 1222 / POJ - 3279 枚举第一行

    说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...

  5. AngularJS 中ng-model通过$watch动态取值

    这个例子的意思是,当xxxx的长度不超过6时,xxxx和yyyy两个input的model是无关的,但当xxxx超过6,则yyyy会跟随其值而变化. 另一种做法是在input的ng-model后面添加 ...

  6. scrapy_redis之官网列子domz

    一.  domz.py from scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, ...

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

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

  8. nginx 反向代理导致的session丢失的问题

    [原文链接] https://blog.csdn.net/xiaweiyidengzhewo/article/details/80921750 注意这篇文章解释的是“丢失”而不是“一致性”

  9. 使用JWT来实现单点登录功能

    出处: https://www.cnblogs.com/zexin/p/10389541.html 我们平时自己开发项目,分布式的结构时,访问量不大,但是又不想搭建redis服务器,这时我觉得jwt不 ...

  10. javascript中构造函数与普通函数的区别还有关于“new”操作符的一些原理

    有一种创建对象的方法叫做工厂模式,例如: function person(name,age){ var o=new Object(); o.name=name; o.age=age; return o ...