Groupon面经Prepare: Max Cycle Length
题目是遇到偶数/2,遇到奇数 *3 + 1的题目,然后找一个range内所有数字的max cycle length。对于一个数字,比如说44,按照题目的公式不停计算,过程是 44, 22, 11, 8, 9 ,1(瞎起的),从44到1的这个sequence的长度,叫做cycle length。然后题目是给一个range,比如[2,300],求这里面所有数字的cycle length的最大值。follow up跑1到1 million
一个数一个数慢慢算,如下算法求出每个数的cycle length
package Sorting;
import java.util.*; public class Solution2 {
public List<Integer> maxCycleLen(int min, int max) {
List<Integer> maxCycle = new ArrayList<Integer>();
for (int i=min; i<=max; i++) {
helper(maxCycle, i);
}
return maxCycle;
} public void helper(List<Integer> maxCycle, int num) {
int len = 1;
while (num != 1) {
if (num%2 == 1) num = num*3+1;
else num = num/2;
len++;
}
maxCycle.add(len);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution2 sol = new Solution2();
List<Integer> res = sol.maxCycleLen(1, 100000);
System.out.println(res);
} }
follow up: 建立一个Lookup table, 算过的数就不算了
package Sorting;
import java.util.*; public class Solution3 {
HashMap<Integer, Integer> map;
public List<Integer> maxCycleLen(int min, int max) {
List<Integer> maxCycle = new ArrayList<Integer>();
map = new HashMap<Integer, Integer>();
for (int i=min; i<=max; i++) {
helper(maxCycle, i);
}
return maxCycle;
} public void helper(List<Integer> maxCycle, int num) {
int len = 0;
int numcopy = num;
while (!map.containsKey(num)) {
if (num == 1) {
map.put(1, 1);
maxCycle.add(1);
return;
}
if (num%2 == 1) num = num*3+1;
else num = num/2;
len++;
}
len = len + map.get(num);
maxCycle.add(len);
map.put(numcopy, len);
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution3 sol = new Solution3();
List<Integer> res = sol.maxCycleLen(1, 100000);
System.out.println(res);
} }
Groupon面经Prepare: Max Cycle Length的更多相关文章
- 数据库操作提示:Specified key was too long; max key length is 767 bytes
操作重现: 法1:新建连接——>新建数据库——>右键数据库导入脚本——>提示:Specified key was too long; max key length is 767 by ...
- Mysql Specified key was too long; max key length is 767 bytes
今天导入一个数据库时,看到以下报错信息: Specified key was too bytes 直译就是索引键太长,最大为767字节. 查看sql库表文件,发现有一列定义如下: 列 名:cont ...
- Specified key was too long; max key length is 767 bytes mysql
Specified key was too long; max key length is 767 bytes 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该 ...
- Using innodb_large_prefix to avoid ERROR #1071,Specified key was too long; max key length is 1000 bytes
Using innodb_large_prefix to avoid ERROR 1071 单列索引限制上面有提到单列索引限制767,起因是256×3-1.这个3是字符最大占用空间(ut ...
- EF MySQL 提示 Specified key was too long; max key length is 767 bytes错误
在用EF的CodeFirst操作MySql时,提示 Specified key was too long; max key length is 767 bytes错误,但数据库和表也建成功了.有高人知 ...
- Specified key was too long; max key length is 767 b
alter table - engine=innodb,row_format=dynamic; Specified key was too long; max key length is 767 b
- MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
MySQL错误"Specified key was too long; max key length is 1000 bytes"的解决办法 经过查询才知道,是Mysql的字段设置 ...
- ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
今天在MySQL 5.6版本的数据库中修改InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key le ...
- Specified key was too long; max key length is 767 bytes解决方案
问题描述: 1. 使用spark sql处理数据逻辑,逻辑处理后使用 df.write.mode(saveMode).jdbc(url, tableName, connectionPropertie ...
随机推荐
- mysql线程缓存和表缓存
一.线程缓存1.thread_cache_size定义了线程缓冲中的数量.每个缓存中的线程通常消耗256kb内存2.Threads_cached,可以看到已经建立的线程二.表缓存(table_cach ...
- CentOS 6 RPM安裝python 2.7
先说第一种方法,通过rpmbuild编译XXX.src.rpm包([1].[2]): 安装依赖:sudo yum install -y make autoconf bzip2-devel db4-de ...
- JAVA Callable
Listing -. Calculating Euler’s Number e import java.math.BigDecimal; import java.math.MathContext; i ...
- 在 mac os 上搭建 git server
前言:之前学习了如何使用 git 后,一直想搭建一个本机搭建一个 git server 的,一开始不知道走了弯路用了 gitosis,折腾了我好几天都没配置好.昨晚查资料发现 gitosis 早就过时 ...
- Python 汉字简体和繁体的相互转换
其实利用python实现汉字的简体和繁体相互转早有人做过,并发布到github上了,地址:https://github.com/skydark/nstools/tree/master/zhtools ...
- IE中的fireEvent和webkit中的dispatchEvent
拿浏览器的click事件来说: 在IE浏览器中如果一个element没有注册click事件,那么直接调用的话会出现异常!当然如果你注册了没有什么可说的. 那么如果使用fireEvent来处理,clic ...
- 20145211 《Java程序设计》第九周学习总结——垂死病中惊坐起
教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性 JDBC标准分为两个部分:J ...
- MVVM with ReactiveCocoa
内容提要: 本文首先对比MVC简单介绍了MVVM的概念和优点,其次,简单介绍了Reactive Cocoa的使用,最后,通过一个例子介绍了使用Reactive Cocoa的MVVM框架. 正文: 首先 ...
- 两排滚动js
html: <div class="mr_frbox"> <div class="showtitle"> ...
- ComparatorChain、BeanComparator用法示例(枚举类型排序转)
工作中遇到按照类的某个属性排列,这个属性是个枚举类型,按照要求的优先级排列. 可以使用ComparatorChain.BeanComparator.FixedOrderComparator实现. 举一 ...