算法--学习记录

1.两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for index,value in enumerate(nums):
            if (target - value) not in nums[index+1:]:
                continue
            return [index, nums[index+1:].index(target-value)+index+1]

算法--学习记录_第1张图片

2.回文数 

给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

class Solution:
    def isPalindrome(self, x: int) -> bool:
        if not str(x) == str(x)[::-1]:
            return False
        return True

算法--学习记录_第2张图片

你可能感兴趣的:(学习)