算法

(1)题目1

给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个位置。

思路:不要上来就相加,要思考0的位置。

(1)nums[0] == 0,且len(nums) is not 1

(2)nums[i] == 0,0<i<(len(nums)-1),即0在中间位置,这时只需0之前有一个nums[j] > (i-j)即可跨过0,可到达尾部

答案:

class Solution:
    def canJump(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        if nums[0] is 0:
            if len(nums) is 1:
                return True
            else:
                return False
        elif 0 not in nums[:-1]:
            return True
        else:
            for i in range(1, len(nums)-1):
                if nums[i] is 0:
                    res = False
                    for j in range(i):
                        if nums[j] > (i-j):
                            res = True
                    if res is False:
                        return False
            return True

(2)题目2

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

思路:迭代

strs_list = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']

def get_str(num, strs):
    global strs_list
    strs_res = []
    if num > 1:
        if len(strs) is 0:
            for word in strs_list[num-2]:
                strs.append(word)
            return strs
        else:
            for i in range(len(strs)):
                for word in strs_list[num-2]:
                    strs_res.append(strs[i]+word)
    return strs_res

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        global strs_list
        
        res = []
        # digits = sorted(digits)
        
        if digits is None:
            return None
        
        for num in digits:
            res = get_str(int(num), res)
        return res

(3)题目3

给定一个非空的整数数组,返回其中出现频率前 高的元素。(同频的要算做多个,原题要求,同频算一个其实也对)

思路:有序字典(如pandas的DataFrame,也可用列表加字典实现):统计数组中个数字个数,同频数字已频数为索引放在字典中,同时频数放在一个索引数组中;将索引数组排序,再用索引去查字典;直至查够要求的个数

# import pandas as pd

class Solution:
    def topKFrequent(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: List[int]
        """
        nums = sorted(nums)
        nums_dict = {}
        nums_index = []
        i = 0
        while True:
            try:
                num_count = nums.count(nums[i])
                if num_count not in nums_dict.keys():
                    nums_dict[num_count] = []
                nums_dict[num_count].append(nums[i])
                if num_count not in nums_index:
                    nums_index.append(num_count)
                while nums.count(nums[i]) > 1:
                    nums.remove(nums[i])
                i += 1
            except IndexError:
                break

        # ds = pd.Series(nums_dict).sort_index(ascending=False)
        # res = []
        # for i in range(k):
        #     for num in ds.iloc[i]:
        #         res.append(num)
        #         if len(res) >= k:
        #             return res
        # return res
        
        res = []
        nums_index = sorted(nums_index, reverse=True)
        for i in range(len(nums_index)):
            try:
                for num in nums_dict[nums_index[i]]:
                    res.append(num)
                    if len(res) >= k:
                        return res
            except:
                break
        return res

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值