• 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 Sis 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

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    toCharArray(无参)可以用来打散字符串,很好用

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    hashset可以不用指定储类型, 配合count就能统计数量了

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public int numJewelsInStones(String J, String S) {
            //cc
            if (J.length() == 0 || S.length() == 0) {
                return 0;
            }
            
            //ini set, res
            Set set = new HashSet();
            int res = 0;
            
            //for loop,count
            for (char j : J.toCharArray()) {
                set.add(j);
            }
            for (char s : S.toCharArray()) {
                if (set.contains(s)) res++;
            }
            
            //return res
            return res;
        }
    }
    View Code
  • 相关阅读:
    hdu 1203 I NEED A OFFER! (01背包)
    链式前向星
    hdu 3790 最短路问题 (spfa练手)
    hdu 2859 Phalanx (最大对称子矩阵)
    hdu 1160 FatMouse's Speed (最长上升子序列+打印路径)
    hdu 5289 Assignment (ST+二分)
    vue环境配置 vue-cli脚手架
    1、初始Java应用程序
    HTML5之Canvas画正方形
    布尔表达式
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8915853.html
Copyright © 2020-2023  润新知