Love丶FFC's Blog

LintCode_easy:珠宝和石头

2019-08-30 12:36:49
阅读:582   •   评论:8
标签:,

描述

给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝.

J中的字母一定不同,JS中的字符都是字母。 字母区分大小写,因此"a""A"是不同的类型.

SJ由字母组成且长度最大为50. J中字符各不相同.

样例

样例 1:

输入: J = "aA", S = "aAAbbbb"
输出: 3

样例 2:

输入: J = "z", S = "ZZ"
输出: 0

编程语言:Java

解题思想:

1.使用hashset来存储珠宝,使用String来存储石头。

2.使用hashset中的contains(包含)方法进行判断。

时间复杂度:O(n)

IDE代码如下:

  1. package LintCode_Easy;
  2.  
  3. import java.util.HashSet;
  4.  
  5. public class _1038 {
  6.  
  7. public static void main(String[] args) {
  8. String J = "huirKISxBWFCZPnzwN";
  9. String S = "NbTpgRdJTsCuCjG";
  10. HashSet<String> hashSet = new HashSet<String>();
  11. int total=0;
  12. for(int i=0;i<J.length();i++)
  13. hashSet.add(J.charAt(i)+"");
  14. for(int j=0;j<S.length();j++)
  15. if(hashSet.contains(S.charAt(j)+""))
  16. total++;
  17. System.out.println(total);
  18. }
  19.  
  20. }
  21.  

LintCode编辑器代码如下:

评论板

共有 8 条评论

  1. fruinly

    п»їcialis Then I removed it for no reason

  2. engarma

    clomipramine voltaren crema efectos secundarios Other nominees include U buy cialis viagra In the context of this very dynamic situation, detecting small differences in potassium is impossible

  3. Preliaf

    Also, a coworker tried for 18 months and then got pregnant first try after preseed and 2 more coworkers got pregnant first try with it cialis It was truly a dithgusting thpectacle

  4. Preliaf

    levitra sans ordonnance pour Hi my name is Amanda my husband and i have been trying to have a baby for 3 years and nothing i went to the doctor and he gave me clomid 50mg i took it the first month and nothing the second month i took it and i am now 5 weeks pregnant

  5. fibPBqK

    Jose, Christopher S buy cialis online uk 0260 Comorbidity Hypertension 3

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:510556