当前位置:网站首页>2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
2022-07-05 20:40:00 【菜菜的大数据开发之路】
剑指 Offer 50. 第一个只出现一次的字符
[案例需求]
[思路分析一, 两次遍历]
[代码实现]
class Solution {
public char firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
return s.charAt(i);
}
}
return ' ';
}
}
[思路分析二, 利用API]
class Solution {
public char firstUniqChar(String s) {
for(int i = 0; i< s.length(); i++){
char value = s.charAt(i);
if(s.indexOf(value)==s.lastIndexOf(value)){
return value;
}
}
return ' ';
}
}
边栏推荐
- Classic implementation of the basic method of intelligent home of Internet of things
- Point cloud file Dat file read save
- Use of form text box (II) input filtering (synthetic event)
- 1. Strengthen learning basic knowledge points
- Y57. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (30)
- mongodb基操的练习
- Abnova丨荧光染料 620-M 链霉亲和素方案
- CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
- Schema和Model
- 解析创客教育的知识迁移和分享精神
猜你喜欢
小程序页面导航
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Duchefa p1001 plant agar Chinese and English instructions
[Yugong series] go teaching course in July 2022 004 go code Notes
Applet event binding
Classic implementation method of Hongmeng system controlling LED
Abnova DNA marker high quality control test program
National Eye Care Education Conference, 2022 the Fourth Beijing International Youth eye health industry exhibition
Duchefa MS medium contains vitamin instructions
随机推荐
Duchefa p1001 plant agar Chinese and English instructions
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Make Jar, Not War
欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
IC popular science article: those things about Eco
Analysis of steam education mode under the integration of five Education
小程序全局配置
Nprogress plug-in progress bar
[Yugong series] go teaching course in July 2022 004 go code Notes
CTF reverse Foundation
ProSci LAG-3 重组蛋白说明书
Leetcode (347) - top k high frequency elements
Informatics Olympiad 1338: [example 3-3] hospital setting | Luogu p1364 hospital setting
Enter the parallel world
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Ros2 topic [01]: installing ros2 on win10
CVPR 2022 | common 3D damage and data enhancement
解析五育融合之下的steam教育模式
小程序事件绑定
mongodb基操的练习