【leetcode】1394. Find Lucky Integer in an Array

题目如下:

Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value.

Return a lucky integer in the array. If there are multiple lucky integers return the largest of them. If there is no lucky integer return -1.

Example 1:

Input: arr = [2,2,3,4]
Output: 2
Explanation: The only lucky number in the array is 2 because frequency[2] == 2.

Example 2:

Input: arr = [1,2,2,3,3,3]
Output: 3
Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.

Example 3:

Input: arr = [2,2,2,3,3]
Output: -1
Explanation: There are no lucky numbers in the array.

Example 4:

Input: arr = [5]
Output: -1

Example 5:

Input: arr = [7,7,7,7,7,7,7]
Output: 7

Constraints:

  • 1 <= arr.length <= 500
  • 1 <= arr[i] <= 500

解题思路:非常简单的题目。

代码如下:

class Solution(object):
    def findLucky(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        val = [0] * 501
        for i in arr:
            val[i] += 1
        for inx in range(len(val)-1,0,-1):
            if inx == val[inx]:
                return inx
        return -1

原文地址:https://www.cnblogs.com/seyjs/p/12631461.html

时间: 2024-11-09 08:01:01

【leetcode】1394. Find Lucky Integer in an Array的相关文章

【LeetCode】008 String to Integer (atoi)

题目:LeetCode 008 String to Integer 题意:完成内置函数atoi的功能,将字符串转换成整数. 教训:一开始理所应当的随便一写,然后发现有很多的异常情况需要处理.然后按照C++ Reference中关于atoi的规定一条一条写,才AC.另外还有一个溢出的问题,一开始以为int会自动处理直接返回边界值,其实不是,如果溢出的话大于2147483647的数会给变成负数,因此要单独判断是否会大,但是设置成longlong 之后出现的问题是,还有可能会溢出longlong,所以

【LeetCode】6 - Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Solution: 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 个位数举例 Ⅰ-1.Ⅱ-2.Ⅲ-3.Ⅳ-4.Ⅴ-5.Ⅵ-6.Ⅶ-7.Ⅷ-8.Ⅸ-9 十位数举例 Ⅹ-10.Ⅺ-11.Ⅻ-12.XIII-13.XIV-14.

【LeetCode】8. String to Integer (atoi) 字符串转整数

题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

【LeetCode】8 - String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【LeetCode】013. Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解: 只要知道罗马数字的规律即可 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 1.相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3: 2.小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,

【leetcode】7 Roman to Integer

罗马字符转整数 注意事项: 1 几个罗马字符对应的整数 'I':  1; 'V':  5; 'X':  10; 'L':   50; 'C':  100; 'D':  500; 'M': 1000; 2 对于DC这种前者大于后者的好处理, 对于CD这种前者小于后者的,相当于C+D-2*C class Solution {public:    int toInt(char x){        switch(x){            case 'I': return 1;           

【LeetCode】215. Kth Largest Element in an Array

题目: Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ arr

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k