【题目】

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;
}
}

【参考】

最新文章

  1. 一个 IT 青年北漂四年的感悟
  2. winrar激活
  3. mysql 字符串拼接函数CANCAT()与GROUP_CANCAT()
  4. os即时通讯客户端开发之-mac上安装MySQL
  5. install pip3 for python 3.x
  6. Windows7上FTP服务器建立
  7. Intellij IDEA 2016 mybatis 生成 mapper
  8. 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法
  9. 《深入理解JAVA虚拟机》笔记1
  10. 解决使用mybatis分页插件PageHelper的一个报错问题
  11. 基于kettle的简单HTTP接口监控
  12. IAR软件的使用
  13. Matplotlib学习---用matplotlib画阶梯图(step plot)
  14. Python网络编程-Socket简单通信(及python实现远程文件发送)
  15. 使用yaml+groovy实现Java代码可配置化
  16. python中的异常处理tryexcept
  17. ThinkPHP 5 insertall 只插入最后一条数据的问题
  18. Jmeter中Websocket协议支持包的使用(转)
  19. Linux性能分析 vmstat基本语法
  20. Codeforces Round #360 (Div. 2) A. Opponents 水题

热门文章

  1. linux下git服务器安装
  2. day01 格式化输出和while循环的两个小练习
  3. http返回状态码含义
  4. CSS——对position定位和margin-top的理解
  5. oracle(2)
  6. ant常用的内置 task转自https://www.cnblogs.com/baicj/archive/2015/12/21/5063608.html
  7. Linux下调试.Net core(1):lldb的安装
  8. SSH框架整合,css、js会被过滤器过滤掉
  9. HADOOP HA 踩坑 - 所有 namenode 都是standby
  10. 启动xampp出错,Port 80 in use by "Unable to open process" with PID 4!