import java.util.HashMap;
import java.util.Map;
/**
* @author xienl
* @description 第一个只出现一次的字符
* @date 2022/7/5
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public int FirstNotRepeatingChar(String str) {
int ans = -1;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < str.length(); i++){
map.put(str.charAt(i), map.getOrDefault(str.charAt(i), 0) + 1);
}
for (int i = 0; i < str.length(); i++){
if (map.get(str.charAt(i) )== 1){
return i;
}
}
return -1;
}
}
牛客网:NC31 第一个只出现一次的字符
最新推荐文章于 2025-12-01 18:34:51 发布
本文介绍了一种使用Java实现的方法,该方法能够在输入字符串中找出并返回首次出现的非重复字符的位置。通过运用HashMap来记录每个字符出现的次数,进而确定唯一字符。适用于字符串处理和算法学习。

757

被折叠的 条评论
为什么被折叠?



