题目是遇到偶数/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的更多相关文章

  1. 数据库操作提示:Specified key was too long; max key length is 767 bytes

    操作重现: 法1:新建连接——>新建数据库——>右键数据库导入脚本——>提示:Specified key was too long; max key length is 767 by ...

  2. Mysql Specified key was too long; max key length is 767 bytes

    今天导入一个数据库时,看到以下报错信息: Specified key was too bytes 直译就是索引键太长,最大为767字节. 查看sql库表文件,发现有一列定义如下: 列   名:cont ...

  3. Specified key was too long; max key length is 767 bytes mysql

    Specified key was too long; max key length is 767 bytes 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该 ...

  4. 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 ...

  5. 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错误,但数据库和表也建成功了.有高人知 ...

  6. 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

  7. 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的字段设置 ...

  8. 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 ...

  9. Specified key was too long; max key length is 767 bytes解决方案

    问题描述: 1.  使用spark sql处理数据逻辑,逻辑处理后使用 df.write.mode(saveMode).jdbc(url, tableName, connectionPropertie ...

随机推荐

  1. (转)面试题--JAVA中静态块、静态变量加载顺序详解

    public class Test { //1.第一步,准备加载类 public static void main(String[] args) { new Test(); //4.第四步,new一个 ...

  2. js制作带有遮罩弹出层实现登录小窗口

    要实现的效果如下 点击“登录”按钮后,弹出登录小窗口,并且有遮罩层(这个名词还是百度知道的,以前只知道效果,却不知道名字) 在没有点击“登录”按钮之前登录小窗口不显示,点击“登录”按钮后小窗口显示,并 ...

  3. linux 不能用clock 计算sleep的时间

    http://bbs.csdn.net/topics/390558707 在Windows Sleep()占用processor time,Linux下的sleep()不占用processor tim ...

  4. 关于<a href='javascript:function()'>

    <a href='javascript:function()'> 这样写是为了让这个链接不要链接到新页面转而执行一段js代码.和onclick能起到同样的效果,一般来说,如果要调用脚本还是 ...

  5. php--数据库三范式

    关系数据库的几种设计范式介绍1.第一范式(1NF) 在任何一个关系数据库中,第一范式(1NF)是对关系模式的基本要求,不满足第一范式(1NF)的数据库就不是关系数据库. 所谓第一范式(1NF)是指数据 ...

  6. [Stanford 2011] MVC introduction

    以下是课程笔记,仅供以后复习之便. 1.什么是MVC? (1) Model:如飞机激战的游戏中,太空中的飞船,什么机型,每个飞船有多少机枪,多少护甲,这些硬件组成是model. (2)Controll ...

  7. node express 学习2

    上次我们的express已经安装好了 接下来我们修改渲染引擎为html // view engine setup app.set('views', path.join(__dirname, 'view ...

  8. js数组到后台转 list数组

    前台的数组格式是: [{"credit_record_certificate_id":"452","credit_record_type": ...

  9. ie下的onscroll和onresize的优化

    ie下的scroll和resize的优化 1.onscroll function scrollEvent(){ //do something... console.log('do something. ...

  10. iOS:特殊符号大全

    特殊符号大全分享给大家,直接复制粘贴就可以使用了! ░ ▒ ▬ ♦ ◊ ◦ ♠ ♣ ▣ ۰•● ❤ ●•۰ ► ◄ ▧ ▨ ♨ ◐ ◑ ↔ ↕ ▪ ▫ ☼ ♦ ♧♡♂♀♠♣♥❤☜☞☎☏⊙◎ ☺☻☼▧▨ ...