题解 | #找出字符串中第一个只出现一次的字符#
找出字符串中第一个只出现一次的字符
https://www.nowcoder.com/practice/e896d0f82f1246a3aa7b232ce38029d4
// HJ59-3 找出字符串中第一个只出现一次的字符.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 #include<iostream> #include<bits/stdc++.h> using namespace std; //int dp[128]; int main() { string s; while (cin >> s) { string res = "-1"; int len = s.size(); vector<int>dp(128, 0); for (int i = 0; i < len; i++) { int num = int(s[i]); dp[num]++; } for (int i = 0; i < len; i++) { int num = int(s[i]); if (dp[num] == 1) { res = s[i]; break; } } cout << res << endl; } return 0; }