01.python算法题练习

2022-05-26  本文已影响0人  软件开发技术修炼

1. 统计一致字符串的数目

给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
请你返回 words 数组中 一致字符串 的数目。

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        count1= 0
        for i in words:
            tmp = set(i)
            for j in tmp:
                if j not in allowed:
                    break
            else:
                count1 += 1
        return count1
letcode中其他的解法

2. 两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            tmp = target - nums[i]
            if tmp in nums[i+1:]:
                return [i,nums[i+1:].index(tmp)+i+1]

来源:力扣

上一篇 下一篇

猜你喜欢

热点阅读