当前位置:网站首页>【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),排序所需
边栏推荐
- 移植InfoNES到STM32
- OSPF configuration command of Huawei equipment
- (column 22) typical column questions of C language: delete the specified letters in the string.
- 【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
- [paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning
- H3C firewall rbm+vrrp networking configuration
- Station B, Mr. Liu Er - multiple logistic regression, structure 7
- Processes and threads
- Station B, Master Liu Er - dataset and data loading
- PDK工艺库安装-CSMC
猜你喜欢
![[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS](/img/ed/8d112054f31bd7e593050d1278b9f1.jpg)
[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS

Station B Liu Erden softmx classifier and MNIST implementation -structure 9

B站刘二大人-Softmx分类器及MNIST实现-Lecture 9

What is independent IP and how about independent IP host?

Practice sharing: how to safely and quickly migrate from CentOS to openeuler
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶

Redis message queue

Installation de la Bibliothèque de processus PDK - csmc

局域网同一个网段通信过程

Memory and stack related concepts
随机推荐
【SQL server速成之路】——身份验证及建立和管理用户账户
My 2021
How to download GB files from Google cloud hard disk
Station B Liu Erden - linear regression and gradient descent
Mysql database master-slave cluster construction
网站进行服务器迁移前应做好哪些准备?
Huawei BFD configuration specification
Web服务连接器:Servlet
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
Pay attention to the details of pytoch code, and it is easy to make mistakes
Network protocol model
嵌入式面试题(一:进程与线程)
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
OSPF configuration command of Huawei equipment
授予渔,从0开始搭建一个自己想要的网页
Embedded interview questions (IV. common algorithms)
Yygh-11-timing statistics
Grant Yu, build a web page you want from 0
[happy Spring Festival] if you feel happy, dance
进程和线程