SQLServer Decimal数据类型怎么赋值
268
2022-12-01
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.
思路: 记录J的信息,遍历S,采用Hashmap的思路,每次查询O(1),总时间复杂度为O(n)
class Solution { public int numJewelsInStones(String J, String S) { int[] count = new int[64]; for (char c : J.toCharArray()) { count[c - 'A']++; } int ans = 0; for (char c : S.toCharArray()) { if (count[c - 'A'] >= 1) ans ++; } return ans; }}
/** * @param {string} J * @param {string} S * @return {number} */var numJewelsInStones = function(J, S) { const jLen = J.length const sLen = S.length if (!jLen || !sLen) { return 0 } let count = 0 const map = new Map() for (let i = 0; i < jLen; i++) { map.set(J[i], true) } for (let j = 0; j < sLen; j++) { if (map.has(S[j])) { count ++ } } return count};
/** * @param {string} J * @param {string} S * @return {number} */var numJewelsInStones = function(J, S) { let sarr = S.split(""); return sarr.filter(item=>J.includes(item)).length};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~