[Leetcode 771]宝石和石子 Jewels and Stones HashSet简单应用
2024-10-14 09:33:10
【题目】
You're given strings J
representing the types of stones that are jewels, and S
representing the stones you have. Each character in S
is a type of stone you have. You want to know how many of the stones you have are also jewels.
The letters in J
are guaranteed distinct, and all characters in J
and S
are letters. Letters are case sensitive, so "a"
is considered a different type of stone from "A"
.
两个集合A、B。A中是石头,B中是宝石。通过比对返回石头A中是宝石的个数。
【思路】
string转换外char数组,把原始数据存在HashSet中,通过比较键值,是否contains了B中的元素来控制ans++
【代码】
class Solution {
public int numJewelsInStones(String J, String S) {
char[] js=J.toCharArray();
char[] ss=S.toCharArray();
Set data=new HashSet();
int ans=;
//加入数据
for(char j:js){
data.add(j);}
//遍历是否相等
for(char s:ss){
if(data.contains(s))
ans++;
}
return ans;
}
}
【参考】
最新文章
- 一个 IT 青年北漂四年的感悟
- winrar激活
- mysql 字符串拼接函数CANCAT()与GROUP_CANCAT()
- os即时通讯客户端开发之-mac上安装MySQL
- install pip3 for python 3.x
- Windows7上FTP服务器建立
- Intellij IDEA 2016 mybatis 生成 mapper
- 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法
- 《深入理解JAVA虚拟机》笔记1
- 解决使用mybatis分页插件PageHelper的一个报错问题
- 基于kettle的简单HTTP接口监控
- IAR软件的使用
- Matplotlib学习---用matplotlib画阶梯图(step plot)
- Python网络编程-Socket简单通信(及python实现远程文件发送)
- 使用yaml+groovy实现Java代码可配置化
- python中的异常处理tryexcept
- ThinkPHP 5 insertall 只插入最后一条数据的问题
- Jmeter中Websocket协议支持包的使用(转)
- Linux性能分析 vmstat基本语法
- Codeforces Round #360 (Div. 2) A. Opponents 水题
热门文章
- linux下git服务器安装
- day01 格式化输出和while循环的两个小练习
- http返回状态码含义
- CSS——对position定位和margin-top的理解
- oracle(2)
- ant常用的内置 task转自https://www.cnblogs.com/baicj/archive/2015/12/21/5063608.html
- Linux下调试.Net core(1):lldb的安装
- SSH框架整合,css、js会被过滤器过滤掉
- HADOOP HA 踩坑 - 所有 namenode 都是standby
- 启动xampp出错,Port 80 in use by ";Unable to open process"; with PID 4!