博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode-Python 1 .Two Sum/ 7. Reverse Integer
阅读量:4100 次
发布时间:2019-05-25

本文共 1449 字,大约阅读时间需要 4 分钟。

1Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].
class Solution(object):    def twoSum(self, nums, target):        d = {}        for i, n in enumerate(nums):            sec_n = target - n            if sec_n in d:                return [d[sec_n], i]            else:                d[n] = ix =Solution()print(x.twoSum([1,1,3,4],5))

7Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123Output:  321

Example 2:

Input: -123Output: -321

Example 3:

Input: 120Output: 21

Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

class Solution:    def reverse(self, x):        """        :type x: int        :rtype: int        """        x = str(x)        if x[-1] == "0" and len(x)>1:        	x = x[:-1]        if x[0] == "-":        	res = int("-" + x[-1:0:-1])        	return res if res.bit_length() < 32 else 0        else:        	res = int(x[::-1])        	return res if res.bit_length() < 32 else 0x = Solution()print(x.reverse(0))print(x.reverse(-12030))
 

转载地址:http://yvzsi.baihongyu.com/

你可能感兴趣的文章
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet&jsp 的使用以及jsp的历史遗留用法
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>
设计模式六大原则(6):开闭原则
查看>>
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
【设计模式】—-(2)工厂方法模式(创建型)
查看>>
有return的情况下try catch finally的执行顺序(最有说服力的总结)
查看>>
String s1 = new String("abc"); String s2 = ("abc");
查看>>
JAVA数据类型
查看>>
Xshell 4 入门
查看>>
SoapUI-入门
查看>>