当前位置:网站首页>代码随想录笔记_哈希_383赎金信
代码随想录笔记_哈希_383赎金信
2022-08-02 03:14:00 【Erik_Won】
代码随想录笔记_哈希表
代码随想录二刷笔记记录
LC 383.赎金信
题目
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1:
输入:ransomNote = “a”, magazine = “b”
输出:false
示例 2:
输入:ransomNote = “aa”, magazine = “ab”
输出:false
示例 3:
输入:ransomNote = “aa”, magazine = “aab”
输出:true
思路分析
step1: 声明一个长度为26的字典,记录magazine里字母出现的次数
step2: 用ransomNote去验证这个字典是否包含了ransomNote中的所有字母。
代码实现
完整代码实现
public boolean canConstruct(String ransomNote, String magazine) {
int[] cnt = new int[26];
char[] ca1 = ransomNote.toCharArray();
for (char c : ca1) {
cnt[c - 'a']++;
}
char[] ca2 = magazine.toCharArray();
for (char c : ca2) {
cnt[c-'a']--;
}
for (int i : cnt) {
if (i > 0){
return false;
}
}
return true;
}
边栏推荐
- JSP Webshell 免杀
- 7-40 奥运排行榜 (25 分)多项排序
- 基于可逆网络的单一图像超分辨率
- 精益思想如何加速企业的全局价值流动?
- 自定义mvc框架复习(crud)
- Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
- 5. Hezhou Air32F103_LCD_key
- (Reposted) The relationship between hashcode and equals
- LeetCode:1161. 最大层内元素和【BFS层序遍历】
- [LeetCode] 83. Delete duplicate elements in the sorted list
猜你喜欢

深度学习:目标检测入门知识

关于#sql#的问题:该怎么写sql语句,

Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers

MySQL8.0.26安装配置教程(windows 64位)

JDBC的入门使用

Using WebShell to get Shell Skills

centos安装mysql8

DOM破坏及复现实验

Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)

AntV X6制作画板工具(图形,线段,图片上传)
随机推荐
Flexbox flex property
MySQL8.0.26 installation and configuration tutorial (windows 64-bit)
第七周复习
基于分布式随机森林的火电厂燃烧系统设备建模方法
WebShell特征值汇总与检测工具
支付通道对接常见的问题有哪些?
STM32——LCD—TFTLCD原理与配置介绍
ASP WebShell backdoor script and anti-kill
WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用
OD-Model【4】:SSD
WebShell Feature Value Summary and Detection Tool
7-40 奥运排行榜 (25 分)多项排序
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
SSM网上商城购物系统(前台+后台)
深度自编码网络的集成学习ICPS入侵检测模型
ROS2自学笔记:launch文件完整编写流程
OD-Model [4]: SSD
(转帖)HashCode总结(1)
# ODS及DWD层自动化构建##, 220731,
MySQL8 -- use msi (graphical user interface) under Windows installation method