首页 > 编程语言 > 详细

LeetCode-771 Jewels and Stones Solution with Java

时间:2020-03-02 12:51:09      阅读:44      评论:0      收藏:0      [点我收藏+]

1. Description:

技术分享图片

Notes:技术分享图片

2. Examples: 技术分享图片

3.Solutions:

Version 1: hashmap

 1  /**
 2      * Created by sheepcore on 2020-03-02
 3      * @param J
 4      * @param S
 5      * @return the numbers of jewels in stones
 6      */
 7     public int numJewelsInStones(String J, String S) {
 8         Map<Character, Integer> jewels = new HashMap<>();
 9         int check;
10         for (char c : J.toCharArray())
11             if (!jewels.containsKey(c))
12                 jewels.put(c, 0);
13 
14         for (char c: S.toCharArray())
15             if (jewels.containsKey(c))
16                 jewels.put(c, jewels.get(c) + 1);
17 
18         Collection<Integer> values = jewels.values();
19         int sum = 0;
20         for(int val : values) {
21             sum += val;
22         }
23         return sum;
24     }

Version 1:  for-loop

1 public int numJewelsInStones(String J, String S) {
2         int num = 0;
3         for (int i = 0; i < S.length(); i++) {
4             if (J.contains(S.charAt(i) + ""))
5                 num++;
6         }
7         return num;
8     }

LeetCode-771 Jewels and Stones Solution with Java

原文:https://www.cnblogs.com/sheepcore/p/12394816.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!