当前位置:网站首页>leetcode刷题:哈希表06 (赎金信)
leetcode刷题:哈希表06 (赎金信)
2022-06-23 17:23:00 【涛涛英语学不进去】
383. 赎金信
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)
/** * 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 * 如果可以,返回 true ;否则返回 false 。 * magazine 中的每个字符只能在 ransomNote 中使用一次。 * <p> * 这个题不就是问两个是不是字母异位词嘛。 * * @param ransomNote * @param magazine * @return */
public static boolean canConstruct(String ransomNote, String magazine) {
final byte[] magazineBytes = magazine.getBytes();
HashMap<Byte, Integer> map = new HashMap<>();
for (byte i : magazineBytes) {
//如果不包含,添加,包含则数量+1
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
final byte[] ransomNoteBytes = ransomNote.getBytes();
int value;
for (byte i : ransomNoteBytes) {
//如果不包含,则false
if (!map.containsKey(i)) {
return false;
} else {
//如果这个字母只剩最后一次使用机会了,则删除这个键
value = map.get(i);
if (value == 1) {
map.remove(i);
} else {
map.put(i, value - 1);
}
if (map.size() == 0) {
return true;
}
}
}
return true;
}

很像字母异位同那题,但不全是,只要右边完全包含左边即可,需要计数。
终极思路:使用数组做,看起来也简洁很多,虽然思路一样的。
/** * 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 * 如果可以,返回 true ;否则返回 false 。 * magazine 中的每个字符只能在 ransomNote 中使用一次。 * <p> * 这个题不就是问两个是不是字母异位词嘛。 * * @param ransomNote * @param magazine * @return */
public static boolean canConstruct(String ransomNote, String magazine) {
final byte[] magazineBytes = magazine.getBytes();
int[] result = new int[26];
for (byte i : magazineBytes) {
result[i - 'a'] += 1;
}
final byte[] ransomNoteBytes = ransomNote.getBytes();
for (byte i : ransomNoteBytes) {
if (result[i - 'a'] <= 0) {
return false;
}else {
result[i - 'a'] -= 1;
}
}
return true;
}

边栏推荐
- How to use JSON data format
- 芯片原厂必学技术之理论篇(4-1)时钟技术、复位技术
- 【剑指Offer】45. 把数组排成最小的数
- Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation
- 暂停更新公告—行走的皮卡丘
- Paper reading (48):a Library of optimization algorithms for organizational design
- [failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
- QML类型:Loader
- Programmers are very useful ten tool websites, which are worth collecting
- Call face recognition exception
猜你喜欢

【win10 VS2019 opencv4.6 配置参考】

torch学习(一):环境配置

计算机学院改考后,网络空间安全学院也改考了!南京理工大学计算机考研

TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!

【故障公告】取代 memcached 的 redis 出现问题造成网站故障

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...

暂停更新公告—行走的皮卡丘

深入理解 padding

Video anomaly detection data set (shanghaitech)

Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet
随机推荐
. Net cloud native architect training camp (responsibility chain mode) -- learning notes
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...
leetcode刷题:哈希表07 (三数之和)
console. Log() is an asynchronous operation???
Landing of global organizational structure control
Also using copy and paste to create test data, try the data assistant!
视频异常检测数据集 (ShanghaiTech)
7、VLAN-Trunk
Ner's past, present and future Overview - Future
Paper reading (54):deepfool: a simple and accurate method to four deep neural networks
VNC Viewer方式的远程连接树莓派
Baidu AI Cloud product upgrade Observatory in May
[failure announcement] there is a problem with the redis that replaces memcached, causing the website to fail
【Wwise】Wwise嵌入Unity后打包出现没有声音问题
客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
深入理解 padding
实现领域驱动设计 - 使用ABP框架 - 通用准则
Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based
微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到