张德长

导航

牛客[编程题] HJ59 找出字符串中第一个只出现一次的字符

HJ59 找出字符串中第一个只出现一次的字符
中等  通过率:32.27%  时间限制:1秒  空间限制:32M
 

描述

找出字符串中第一个只出现一次的字符
 
 

数据范围:输入的字符串长度满足 1 \le n \le 1000 \

 

输入描述:

输入一个非空字符串

输出描述:

输出第一个只出现一次的字符,如果不存在输出-1

示例1

输入:
asdfasdfo
输出:
o

 

 

 using System;
    public class Program
    {
        public static void Main()
        {
            string line; bool isSingle; string res = "-1";
            while ((line = System.Console.ReadLine()) != null)
            { // 注意 while 处理多个 case
                
                for (int i = 0; i < line.Length; i++)
                {
                     isSingle = true;
                    for (int j = 0; j < line.Length; j++)
                    {
                        if (j!=i&&line[j]==line[i])
                        {
                            isSingle = false;
                            break;
                        }
                    }
                    if (isSingle)
                    {
                        res= line[i].ToString();
                        break;
                    }
                }
                Console.WriteLine(res);
            }
        }
    }

 

posted on 2023-11-11 21:35  张德长  阅读(10)  评论(0编辑  收藏  举报