用空间换时间,只进行两次循环:
1、第一次遍历字符串,创建一个数组,记录从a到z,分别出现了几次
2、第二次遍历字符串,检测记录表中是否为1,如果是则找到
用空间换时间,只进行两次循环:
1、第一次遍历字符串,创建一个数组,记录从a到z,分别出现了几次
2、第二次遍历字符串,检测记录表中是否为1,如果是则找到
string str = console.readline( );
int[] countarray=new int[26];
for(int i = 0; i < str.length; i++)
{
countarray[str[i] - 'a' ] = countarray[str[i] - 'a' ] + 1;
}
bool is find = false;
char c = ' ';
for(int i = 0; i < str.length; i++)
{
if(counarray[str[i] -'a'] == 1)
{
isFind = true;
c = str [i];
}
}
string str = Console.ReadLine();
int[] countArray = new int[26];
for(int i =0;i<str.Length;i++)
{
countArray[str[i]-'a']=countArray[str[i]-'a']+1';
}
bool isFind = false;
char c=' ';
for(int i = 0;i< str.Length; i++){
if(countArray[str[i] - 'a']==1){
isFind = true;
c = str[i];
break;
}
}
if (isFind== false){
Console.writeLine(no");
}
else{
Console.Writeline(c;
}
static void test06()
{
string str = Console.ReadLine();
int[] countArray = new int[26];
for (int i = 0; i < str.Length; i++)
{
countArray[str[i] - 'a'] = countArray[str[i] - 'a'] + 1;
}
for (int i = 0; i < countArray.Length; i++)
{
Console.Write(countArray[i]+" ");
}
bool isFind = false;
char c = ' ';
for (int i = 0; i < str.Length; i++)
{
if (countArray[str[i] - 'a'] == 1)
{
isFind = true;
c = str[i];
break;
}
}
Console.WriteLine();
if (isFind==false)
{
Console.WriteLine("no");
}
else
{
Console.WriteLine(c);
}
}
Conaole.ResLine();用户输入
//找出字符串中第一个仅出现一次的字符
string str = Console.ReadLine();
bool isOnlyOne = false;
for (int i = 0; i < str.Length; i++)
{
if (str.IndexOf(str[i]) == str.LastIndexOf(str[i]))
{
isOnlyOne = true;
Console.WriteLine($"第一个仅出现一次的字符是:{str[i]}");
break;
}
}
if (isOnlyOne == false) Console.WriteLine($"所有字符均出现两次及以上");