01 | Jewels and Stones
Question
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:
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
Answer One
class Solution { /**
* @param String $J
* @param String $S
* @return Integer
*/
function numJewelsInStones($J, $S) {
$Js = str_split($J);
$sum = 0;
foreach($Js as $v) {
$sum += substr_count($S, $v);
}
return $sum;
}
}
Answer Two
class Solution { /**
* @param String $J
* @param String $S
* @return Integer
*/
function numJewelsInStones($J, $S) {
// $Js = str_split($J);
$Ss = str_split($S);
$sum = 0;
$res = array_count_values($Ss);
foreach($res as $k => $v) {
if(strpos($J, $k) !== false) {
$sum += $v;
}
}
return $sum;
}
}
PHP 原生函数大法好...
01 | Jewels and Stones的更多相关文章
- LeetCode --> 771. Jewels and Stones
Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...
- Leetcode#771.Jewels and Stones(宝石与石头)
题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- 【Leetcode_easy】771. Jewels and Stones
problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...
- 771. Jewels and Stones - LeetCode
Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...
- 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [Swift]LeetCode771. 宝石与石头 | Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- [LeetCode] Jewels and Stones 珠宝和石头
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
随机推荐
- linux socket详解
1 linux socket编程的固定模式 server端,bind.listen.accept client端,connect client端和server端之间的一次通信: client端,wri ...
- tornado之异步web服务一
大部分Web应用(包括我们之前的例子)都是阻塞性质的,也就是说当一个请求被处理时,这个进程就会被挂起直至请求完成.在大多数情况下,Tornado处理的Web请求完成得足够快使得这个问题并不需要被关注. ...
- WSDL文档深入分析
借助jdk的wsimort.exe工具生成客户端代码 格式:wsimport -keep url //url为wsdl文件的路径 直接生成客户端代码会抛异常, 无法生成客户端代码, 解决办法: 将 ...
- SDUT OJ 进制转换
进制转换 Time Limit: 1000MS Memory limit: 65536K 题目描述 输入一个十进制数N,将它转换成R进制数输出. 输入 输入数据包含多个测试实例,每个测试实例包含两个整 ...
- avf_showspectrum.c:112: undefined reference to `av_rdft_end
下面还有一堆错,是由于ffmpeg库没编好,重新编好即可
- Partition算法及Partition算法用于快速排序
JavaScript简单方便,所以用JavaScript实现,可以在Chrome控制台下观察运行结果.主要实现Partition算法,比如输入为 var array = [4, 2, 1, 3, ...
- Json 不同语言的使用
Javascript: 1.使用eval var parse_json_by_eval = function(str){ return eval('('+str+')'); } var value = ...
- swprintf has been changed to conform with the ISO C standard, adding an extra character count parameter.
'swprintf': swprintf has been changed to conform with the ISO C standard, adding an extra character ...
- ceph之ceph-client安装
1.安装ceph-client ceph-deploy install ceph-client 2.创建块设备 [root@mon1 ~]# rbd create test1 --image-form ...
- Ubuntu 图形处理软件
sudo add-apt-repository ppa:dhor/myway sudo apt-get update sudo apt-get install photivo