letCode(771 Jewels and Stones )
问题描述:
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
};
最新文章
- delphi中的各种文件类型介绍【转】
- 猿题库 iOS 客户端架构设计
- springmvc----struts2比较
- Windows Phone8.1 SDK中的新控件
- DzzOffice管理员登陆方法和管理员应用介绍
- ActiveXObject对象详解
- 多线程与Socket编程
- 查看ORACLE事务隔离级别方法(转)
- win32 sdk 列表视图控件绘制
- Unable to chmod /system/build.prop.: Read-only file system
- Spring依赖注入的简化配置
- Leetcode题解(29)
- IOS8,IOS8.1等系统出现锁屏状态下WIFI断开问题的解决办法!
- Maven项目执行java入口main方法
- Groovy 设计模式 -- 装饰器模式
- Cannot read property &#39;properties&#39; of undefined
- jdango 使用oss存储
- P3763 [TJOI2017]DNA
- Unix操作系统监控详解(一)
- php管理nginx虚拟主机shell脚本
热门文章
- 字符串转换整数 (atoi) C++实现 java实现 leetcode系列(八)
- OKHttp使用简介
- BZOJ 2005 容斥原理
- (转载)自定义View——弹性滑动
- 51nod 1102 面积最大的矩形 &;&; 新疆大学OJ 1387: B.HUAWEI&#39;s billboard 【单调栈】+【拼凑段】(o(n) 或 o(nlog(n))
- 向Vue实例混入plusready
- swift学习笔记 - swift中常用关键字
- ZBrush软件特性之Material
- DataTable相关操作,筛选,取前N条数据,去重复行,获取指定列数据
- 前端路由的两种模式:hash(#)模式和history模式(转)