zoukankan      html  css  js  c++  java
  • lintcode入门篇十六

    1038. 珠宝和石头

    中文English

    给定字符串J代表是珠宝的石头类型,而S代表你拥有的石头.S中的每个字符都是你拥有的一个石头. 你想知道你的石头有多少是珠宝.

    J中的字母一定不同,JS中的字符都是字母。 字母区分大小写,因此"a""A"是不同的类型.

    样例

    样例 1:

    输入: J = "aA", S = "aAAbbbb"
    输出: 3
    

    样例 2:

    输入: J = "z", S = "ZZ"
    输出: 0
    

    注意事项

    • SJ由字母组成且长度最大为50.
    • J中字符各不相同.
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param J: the types of stones that are jewels
        @param S: representing the stones you have
        @return: how many of the stones you have are also jewels
        """
        '''
        大致思路:
        1.J中代表的是珠宝的类型,初始化J_dic = [],将J中各个字符append到J_dic里面。
        2.初始化count = 0,循环S,如果当前字符在J_dic里面的话,说明是珠宝,count += 1。返回count
        '''
        def numJewelsInStones(self,J,S):
            J_dic = [i for i in J]
            
            count = 0 
            for num in S:
                if num in J_dic:
                    count += 1
            return count

    1053. 至少是其他数字两倍的最大数

    中文English

    在一个给定的数组nums中,总是存在一个最大元素 。

    查找数组中的最大元素是否至少是数组中每个其他数字的两倍。

    如果是,则返回最大元素的索引,否则返回-1。

    样例

    示例 1:
    
    输入: nums = [3, 6, 1, 0]
    输出: 1
    解释: 6是最大的整数, 对于数组中的其他整数,
    6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.
    
    示例 2:
    
    输入: nums = [1, 2, 3, 4]
    输出: -1
    解释: 4没有超过3的两倍大, 所以我们返回 -1.
    

    注意事项

    • nums 的长度范围在[1, 50].
    • 每个 nums[i] 的整数范围在 [0, 99].
    输入测试数据 (每行一个参数)如何理解测试数据?(lintcode不能remove用法)
    class Solution:
        '''
        大致思路:
        1.首先得到最大值,然后移除掉(总是存在一个最大元素)
        2.取出第二最大值,然后判断最开始的最大值是否大于第二最大值的两倍,如果是返回最大元素的索引,否则-1
        '''
        def dominantIndex(self,nums):
            max_num,index = max(nums),nums.index(max(nums))
            for i,value in enumerate(nums):
                if value == max_num:
                    continue
                elif value*2 > max_num:
                    return -1
            return index

    1056. 请找出大于目标的最小字母

    中文English

    给定一串只含有小写形式的、排序过的 letters,并且给定一个目标字母 target ,请找出在给定字母串中,大于目标字母的最小的那一个字母。

    在本题中,字母是绕回编址的(即“z”后一位重新变为“a”)。比如说,如果target = 'z',而给定字母串为letters = ['a', 'b'],那么答案为“a”。

    样例

    样例 1:

    输入:
    letters = ["c", "f", "j"]
    target = "a"
    输出: "c"
    

    ** 样例 2:**

    输入:
    letters = ["c", "f", "j"]
    target = "c"
    输出: "f"
    

    样例 3:

    输入:
    letters = ["c", "f", "j"]
    target = "d"
    输出: "f"
    

    样例 4:

    输入:
    letters = ["c", "f", "j"]
    target = "g"
    输出: "j"
    

    样例 5:

    输入:
    letters = ["c", "f", "j"]
    target = "j"
    输出: "c"
    

    样例 6:

    输入:
    letters = ["c", "f", "j"]
    target = "k"
    输出: "c"
    

    注意事项

    1.letters 长度范围为 [2, 10000].
    2.letters 只含有小写字母,并且其中至少含有两个互不相同的字母。
    3.target 也是一个小写字母。

    class Solution:
        """
        @param letters: a list of sorted characters
        @param target: a target letter
        @return: the smallest element in the list that is larger than the given target
        """
        '''
        大致思路:
        1.如果是存在ascii值大于目标值的话,则返回。如果是不存在的话,则按照最小值来看
        '''
        def nextGreatestLetter(self,letters,target):
            for column in letters:
                if column > target:
                    return column
            return min(column)
  • 相关阅读:
    Linux系统调用和库函数调用的区别
    Linux驱动的两种加载方式过程分析
    activeMQ的两个默认端口8161和61616的区别
    ActiveMQ安装报错Wrapped Stopped解决办法
    Spring Security 5.x兼容多种密码加密方式
    Spring Security 5中 PasswordEncoder的使用
    There is no PasswordEncoder mapped for the id "null"的解决办法
    spring security 5 There is no PasswordEncoder mapped for the id "null" 错误
    MySQL——修改视图
    MySQL视图 definer & invoker 权限
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/12604028.html
Copyright © 2011-2022 走看看