当前位置:网站首页>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 ' ';
}
};边栏推荐
- opencv4.60版本安装和配置
- OpenShift 4 - 使用 VerticalPodAutoscaler 优化应用资源 Request 和 Limit
- Solution and implementation of APP accelerating reading and displaying IPFs pictures
- Magic brace- [group theory] [Burnside lemma] [matrix fast power]
- Rgb-t tracking: [multimodal fusion] visible thermal UAV tracking: a large scale benchmark and new baseline
- [592. Fraction addition and subtraction]
- 信息学奥赛一本通 1617:转圈游戏 | 1875:【13NOIP提高组】转圈游戏 | 洛谷 P1965 [NOIP2013 提高组] 转圈游戏
- C simply call FMU for simulation calculation
- 公众号简介
- IntelliJ IDEA 关联数据库
猜你喜欢

JSON file storage

Recommend an artifact to get rid of the entanglement of variable names and a method to modify file names in batches
![【解决】ERROR in [eslint] ESLint is not a constructor](/img/58/2ce1243d0085462af3ba6d3da0817d.png)
【解决】ERROR in [eslint] ESLint is not a constructor

AMQ streams (1) of openshift 4 - multiple consumers receive data from partition

12 common design ideas of design for failure

【一花一世界-郑一教授-繁简之道】可解释神经网络

Realize batch data enhancement | use of keras imagedatagenerator

v-bind指令的详细介绍

Promise学习笔记

From development to testing: I started from scratch and worked for six years of automated testing
随机推荐
MDM data quality application description
An entry artifact tensorflowplayground
Implementation principle of golang synergy
ES6 变量的解构赋值
Oracle-11gR2默认的系统JOB
Design for failure常见的12种设计思想
【592. 分数加减运算】
【leetcode周赛总结】LeetCode第 83场双周赛(7.23)
1.5 merge\rebase\revert\stash\branch
The cooperation between starfish OS and metabell is just the beginning
ES6 let and Const
ES6 let与const
Send a message to the background when closing the page
Magic brace- [group theory] [Burnside lemma] [matrix fast power]
[package deployment]
7 C控制语句:分支和跳转
【多线程】long和double的非原子性协定
App加速读取显示IPFS图片的解决方案和实现
【JVM】JVM表示浮点数
The new mode of 3D panoramic display has become the key to breaking the game