Lucky Number


http://blog.csdn.net/zengzhen_csdn/article/details/52450129
4和7是两个幸运数字,我们定义,十进制表示中,每一位只有4和7两个数的正整数都是幸运数字。前几个幸运数字是:4,7,44,47,74,77,444,447…
输入:
数字k
输出:
第k个幸运数
样例输入:
3
5
100
10000000
样例输出:
74
744747
44774447447477474444447
思路:
使用完全二叉树的思想,假设根节点为第0个数,值为-1;第1个数值为4,第2个数值为7,第3个数值为4,第4个数值为7,以此类推,即第奇数个数值为4,第偶数个数值为7。同时将其转为完全二叉树,如图所示:
这里写图片描述
于是第k个幸运数就是从位置为k的叶子结点向上遍历直到根节点的左或右子节点的路径。根据完全二叉树的性质,可以由子节点的位置求得其父节点的位置。若子节点是奇数位置n,则其父节点是(n-1)/2;若子节点是偶数位置,则其父节点是(n-2)/2。遍历过程中拼接字符串即可得到幸运数。

public void luckyNum(int n){ String res = ""; while(n > 0){ if((n & 0x01) == 1){ n = (n-1)/2; res = 4 + res; } else{ n = (n-2)/2; res = 7 + res; } } System.out.println(res); }
http://blog.csdn.net/u011824857/article/details/52494101
基本上都是用二进制的思想进行处理
1、将幸运数字的解空间,当做一个二叉树来处理
 2、一个j位长的幸运数字,可以看成是进行了j次选择,每次从4和7中选择一个作为幸运数字的一位,最终组成j+1层的二叉树
 3、为了方便计算,我们将选4记为0,将选7记为1
 4、求解的过程分两步:
 4.1、求解所在的层数(利用二叉树的特性来计算)
 4.2、求解是所在的层得几个节点(利用二叉树的性质来计算)
 5、将得到的选择转换成4和7,即0->4;1->7
  1.     public static String getLuckNumber(int n){  
  2.             //1、为方便处理将n装换成二进制的串  
  3.             String str = Integer.toBinaryString(n);  
  4.               
  5.             //2、计算二叉树的层数层数  
  6.             int level = str.length();  
  7.             if(!str.contains("0")){  
  8.                 level += 1;  
  9.             }  
  10.               
  11.             //3、设定第n个是所在层的第k个,计算K  
  12.             int m= n-((1<<(level-1)) -2);   
  13.               
  14.             //4、计算对应的二进制  
  15.             //  (1)位数:就是所在层数减一,即level-1  
  16.             //  (2)大小:树的同一层是从0开始记得,以第四层为例 000,001,010,011,100,101,110,111  
  17.             //      而m是从1,开始记的,所以从零开始计的话,就是第m-1个节点,  
  18.             //      第m-1个节点的值就是将m-1转成二进制,左面以0填充  
  19.             String result = Integer.toBinaryString(m-1);  
  20.             while(result.length()<level-1){  
  21.                 result = "0"+result;  
  22.             }  
  23.               
  24.             //5、将“0”换成“4”,“1”换成“7”,得到最终结果  
  25.             result = result.replace("0""4");  
  26.             result = result.replace("1""7");  
  27.             return result;  
  28.     } 
http://www.jianshu.com/p/47224ce04d2d
将4换成0,7换成1,那么
4, 7, 44, 47, 74, 77, 444, 447... 变成了
0, 1, 00, 01, 10, 11, 000, 001...对应的十进制是:
0, 1, 0, 1, 2, 3, 0, 1...看着没什么规律啊,不好处理,关键的问题在于00,01,000等都因为前边是0失去了本身的大小,那么如果我们在前面都加个1呢?
10, 11, 100, 101, 110, 111, 1000, 1001...变成十进制:
2, 3, 4, 5, 6, 7, 8, 9...
规律出来了。
我要求第K个数字,那么反向推不就得了。
先把K变成二进制(K+1的二进制),然后去掉最前面的1,然后将0替换为4,将1替换为7。答案就出来了。

http://m.blog.csdn.net/article/details?id=52443975
  while (cin.hasNextInt()) {

   count = cin.nextInt();
   int nArr[] = new int[count];
   for (int i = 0; i < count; i++) {
    nArr[i] = cin.nextInt();
   }
   for (int i = 0; i < count; i++) {
    StringBuilder resultBuilder = new StringBuilder();
    n = nArr[i];
    int length;
    for (int j = 2;; j++)
     if (n <= Math.pow(2, j) - 2) {
      length = j - 1;
      break;
     }
    while (length > 0) {
     length--;
     if (n > Math.pow(2, length + 1) - 2 + Math.pow(2, length)) {
      resultBuilder.append(7);
      n = n - (int) Math.pow(2, length + 1);
     } else {
      resultBuilder.append(4);
      n = n - (int) Math.pow(2, length);
     }
    }
    System.out.println(resultBuilder.toString());
   }

http://blog.tk-xiong.com/archives/956
代码如下: 注意一定是Long Long – 我调了20分钟,就是因为它。
PS.幸运数我下来略微改动了一下,不过肯定可以AC的。

有人问思想,我来补充下。打表: 0 是 NULL
012345678910
4744477477444447474477
我们可以看到
Lucky(3) 是 Lucky(1) + 4
Lucky(4) 是 Lucky(1) + 7
Lucky(5) 是 Lucky(2) + 4
Lycky(6) 是 Lucky(2) + 7
是不是和格雷码的 0 1  00 01 10 11 … 很像。
画个图哈
这样就很清楚了…
我要输出444 就输出 44 (Lucky(Prev)) 然后输出一个 4 (Lucky(Now))
我要输出 744 就输出一个 74 然后输出一个 4
好理解吧…

有网友简化了一下Lucky函数…这里贴一下:我还是稍微改了下…那些中间值不用也罢。
但是就是可能写上,清楚一些。 447 可以理解为 44是前一层, 7 是当前层。

然后根据网友的建议…再画一个二叉树的图…帮助理解:
按这个思路来:每一条路径就是一个结果

这里网友提供了一种新方法:二进制方法 – 打表如下:
输入数据二进制数据处理分析最终结果
11不输出NULL
21004
31117
41000044
51010147
61101074
71111177
81000000444
91001001447
101010010474
…后面就省略了-理论上完全可行的,略去了递归的层数。 代码如下:


Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts