题解 | #找出字符串中第一个只出现一次的字符#

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

http://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4

思路

  1. 获得字符串及其长度
  2. 循环比较和每个字符相同的字符,并记录在对应的数组中
  3. 当检测记录数组中有1时,退出循环
  4. 特殊用标志位处理没有只出现1次的字符

Answer

#include<stdio.h>
#include<string.h>

int main()
{
    char str[1000];
    int str_cnt[1000];
    scanf("%s",str);
    int len = strlen(str);
    
    for(int i=0; i<len; i++){
        int k=0; 
        for(int j=0; j<len; j++){
            
            if(str[i]==str[j]){
                k++;
            }
            str_cnt[i]=k;
            
        }
    }
    int flg=1;
    for(int i=0; i<len; i++){
        if(str_cnt[i]==1){
            flg=1;
            printf("%c\n",str[i]);
            break;
        }else{
            flg=0;
        }
    }
    if(flg==0){
        printf("-1\n");
    }
        
    
    return 0;
}
华为机试刷题 文章被收录于专栏

刷华为机试习题

全部评论

相关推荐

明天不下雨了:我靠2022了都去字节了还什么读研我教你****:你好,本人985电子科大在读研一,本科西南大学(211)我在字节跳动实习过。对您的岗位很感兴趣,希望获得一次投递机会。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务