题目描述

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
 
# -*- coding:utf-8 -*-
class Solution:
def GetUglyNumber_Solution(self, index):
# write code here
#如果为空则返回0
#如果不为空则通过索引判断是否为丑数
if index <=0:
return 0
res = [1]
index2 = index3 = index5 = 0
for i in range(1, index):
u2 = res[index2] * 2
u3 = res[index3] * 3
u5 = res[index5] * 5
res.append(min(u2, min(u3, u5)))
if res[i]/2 == res[index2]:
index2 += 1
if res[i]/3 == res[index3]:
index3 += 1
if res[i]/5 == res[index5]:
index5 += 1
return res.pop()

  

丑数(python)的更多相关文章

  1. 【python】Leetcode每日一题-丑数2

    [python]Leetcode每日一题-丑数2 [题目描述] 给你一个整数 n ,请你找出并返回第 n 个 丑数 . 丑数 就是只包含质因数 2.3 和/或 5 的正整数. 示例1: 输入:n = ...

  2. 【python】Leetcode每日一题-丑数

    [python]Leetcode每日一题-丑数 [题目描述] 给你一个整数 n ,请你判断 n 是否为 丑数 .如果是,返回 true :否则,返回 false . 丑数 就是只包含质因数 2.3 和 ...

  3. python练习笔记——丑数的计算

    只包含因子2,3,5的正整数被称作丑数,比如4,10,12都是丑数,而7,23,111则不是丑数,另外1也不是丑数.——摘自百度百科 get_num = int(input("请您输入丑数的 ...

  4. leetcode python丑数

    # Leetcode 263 丑数### 题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 `2, 3, 5` 的**正整数**. **示例1:** 输入: 6 输出: true ...

  5. lintcode :Ugly Numbers 丑数

    题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n ...

  6. ural 1748 The Most Complex Number 和 丑数

    题目:http://acm.timus.ru/problem.aspx?space=1&num=1748 题意:求n范围内约数个数最多的那个数. Roughly speaking, for a ...

  7. 剑指Offer 33. 丑数 (其他)

    题目描述 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目地 ...

  8. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

  9. 力扣题目汇总(丑数,重复N的元素,求众数)

    丑数 1.题目描述 编写一个程序判断给定的数是否为丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例 1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例 2: 输入: 8 ...

随机推荐

  1. codeforces 804A Find Amir 思维/水题

    A few years ago Sajjad left his school and register to another one due to security reasons. Now he w ...

  2. CSRF的防御解决过程

    CSRF是什么,就不多说,网络上的帖子多的去了,关于其定义. 这里主要介绍我们项目中,是如何解决这个问题的.方案比较简单,重点是介绍和记录一下遇到的问题和一些小的心得. 1. 解决方案 A. 用户登录 ...

  3. keepalived 高可用配置

    下载地址:http://www.keepalived.org/software/keepalived-1.2.12.tar.gzht 安装方法:1. ./configure 可能出现的错误 !!! O ...

  4. python成功之道

    https://blog.ansheng.me/article/python-full-stack-way

  5. getattribute

    属性访问拦截器  class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self.subject2 = ...

  6. PAT 甲级 1027 Colors in Mars (20 分)

    1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...

  7. Angular2中使用ngx-translate进行国际化

    转自 https://blog.csdn.net/u014291497/article/details/61233033 相较于angularjs中的ng-translate, angular2也有适 ...

  8. gradle重复依赖终极方案解决办法

    buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build: ...

  9. docker入门 什么是docker? 为什么使用docker?

    1.什么是docker? 轻量级操作系统虚拟化解决方案 2.为什么使用docker? 1.docker的启动是秒级的,比传统虚拟机快很多 2.资源利用率高,一台主机上可同时运行数千个docker容器 ...

  10. C#控制台程序点击后暂停工作

    C#控制台应用程序,点击后就会暂停运行,但是我想让它运行不受点击的干扰.下面是程序演示: public void Test() { ThreadOut(); } private void Thread ...