Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1734247
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2016-06-15 13:30:37

1. 一本书共n页,有一串页码从1到n,计算各数码分别用到了几次

注:数码为0,1,2。。。。。。9的数字

先把数字转化成列表,然后从1-n,都转化成列表后extend. 对于这个列表用Counter 处理一下。

点击(此处)折叠或打开

  1. from collections import Counter
  2. def count_pagenum(n):
  3.     ll=list(range(1,10))
  4.     for i in range(10,n+1):
  5.         lpage=num_2_seq(i)
  6.         ll.extend(lpage)
  7.     ll.sort()
  8.     d=Counter(ll)
  9.     print(d)

  10. def num_2_seq(n):
  11.     #n>9
  12.     ll=[]
  13.     while n > 9:
  14.         a,b=divmod(n,10)
  15.         ll.append(b)
  16.         n=a
  17.     ll.append(a)
  18.     return ll

  19. #count_pagenum(11)

2. Given an unsorted integer array, find the first missing positive integer,

INPUT: [1,2,0] OUTPUT:3

INPUT: [3,4,-1,1] OUTPUT:2

leetcode 上的题目,用dict 就可以解决问题。 

点击(此处)折叠或打开

  1. def first_missing_positive(seq):
  2.     length=len(seq)
  3.     if length==0:
  4.         return 1
  5.     numdict={}
  6.     for num in seq:
  7.         if num > 0:
  8.             numdict[num]=num
  9.     for i in range(1,length+1):
  10.         if numdict.get(i,-1)==-1:
  11.             return i
  12.     return length+1

  13. print(first_missing_positive([1,2,0]))
  14. print(first_missing_positive([3,4,-1,1]))
3. Convert a picture to ascii text.

可参考的代码如下:
#
#
#

阅读(1184) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~