python 寻找没有出现的数字
2014-05-02 12:44
316 查看
Missing numberGiven an array containing all numbers from 1 to N with the exception of one print the missing number to the standard output.Example input:array: 5 4 1 2Example output:3////////////////////////////打算使用二进制的特性,但编译器的精度不给过。。
import mathdef find_missing_number(v):# Write your code here# To print results to the standard output you can use print# Example: print "Hello world!"maxNum=0sumNum = 2for item in v:sumNum+=2**itemif(item > maxNum):maxNum =itemprint math.floor( math.log(2**(maxNum+1) - sumNum - 2, 2) )////////////////////////////那就用等差数列公式吧
import mathdef find_missing_number(v):# Write your code here# To print results to the standard output you can use print# Example: print "Hello world!"maxNum=NoneminNum =()sumNum = 0for item in v:sumNum+=itemif(item > maxNum):maxNum =itemif (item < minNum):minNum = itemif minNum != 1:print '1'else:print (minNum+maxNum)*(len(v)+1)/2 - sumNum///////////////////////////////////////另外有更简洁的
def find_missing_number(v):n = len(v) + 1print(n * (n+1) / 2 - sum(v))
相关文章推荐
- python文件整理工具
- python with 和 contextlib
- Python 2.7安装setuptools时的UnicodeDecodeError解决办法
- python登录新浪微博并抓取内容
- python实现凯撒加密
- python学习之--习题集
- python—xml读写—minidom and etree
- Benchmark of Python WSGI Servers
- Asynchronous Servers in Python
- python文件调用另一个python class文件中的方法
- double_linked_list in Python
- python -----example
- Leetcode_divide-two-integers(python version)
- [leetcode]Valid Number @ Python
- python 第6章抽象
- python 第6章抽象
- python 安装django
- Python修饰器的函数式编程
- Python inpu和raw_input的区别
- 修复升级python版本之后ibus输入法不显示控制面板和候选框的问题