鱼C论坛

 找回密码
 立即注册
查看: 1610|回复: 0

[学习笔记] leetcode 771. Jewels and Stones

[复制链接]
发表于 2019-9-1 08:24:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. 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.

  2. 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".

  3. Example 1:

  4. Input: J = "aA", S = "aAAbbbb"
  5. Output: 3
  6. Example 2:

  7. Input: J = "z", S = "ZZ"
  8. Output: 0
  9. Note:

  10. S and J will consist of letters and have length at most 50.
  11. The characters in J are distinct.
复制代码

  1. class Solution {
  2.     public int numJewelsInStones(String J, String S) {
  3.         
  4.         if(S.length() == 0) return 0;
  5.         
  6.         char[] charArray = S.toCharArray();
  7.         int count = 0;
  8.         
  9.         for(char i : charArray){
  10.             
  11.             if(J.indexOf(i) != -1){
  12.                
  13.                 count++;
  14.             }
  15.         }
  16.         
  17.         return count;
  18.     }
  19. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 10:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表