当前位置:网站首页>LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
LeetCode(剑指 Offer)- 50. 第一个只出现一次的字符
2022-07-28 08:49:00 【放羊的牧码】
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 字节跳动
AC 代码
- Java
// 解决方案(1)
class Solution {
public char firstUniqChar(String s) {
HashMap<Character, Boolean> dic = new HashMap<>();
char[] sc = s.toCharArray();
for(char c : sc)
dic.put(c, !dic.containsKey(c));
for(char c : sc)
if(dic.get(c)) return c;
return ' ';
}
}
// 解决方案(2)
class Solution {
public char firstUniqChar(String s) {
Map<Character, Boolean> dic = new LinkedHashMap<>();
char[] sc = s.toCharArray();
for(char c : sc)
dic.put(c, !dic.containsKey(c));
for(Map.Entry<Character, Boolean> d : dic.entrySet()){
if(d.getValue()) return d.getKey();
}
return ' ';
}
}
// 解决方案(3)
class Solution {
public char firstUniqChar(String s) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
cnt[c - 'a']++;
}
for (int i = 0; i < s.length(); i++) {
if (cnt[s.charAt(i) - 'a'] == 1) {
return s.charAt(i);
}
}
return ' ';
}
}- C++
// 解决方案(1)
class Solution {
public:
char firstUniqChar(string s) {
unordered_map<char, bool> dic;
for(char c : s)
dic[c] = dic.find(c) == dic.end();
for(char c : s)
if(dic[c]) return c;
return ' ';
}
};
// 解决方案(2)
class Solution {
public:
char firstUniqChar(string s) {
vector<char> keys;
unordered_map<char, bool> dic;
for(char c : s) {
if(dic.find(c) == dic.end())
keys.push_back(c);
dic[c] = dic.find(c) == dic.end();
}
for(char c : keys) {
if(dic[c]) return c;
}
return ' ';
}
};边栏推荐
猜你喜欢

Oracle-11gR2默认的系统JOB

v-bind指令的详细介绍

2022 safety officer-c certificate special operation certificate examination question bank and answers

Introduction to official account

IntelliJ IDEA 关联数据库

C#简单调用FMU ,进行仿真计算

Vs2015 use dumpbin to view the exported function symbols of the library

5 运算符、表达式和语句

【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分

【592. 分数加减运算】
随机推荐
10. Learn MySQL like clause
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
5 operators, expressions, and statements
GBase 8a如何使用使用预处理快速插入数据?
Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit
快速上手Flask(一) 认识框架Flask、项目结构、开发环境
Implementation principle of golang synergy
LeetCode_406_根据身高重建队列
376. 摆动序列【贪心、动态规划------】
技术分享| 快对讲综合调度系统
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
完善的交叉编译环境记录 peta 生成的shell 脚本
【leetcode周赛总结】LeetCode第 83场双周赛(7.23)
【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
MQTT. JS introductory tutorial: learning notes
IntelliJ IDEA 关联数据库
股指期货开户的条件和流程
Machine learning (11) -- time series analysis
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始