博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 202. Happy Number
阅读量:5324 次
发布时间:2019-06-14

本文共 3624 字,大约阅读时间需要 12 分钟。

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

Credits:

Special thanks to  and  for adding this problem and creating all test cases.

题意:给定一个整数,判断该数是否为Happy Number。

Happy Number的定义:计算整数的各个位上数字的平方和,重复该过程,如果出现和为1,则说明该数是Happy Number。如果在一个数字圈中无限循环,且该数字圈不包括1,则说明该数不是Happy Number。

 

方法一:开始理解错题意,想成了如果一个数不是Happy Number,则会出现平方和一直为某一个值的无限循环。提交不通过后又读一遍题,发现是如果一个数不是Happy Number,则平方和会从某处开始一直循环(想想也是这个道理)。

利用Set不能存放重复元素的特性,将每次的平方和都存入set,如果某次的平方和sum在set中已经出现过,则说明已经进入循环,该数字不是一个Happy Number。
beats 18.50 % of java submissions.

public boolean isHappy(int n) {        int sum = 0, temp;        Set
set = new HashSet<>(); while(!set.contains(n)){ set.add(n); while(n != 0){ temp = n % 10; sum += temp * temp; n = n / 10; } if(sum == 1) return true; n = sum; sum = 0; } return false; }

 

方法二:将每次计算的平方和存入一个链表,利用快慢指针,如果快指针跟慢指针重逢,则说明存在一个环,该数不是Happy Number。

beats 78.05 % of java submissions.

public boolean isHappy(int n) {        int sum = 0, temp;        Node head = new Node(n);        Node fast = head, slow = head, test = head;        while(slow == test || fast.val != slow.val){
//slow == test用于排除fast和slow都指向第一个结点的初试情况 while(n != 0){ temp = n % 10; sum += temp * temp; n = n / 10; } if(sum == 1) return true; n = sum; sum = 0; head.next = new Node(n); head = head.next; if(fast.next.next != null){ fast = fast.next.next; slow = slow.next; } } return false; } class Node{ int val; Node next; public Node(int val){ this.val = val; } }

 

方法三:只是与方法二的循环结束条件不同

public boolean isHappy(int n) {        int sum = 0, temp;        Node head = new Node(n);        Node fast = head, slow = head, test = head;;        while(n != 1){            while(n != 0){                temp = n % 10;                sum += temp * temp;                n = n / 10;            }            n = sum;            sum = 0;            head.next = new Node(n);            head = head.next;            if(fast.next.next != null){                fast = fast.next.next;                slow = slow.next;            }            if(slow != test && fast.val == slow.val)                return false;        }        return true;    }

 

方法四:利用函数来取代快慢指针,参考:https://www.jianshu.com/p/f7b632e31d5f

beats 78.05 % of java submissions.

public boolean isHappy(int n){        int sum = 0;        int fast = n, slow = n;        while(sum != 1){            slow = getSum(slow);            if(slow == 1)                return true;            fast = getSum(getSum(fast));             if(fast == slow)                return false;            sum = slow;        }        return true;    }    public int getSum(int n){        int temp, sum = 0;        while(n != 0){            temp = n % 10;            sum += temp * temp;            n /= 10;        }        return sum;    }

 

转载于:https://www.cnblogs.com/zeroingToOne/p/8179148.html

你可能感兴趣的文章
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
查看>>
随手练——HDU 5015 矩阵快速幂
查看>>
启动redis一闪就关
查看>>
Maven之setting.xml配置文件详解
查看>>
ASP.NET 4.5 Web Forms and Visual Studio vs2013年入门1
查看>>
SDK目录结构
查看>>
malloc() & free()
查看>>
HDU 2063 过山车
查看>>
高精度1--加法
查看>>
String比较
查看>>
Django之Models
查看>>
CSS 透明度级别 及 背景透明
查看>>
Linux 的 date 日期的使用
查看>>
PHP zip压缩文件及解压
查看>>
SOAP web service用AFNetWorking实现请求
查看>>
Java变量类型,实例变量 与局部变量 静态变量
查看>>
mysql操作命令梳理(4)-中文乱码问题
查看>>
Python环境搭建(安装、验证与卸载)
查看>>
一个.NET通用JSON解析/构建类的实现(c#)
查看>>
Windows Phone开发(5):室内装修 转:http://blog.csdn.net/tcjiaan/article/details/7269014
查看>>