LeetCode "Ugly Number II"
2015-08-25 13:15
197 查看
Key: each of 2\3\5 is trying to multiply with the least number it has not been multiplied.
class Solution { public: int nthUglyNumber(int n) { if(n< 1) return 0; int ret = 1, i2 = 0, i3= 0, i5 = 0; vector<int> buf; while(--n) { buf.push_back(ret); int v2 = buf[i2] * 2; int v3 = buf[i3] * 3; int v5 = buf[i5] * 5; ret = min(v2, min(v3, v5)); i2 += ret == v2; i3 += ret == v3; i5 += ret == v5; } return ret; } };
相关文章推荐
- python find file
- 使用博客的一些理由
- jackJson的基本用法1___obj-json 与 json-obj
- (转)python 计算代码行数以及相关知识点
- MySQL执行mysql_install_db初始化
- JQuery中根据属性或属性值获得元素(6种情况获取方法)
- poj 3615
- 更改文件中的内容
- 利用URL Protocol实现网页调用本地应用程序
- 初亏博弈门道
- 华为OJ(统计每个月兔子的总数)
- UVA 10158 War 并查集
- Python并发编程
- maven使用Nexus私有仓库进行管理
- js方法格式化数字,加入千分符,并可以指定小数点位数
- IOS 多线程(4) --线程通讯
- HDU 2222 AC自动机
- 【POJ1456】【并查集变形】【技巧】
- 《数据库系统概念第六版》读书笔记
- C++常用术语及其英文翻译的含义和简单用途总结(八)