zoukankan      html  css  js  c++  java
  • 771. Jewels and Stones

    package LeetCode_771
    
    /**
     * 771. Jewels and Stones
     * https://leetcode.com/problems/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:
    1. S and J will consist of letters and have length at most 50.
    2. The characters in J are distinct.
     * */
    
    class Solution {
        /*
        * solution: array, calculate how many character of J in S, Time:O(j+s), Space:O(1)
        * */
        fun numJewelsInStones(J: String, S: String): Int {
            var count = 0
            //because case sensitive
            val mapOfS = IntArray(128)
            for (c in S.toCharArray()) {
                //calculate count of char in S
                mapOfS[c.toInt()]++
            }
            for (c in J) {
                //get the count from S
                count += mapOfS[c.toInt()]
            }
            return count
        }
    }
  • 相关阅读:
    python常见排序算法解析
    分析python日志重复输出问题
    Mysql数据库基础
    横屏竖屏
    禁止iOS的弹性滚动 微信的下拉回弹
    移动性能
    取消双击上滑(针对iso)
    关于微信端 顶部会撑开页面的解决方案
    CSS动画简介
    browser-sync 使用简介
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/14065803.html
Copyright © 2011-2022 走看看