当前位置:网站首页>【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),排序所需
边栏推荐
- What preparations should be made for website server migration?
- RustDesk 搭建一个自己的远程桌面中继服务器
- Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
- A master in the field of software architecture -- Reading Notes of the beauty of Architecture
- (column 22) typical column questions of C language: delete the specified letters in the string.
- 养了只小猫咪
- 【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
- 进程和线程
- Network protocol model
- C language bubble sort
猜你喜欢
实践分享:如何安全快速地从 Centos迁移到openEuler
Station B Liu Erden - linear regression and gradient descent
[email protected] raspberry pie"/>
[email protected] raspberry pie
应用安全系列之三十七:日志注入
ArcGIS application foundation 4 thematic map making
Report on the competition status and investment decision recommendations of Guangxi hospital industry in China from 2022 to 2028
(column 22) typical column questions of C language: delete the specified letters in the string.
HCIA复习
Station B Liu Erden softmx classifier and MNIST implementation -structure 9
28io stream, byte output stream writes multiple bytes
随机推荐
wib3.0 跨越,在跨越(ง •̀_•́)ง
As3013 fire endurance test of cable distribution system
【SQL server速成之路】——身份验证及建立和管理用户账户
什么是独立IP,独立IP主机怎么样?
Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
[experience] install Visio on win11
初识数据库
清除浮动的方式
Pytorch代码注意的细节,容易敲错的地方
华为BFD的配置规范
类和对象(一)this指针详解
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
Query the standard text code corresponding to a work center (s) in the production order
J'ai un chaton.
Anti shake and throttling are easy to understand
Memory and stack related concepts
RustDesk 搭建一个自己的远程桌面中继服务器
大型网站如何选择比较好的云主机服务商?
ArcGIS application foundation 4 thematic map making
27io stream, byte output stream, OutputStream writes data to file