因为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. hdu1702 ACboy needs your help again!(栈处理)

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  2. PIE SDK灾前灾后对比

    灾前灾后对比功能是GIS软件中常用的功能之一,指利用多时相获取的覆盖同一地表区域的遥感影像及其它辅助数据来确定和分析地表变化.它利用计算机图像处理系统,对不同时段目标或现象状态的变化进行识别.分析:它 ...

  3. 转 .net数据类型

    .net 数据类型   short s=0; s = s + 1; 和short s=0; s += 1; 这两个表达式有什么区别,会报什么错误? 有区别吗??大家要想想呢还是要测试一下啊,我选择测试 ...

  4. Android耗时操作

    No subscribers registered for event class com.test.MessageEvent import de.greenrobot.event.EventBus; ...

  5. MongoDB数据库进阶 --- 增删查改...

    注意: monogdb数据在使用之后必须及时 mongodb.close()否则后台崩溃. 在之前的文章中,我已经介绍了什么事MongoDB以及怎么在windows下安装MongoDB等等基本知识. ...

  6. ife task0003学习笔记(三):JavaScript闭包

    一.this易错分析 在学习闭包的时候,有一个概念this很重要,关于this的理解,下面3种情况:this指向谁? fn.call(obj1); obj2.fn() fn() 答案是obj1 obj ...

  7. ebiao 报表工具使用入门

    一.ebiao简价 e表是一个功能强大的Web报表工具,可使复杂报表的设计简单化,避免了大量的复杂SQL编写以及编程来准备数据,报表设计的效率大大提高.e表分为e表 for .NET和e表 for J ...

  8. js字符操作

    js字符串方法预览: fromCharCode(num1, num2,,,), charAt(), charCodeAt(), length, split(''), slice(start, end? ...

  9. [转] EF cannot be tracked because another instance of this type with the same key is already being tracked

    本文转自:http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-ob ...

  10. Spring JdbcTemplate 使用总结

    1.查询Object public Classify queryClassifById(int id){ String sql="select * from t_classify where ...