问题描述:

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

Example 1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

Note:

 S and J will consist of letters and have length at most 50.

The characters in J are distinct

解决方案:

  1 数组

    var numJewelsInStones = function(J, S) {
    var arr1=J.split("");
    var arr2=S.split("");
    var count=0;
    for(var i=0;i<arr2.length;i++){
    for(var j=0;j<arr1.length;j++){
    if(arr2[i]==arr1[j]){
    count++
    }
    }
    }
    return count
    };

2 字符串方法

    

    var numJewelsInStones = function(J, S) {
    var count=0;
    for(var i=0;i<S.length;i++){
    if(J.indexOf(S.charAt(i))!==-1){
    count++
    }
    }
    return count
    };

最新文章

  1. delphi中的各种文件类型介绍【转】
  2. 猿题库 iOS 客户端架构设计
  3. springmvc----struts2比较
  4. Windows Phone8.1 SDK中的新控件
  5. DzzOffice管理员登陆方法和管理员应用介绍
  6. ActiveXObject对象详解
  7. 多线程与Socket编程
  8. 查看ORACLE事务隔离级别方法(转)
  9. win32 sdk 列表视图控件绘制
  10. Unable to chmod /system/build.prop.: Read-only file system
  11. Spring依赖注入的简化配置
  12. Leetcode题解(29)
  13. IOS8,IOS8.1等系统出现锁屏状态下WIFI断开问题的解决办法!
  14. Maven项目执行java入口main方法
  15. Groovy 设计模式 -- 装饰器模式
  16. Cannot read property &#39;properties&#39; of undefined
  17. jdango 使用oss存储
  18. P3763 [TJOI2017]DNA
  19. Unix操作系统监控详解(一)
  20. php管理nginx虚拟主机shell脚本

热门文章

  1. 字符串转换整数 (atoi) C++实现 java实现 leetcode系列(八)
  2. OKHttp使用简介
  3. BZOJ 2005 容斥原理
  4. (转载)自定义View——弹性滑动
  5. 51nod 1102 面积最大的矩形 &amp;&amp; 新疆大学OJ 1387: B.HUAWEI&#39;s billboard 【单调栈】+【拼凑段】(o(n) 或 o(nlog(n))
  6. 向Vue实例混入plusready
  7. swift学习笔记 - swift中常用关键字
  8. ZBrush软件特性之Material
  9. DataTable相关操作,筛选,取前N条数据,去重复行,获取指定列数据
  10. 前端路由的两种模式:hash(#)模式和history模式(转)