LeetCode

1、两数字之和


 

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

 

1、暴力搜索 O(n^2)

# 方案1:暴力搜索
def twoSum1(nums, target):
    n = len(nums)
    for i in range(n):
        for j in range(i+1,n):      # 用x+1是减少不必要的循环,y的取值肯定是比x大
            if nums[i]+nums[j] == target:
                return i,j

 

 2、优化版本

# 方案2:求差值,判断差值是否在nums数组里
# 时间复杂度:O(n2),空间复杂度:O(1)   (
# 补充:python中list对象的存储结构采用的是线性表,因此其查询复杂度为O(n)
# 也就是 if b in nums 时间复杂度是O(n))
def twoSum2(nums, target):
    n = len(nums)
    for i in range(n):
        j = target-nums[i]
        if j in nums:
            if i != nums.index(j):
                return i,nums.index(j)

 

3、 hash表:字典

# 时间复杂度:O(1) 、空间复杂度O(n)
# 补充:dict对象的存储结构采用的是散列表(hash表),其在最优情况下查询复杂度为O(1))
def twoSum(nums, target):

    dic = {}
    n = len(nums)
    for i in range(n):
        j = target - nums[i]

        if nums[i] in dic:  # 字典dic中存在nums[i]时
            return dic[nums[i]],i

        else:
            dic[j] = i  # 否则往字典增加键/值对

            # 边往字典增加键/值对,边与nums[i]进行对比
    return dic

print(twoSum([3,2,4],6))

 

 2、翻转整数

给定一个 32 位有符号整数,将整数中的数字进行反转。

示例 1:

输入: 123
输出: 321

 示例 2:

输入: -123
输出: -321

示例 3:

输入: 120
输出: 21

注意:

假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。

1、lowB写法

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        if x < 0:
            x = list(str(x)[1:])
            x.reverse()
            ret = ''.join(x)
            ret = '-' + ret
            ret = int(ret)
            if (2**31-1) < ret or (-2**31) > ret:
                return 0
            else:
                return ret

        else:
            x = list(str(x))
            x.reverse()
            ret = ''.join(x)
            ret = int(ret)
            if (2**31 - 1) < ret or (-2**31) > ret:
                return 0
            else:
                return ret
        

 

2、大神写法

利用R = X[::-1]这种方法对X(X必须是字符串)进行一个反转复制的操作

class Solution:
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        flag = 1
        if x < 0:
            flag = -1
        ret = str(abs(x))[::-1]
        ret = int(ret)
        return ret*flag if -2147483648 < ret < 2147483647 else 0

 

3、flag标志位使用的小技巧
flag是用来判断输入值正负的标志值。用 11 和 1−1 作为其判断值,可以在返回时,无需使用if语句,只需要返回数值乘以flag即可,使代码更加精简。

3、大大神代码

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
        return x if x < 2147483648 and x >= -2147483648 else 0

 

 

3、回文数


 

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

示例 1:

输入: 121
输出: true

示例 2:

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

示例 3:

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

进阶:

你能不将整数转为字符串来解决这个问题吗?

1、我的代码

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        x = str(x)
        x1 = x[::-1]
        return True if x==x1 else False

 

2、大神代码

def is_palindrome(n):
    n=str(n)
    m=n[::-1]
    return n==m

 

 

4

 

 

5

6

 

posted @ 2018-08-18 21:07  venicid  阅读(210)  评论(0编辑  收藏  举报