当前位置:网站首页>【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
2022-07-06 05:48:00 【倒过来是圈圈】
1.字符串中的第一个唯一字符
题解
使用哈希表存储频数,这里哈希表用HashMap
class Solution {
public int firstUniqChar(String s) {
Map<Character,Integer>hashMap=new HashMap<>();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(hashMap.containsKey(c)){
int count=hashMap.get(c);
hashMap.put(c,count+1);
}
else
hashMap.put(c,1);
}
for(int i=0;i<s.length();i++){
if(hashMap.get(s.charAt(i))==1)
return i;
}
return -1;
}
}
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣),其中 Σ \Sigma Σ 是字符集
2.赎金信
题解
使用哈希表存储频数,这里哈希表用数组,运行时间更短
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] cnt=new int[26];
for(int i=0;i<magazine.length();i++){
cnt[magazine.charAt(i)-'a']++;
}
for(int i=0;i<ransomNote.length();i++){
char c=ransomNote.charAt(i);
if(cnt[c-'a']<=0)
return false;
cnt[c-'a']--;
}
return true;
}
}
时间复杂度: O ( m + n ) O(m+n) O(m+n)
空间复杂度: O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣),其中 Σ \Sigma Σ 是字符集
3.有效的字母异位词
题解
哈希表
class Solution {
public boolean isAnagram(String s, String t) {
int m=s.length(),n=t.length();
if(m!=n)
return false;
int[] snt=new int[26];
for(int i=0;i<m;i++)
snt[s.charAt(i)-'a']++;
for(int i=0;i<n;i++){
snt[t.charAt(i)-'a']--;
if(snt[t.charAt(i)-'a']<0)
return false;
}
return true;
}
}
注意:在进阶中提到“如果输入字符串包含 unicode 字符怎么办”?
这种情况就不可以用数组表示哈希表了,而只能用HashMap!
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( ∣ Σ ∣ ) O(∣Σ∣) O(∣Σ∣),其中 Σ \Sigma Σ 是字符集
排序
t 是 s 的异位词等价于「两个字符串排序后相等」。因此我们可以对字符串 s 和 t 分别排序,看排序后的字符串是否相等即可判断。
重点记一下字符串转数组、排序、判等的操作API
class Solution {
public boolean isAnagram(String s, String t) {
int m=s.length(),n=t.length();
if(m!=n)
return false;
char[] str1=s.toCharArray();
char[] str2=t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1,str2);
}
}
时间复杂度: O ( n l o g n ) O(nlogn) O(nlogn),排序所需
空间复杂度: O ( l o g n ) O(logn) O(logn),排序所需
边栏推荐
- C language learning notes (mind map)
- CoDeSys note 2: set coil and reset coil
- 【经验】win11上安装visio
- [JVM] [Chapter 17] [garbage collector]
- Memory and stack related concepts
- Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- LAN communication process in the same network segment
- Implementation of linked list in address book management system
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
猜你喜欢
Yygh-11-timing statistics
C language bubble sort
The usage and difference between strlen and sizeof
CoDeSys note 2: set coil and reset coil
Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
嵌入式面试题(四、常见算法)
J'ai un chaton.
27io stream, byte output stream, OutputStream writes data to file
B站刘二大人-数据集及数据加载 Lecture 8
Practice sharing: how to safely and quickly migrate from CentOS to openeuler
随机推荐
【SQL server速成之路】——身份验证及建立和管理用户账户
H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
【经验】win11上安装visio
网络协议模型
養了只小猫咪
Is it difficult for an information system project manager?
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
CoDeSys note 2: set coil and reset coil
B站刘二大人-多元逻辑回归 Lecture 7
[SQL Server fast track] - authentication and establishment and management of user accounts
27io stream, byte output stream, OutputStream writes data to file
[force buckle]43 String multiplication
First knowledge database
Station B Liu Erden - linear regression and gradient descent
Pytorch代码注意的细节,容易敲错的地方
Mysql database master-slave cluster construction
[email protected]树莓派
Web service connector: Servlet
入侵检测领域数据集总结
大型网站如何选择比较好的云主机服务商?