python练习(十二)
来源:互联网 发布:李易峰睡杨幂 知乎 编辑:程序博客网 时间:2024/06/08 07:42
- Move Zeroes
- 题目
- 思路与解答
- 答案
- Sum of Two Integers
- 题目
- 思路与解答
- 扩展阅读-取模mod与取余rem的区别
- 上边的扩展阅读和这道题毛线关系都没有继续做题
- 答案
- Ugly Number
- 题目
- 思路与解答
- 答案
- Two Sum
- 题目
- 思路与解答
- 答案
- Single Number
- 题目
- 思路与解答
- 答案
注意,答案只是代表是他人写的代码,正确,但不一定能通过测试(比如超时),列举出来只是它们拥有着独到之处,虽然大部分确实比我的好
Move Zeroes
题目
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
思路与解答
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ l = len(nums) nums.remove(k) nums += [0]*(l-len(nums))
remove不是全删掉啊
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ l = len(nums) while 0 in nums: nums.remove(0) nums += [0]*(l-len(nums))
225ms,15%,绝对不行啊
答案
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ nums.sort(key = lambda x:1 if x == 0 else 0)
解释
# in-placedef moveZeroes(self, nums): zero = 0 # records the position of "0" for i in xrange(len(nums)): if nums[i] != 0: nums[i], nums[zero] = nums[zero], nums[i] zero += 1
Sum of Two Integers
题目
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
思路与解答
不允许使用加和减……
怎么做,位操作可以吗?不过不会啊
感谢LHearen,这个链接是关于位操作的一个总结
学习后的代码
while b: a,b=a^b,(a&b)<<1 return a
然后测试时输入个负数,炸了
负数是什么鬼东西啊,负数是怎么在python中存储的啊
扩展阅读-取模(mod)与取余(rem)的区别
想搜负数在python中的存储方式,不太好搜,倒是搜到一些其它有意思的东西
负数除法相关-知乎-有些乱,详实
负数除法相关-博客-清晰,不全
取模运算-百度百科-写的不好,我都想把这个链接踢掉
取模(mod)与取余(rem)的区别-鬼知道作者是谁了
上边的扩展阅读和这道题毛线关系都没有,继续做题
这才是对这道题有帮助的文章
#不用额外的变量实现两个数字互换。def swap(num_1, num_2): num_1 ^= num_2 num_2 ^= num_1 num_1 ^= num_2 return num_1, num_2'''证明很简单,我们只需要明白异或运算满足下面规律:0^a = a;a^a = 0;a^b^c = a^c^b;'''
又跑题了?
class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ mask = 0xFFFFFFFF while b: a,b=(a^b)&mask,((a&b)<<1)&mask return a if a <= 0x7FFFFFFF else ~(a ^ mask)
不&mask就会出错
答案
那些用sum的我就不提了
class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ # 32 bits integer max MAX = 0x7FFFFFFF # 32 bits interger min MIN = 0x80000000 # mask to get last 32 bits mask = 0xFFFFFFFF while b != 0: # ^ get different bits and & gets double 1s, << moves carry a, b = (a ^ b) & mask, ((a & b) << 1) & mask # if a is negative, get a's 32 bits complement positive first # then get 32-bit positive's Python complement negative return a if a <= MAX else ~(a ^ mask)
Ugly Number
题目
Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
思路与解答
class Solution(object): def isUgly(self, num): """ :type num: int :rtype: bool """ if not num:return False while num%2==0: num = num/2 while num%3==0: num = num/3 while num%5==0: num = num/5 return True if num ==1 else False
之前居然忽略0了,0不ugly哦
答案
for p in 2, 3, 5: while num % p == 0 < num: num /= preturn num == 1
return的很好。循环的也很好,去掉0的判断也很好
class Solution(object): def isUgly(self, num): """ :type num: int :rtype: bool """ #n = (2**30)*(3**20)*(5**13) = 4570198050078720000000000000L return False if num < 1 or (4570198050078720000000000000L)%num != 0 else True
巧妙的数学方法
这只是一个数学trick.ex:
如果k = 2 ^ n
则2 ^ 30%k == 0
Two 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): """ :type nums: List[int] :type target: int :rtype: List[int] """ for k,v in enumerate(nums): if (target-v) in nums: return [k,nums.index(target-v)]
哦,不能重复使用
class Solution(object): def twoSum(self, nums, target): l=[] for k,v in enumerate(nums): if (target-v) in l: return [l.index(target-v),k] l.append(v)
虽然512ms,40%,但是,不可接受
答案
我看其它答案就是把列表换成了字典,于是我就改了一下
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ l={} for k,v in enumerate(nums): if (target-v) in l: return [l[target-v],k] l[v] = k
36ms,哇哦噢噢噢噢,爆炸
在字典里查值只需要O(1)
Single Number
题目
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
思路与解答
想起了异或,写写试试
return reduce(operator.xor,nums)
so easy!
答案
def singleNumber1(self, nums): dic = {} for num in nums: dic[num] = dic.get(num, 0)+1 for key, val in dic.items(): if val == 1: return keydef singleNumber2(self, nums): res = 0 for num in nums: res ^= num return resdef singleNumber3(self, nums): return 2*sum(set(nums))-sum(nums)def singleNumber4(self, nums): return reduce(lambda x, y: x ^ y, nums)def singleNumber(self, nums): return reduce(operator.xor, nums)
第3种很有想法
第一种好像可以优化下
抱歉,并不能
不对,如果这样的话
class Solution(object): def singleNumber(self, nums): dic = {} for num in nums: if num in dic: del dic[num] else: dic[num] = 1 for key,v in dic.items(): return key
确实快了一点
- python练习(十二)
- Scala练习(十二)
- 学习Python(十二)
- Python (十二)模块
- (二十二)java小练习三
- python 学习笔记(十二)
- 《Python编程》笔记(十二)
- Python学习笔记(十二)
- python学习(二十二)
- Python(十二)元类
- Python学习笔记(十二)-- Python模块
- 十二章 练习
- 十二章练习
- python练习(二)
- python练习(三)
- python练习(一)
- python练习(三)
- python练习(四)
- hdu_6016
- codeup 1906 特殊乘法
- AddBinary
- 关于除法的细节问题。。。蛋疼。。
- 第一个网站搭建过程额(2)
- python练习(十二)
- H
- 20170812_继承与多态测试实例
- ucos2中涉及到的英文
- hadoop学习网站
- LeetCode 343. Integer Break
- 安装windows_server2008配置外网可以访问的数据库
- 2017——8.12——二分
- HDU 3247 AC自动机+状压DP