当前位置:网站首页>LeetCode_ String_ Simple_ 387. first unique character in string
LeetCode_ String_ Simple_ 387. first unique character in string
2022-06-21 17:38:00 【I've been up and down in the Jianghu】
1. subject
Given a string s , find Its first non repeating character , And return its index . If it doesn't exist , Then return to -1 .
Example 1:
Input : s = “leetcode”
Output : 0
Example 2:
Input : s = “loveleetcode”
Output : 2
Example 3:
Input : s = “aabb”
Output : -1
Tips :
1 <= s.length <= 105
s Contains only lowercase letters
source : Power button (LeetCode)
link :https://leetcode.cn/problems/first-unique-character-in-a-string
2. Ideas
(1) Hashtable
(2) Array count
3. Code implementation (Java)
// Ideas 1———— Hashtable
class Solution {
public int firstUniqChar(String s) {
//hashmap Used to store strings s Number of occurrences of each character in
Map<Character, Integer> hashmap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
hashmap.put(c, hashmap.getOrDefault(c, 0) + 1);
}
// find hashmap The number of occurrences in is 1 The characters of , And return its subscript
for (int i = 0; i < s.length(); i++) {
if (hashmap.get(s.charAt(i)) == 1) {
return i;
}
}
// There are no unique characters , return -1
return -1;
}
}
// Ideas 2———— Array count
class Solution {
public int firstUniqChar(String s) {
/* character string s Contains only lowercase letters , Therefore, the service length is 26 Array of freq To store each character in s Is the number of times freq[0] Storage a Number of occurrences ,freq[1] Storage b Number of occurrences , And so on . */
int[] freq = new int[26];
int length = s.length();
for (int i = 0; i < length; i++) {
freq[s.charAt(i) - 'a']++;
}
for (int i = 0; i < length; i++) {
// If you find a character that appears only once , Just return its subscript directly
if (freq[s.charAt(i) - 'a'] == 1) {
return i;
}
}
// There are no unique characters , return -1
return -1;
}
}
边栏推荐
- Accélérer le déploiement de l'application Native Cloud et compléter l'authentification de compatibilité entre Yanrong yrcloudfile et Tianyi Cloud
- 蓄电池外壳如何执行EN45545防火试验
- Stack growth direction and memory growth direction
- Vscade tool
- 一些细节
- How can aggressive programmers improve R & D efficiency Live broadcast Preview
- PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
- iframe跨域传值
- Volcano engine + Yanrong yrcloudfile, driving new growth of data storage
- Software test system learning and construction (13) - basic requirements for test engineers of test foundation
猜你喜欢

一招教你通过焱融 SaaS 数据服务平台+ELK 让日志帮你做决策

Common setting modes

全国行政区划

PTA l3-031 thousand hand Guanyin (30 points)

20 pyGame module to make a jumping ball game

类、接口、函数
![[MySQL learning notes 17] sorting out common functions](/img/11/cb4ea6750cc6dca5a39a65de443074.png)
[MySQL learning notes 17] sorting out common functions

「运维有小邓」Active Directory批量修改用户

Common formula of derivative__ Common formulas of indefinite integral

Bm19 looking for peak
随机推荐
第八章 可编程接口芯片及应用【微机原理】
钣金行业MES系统的特色需求
Fragment and activity value transfer
Characteristic requirements of MES system in sheet metal industry
Software test architecture learning and construction (14) - overview of software test and development model of test foundation
The main relations and differences between Poisson sampling and Bernoulli sampling
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
Variables and pointers
data type
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
module.exports指向问题
fs.readFile() 和 fs.writeFile()
应用架构原则
Kotlin annotation declaration and use
FragmentStatePagerAdapter 与FragmentPagerAdapter的区别
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales bull?
Differences between fragmentstatepageradapter and fragmentpageradapter
Volcano engine + Yanrong yrcloudfile, driving new growth of data storage
一些细节
【mysql学习笔记11】排序查询