1. 题目是遇到偶数/2,遇到奇数 *3 + 1的题目,然后找一个range内所有数字的max cycle length。对于一个数字,比如说44,按照题目的公式不停计算,过程是 44, 22, 11, 8, 9 ,1(瞎起的),从441的这个sequence的长度,叫做cycle length。然后题目是给一个range,比如[2,300],求这里面所有数字的cycle length的最大值。follow up11 million

一个数一个数慢慢算,如下算法求出每个数的cycle length

  1. package Sorting;
  2. import java.util.*;
  3.  
  4. public class Solution2 {
  5. public List<Integer> maxCycleLen(int min, int max) {
  6. List<Integer> maxCycle = new ArrayList<Integer>();
  7. for (int i=min; i<=max; i++) {
  8. helper(maxCycle, i);
  9. }
  10. return maxCycle;
  11. }
  12.  
  13. public void helper(List<Integer> maxCycle, int num) {
  14. int len = 1;
  15. while (num != 1) {
  16. if (num%2 == 1) num = num*3+1;
  17. else num = num/2;
  18. len++;
  19. }
  20. maxCycle.add(len);
  21. }
  22.  
  23. /**
  24. * @param args
  25. */
  26. public static void main(String[] args) {
  27. // TODO Auto-generated method stub
  28. Solution2 sol = new Solution2();
  29. List<Integer> res = sol.maxCycleLen(1, 100000);
  30. System.out.println(res);
  31. }
  32.  
  33. }

follow up: 建立一个Lookup table, 算过的数就不算了

  1. package Sorting;
  2. import java.util.*;
  3.  
  4. public class Solution3 {
  5. HashMap<Integer, Integer> map;
  6. public List<Integer> maxCycleLen(int min, int max) {
  7. List<Integer> maxCycle = new ArrayList<Integer>();
  8. map = new HashMap<Integer, Integer>();
  9. for (int i=min; i<=max; i++) {
  10. helper(maxCycle, i);
  11. }
  12. return maxCycle;
  13. }
  14.  
  15. public void helper(List<Integer> maxCycle, int num) {
  16. int len = 0;
  17. int numcopy = num;
  18. while (!map.containsKey(num)) {
  19. if (num == 1) {
  20. map.put(1, 1);
  21. maxCycle.add(1);
  22. return;
  23. }
  24. if (num%2 == 1) num = num*3+1;
  25. else num = num/2;
  26. len++;
  27. }
  28. len = len + map.get(num);
  29. maxCycle.add(len);
  30. map.put(numcopy, len);
  31. }
  32.  
  33. /**
  34. * @param args
  35. */
  36. public static void main(String[] args) {
  37. // TODO Auto-generated method stub
  38. Solution3 sol = new Solution3();
  39. List<Integer> res = sol.maxCycleLen(1, 100000);
  40. System.out.println(res);
  41. }
  42.  
  43. }

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. 使用ngrok

    使用ngrok让微信公众平台通过80端口访问本机 首先声明我是用java-tomcat来研究微信公众平台的. 微信公众平台要成为开发者,需要填写接口配置信息中的“URL”和“Token”这两项(参见: ...

  2. Pentium II paging mechanism

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION To understand the str ...

  3. [转载]:Delphi xe7并行编程快速入门

    现在多数设备.计算机都有多个CPU单元,即使是手机也是多核的.但要在开发中使用多核的优势,却需要一些技巧,花费时间编写额外的代码.好了,现在可以使用Delphi做并行编程了. 在Delphi.C++ ...

  4. OpenGL完全教程 第一章 初始化OpenGL

    第一章 初始化OpenGL 无论是什么东西,要使用它,就必须对它进行初始化.如果你之前使用过GDI,你应该也多多少少了解到GDI在绘制图形之前要为之创建渲染环境.OpenGL也一样.本章给出的代码,大 ...

  5. android imageButton 点击按钮前中后,按钮颜色的变化

    我们在开发的过程中,往往为了美化界面的需要,会修改按钮的默认外观,而因为Android中的按钮有三种状态—默认,被点击,被选中.所以,如果要改变按钮的外观,需要对这三种情况都做出修改,也许在以往,我们 ...

  6. 兼容IE的CSS的”引入方式“

    1.给IE浏览器的7版本来提供需要引用的样式(如果把7去掉则给所有的IE浏览器提供样式) <!--[if IE 7]> <Link type="text/css" ...

  7. Spring @ResponseBody只能返回String类型数据解决办法

    今天自己搭Spring MVC框架玩,使用AJAX调用Spring controller 并返回map对象,突然发现,哎,怎么@Response中只能返回String, 我用的Spring 3的版本也 ...

  8. ajax处理回调函数,用ajax向后台发送数据

    这是我的后台返回给前台的数据: 处理后台返回的数据有一下两种方式: function sethouse_housing_pattern(housing_pattern){ var str=[]; va ...

  9. 读propert文件

    PropertiesUtil.java package utils; import java.io.BufferedInputStream; import java.io.FileInputStrea ...

  10. python和pywin32实现窗口查找、遍历和点击

    Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和 ...