开启leetcode刷题

开始刷leetcode,从容易的开始。
坚持不易,加油!
码云地址刷题记录:代码


1. 两数之和

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

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
'''
"""
给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
"""
from typing import List

def twoSum(nums: List[int], target: int) -> List[int]:
    length = len(nums)
    for i in range(length):
        for j in range(i + 1, length):
            if nums[i] + nums[j] == target:
                return [i, j]

def twoSum2(nums, target):
    lens = len(nums)
    j = -1
    for i in range(lens):
        if (target - nums[i]) in nums:
            if (nums.count(target - nums[i]) == 1) & (
                    target - nums[i] == nums[i]):  # 如果num2=num1,且nums中只出现了一次,说明找到是num1本身。
                continue
            else:
                j = nums.index(target - nums[i], i + 1)  # index(x,i+1)是从num1后的序列后找num2
                break
    if j > 0:
        return [i, j]
    else:
        return []

def twoSum3(nums, target):
    lens = len(nums)
    j = -1
    for i in range(1, lens):
        temp = nums[:i]
        if (target - nums[i]) in temp:
            j = temp.index(target - nums[i])
            break
    if j >= 0:
        return [j, i]


2. 整数反转

'''
整数反转:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2**31,  2**31 − 1]。
请根据这个假设,如果反转后整数溢出那么就返回 0。
'''
"""
输入: 123
输出: 321

输入: -123
输出: -321

输入: 120
输出: 21
"""
class Solution:
    def reverse(self, x: int) -> int:
        str_x = str(x)
        if not str_x.startswith('-'):
            res = int(str_x[::-1])
        else:
            res = int(str_x[:0:-1])
            res = - res
        return res if -2147483648 < res < 2147483647 else 0

if __name__ == '__main__':
    s = Solution()
    print(s.reverse(-9))

3. 回文数

'''
回文数
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
'''
"""
输入: 121
输出: true

输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。

输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
"""
class Solution:
    def isPalindrome(self, x: int) -> bool:
        # return str(x) == str(x)[::-1]
        if x < 0 or (x != 0 and x % 10 == 0):
            ret = False
        else:
            str_x = str(x)
            reversed = str_x[::-1]
            if reversed == str_x:
                ret = True
            else:
                ret = False
        return ret

class Solution2:  # 不将int转为string
    def isPalindrome(self, x: int) -> bool:
        if x < 0 or (x != 0 and x % 10 == 0):
            ret = False
        elif x == 0:
            ret = True
        else:
            reverse_x = 0
            while x > reverse_x:
                remainder = x % 10
                reverse_x = reverse_x * 10 + remainder
                x = x // 10
            # 当x为奇数时, 只要满足 reverse_x//10 == x 即可
            if reverse_x == x or reverse_x // 10 == x:
                ret = True
            else:
                ret = False
        return ret

if __name__ == '__main__':
    s = Solution2()
    print([s.isPalindrome(i) for i in [1221, 121, 123, 10, -121]])
发布了87 篇原创文章 · 获赞 43 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_31362767/article/details/105026891
今日推荐