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
public class Solution { public boolean isHappy(int n) { HashSet<Integer> hashSet = new HashSet<>(); while (n != 1) { if (!hashSet.add(n)) { return false; } char[] charArray = String.valueOf(n).toCharArray(); int sum = 0; for (char c : charArray) { sum += Math.pow(Integer.valueOf(c + ""), 2); } n = sum; } return true; } }
相关推荐
leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的...Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
Elements 力扣 206 反转链表 | Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design ...
关于algorithm and data structure的一个linked list的C++的code
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
creat() linked list;print(),output linked list;insert(),input index, before data(index)insert new data,if index >c,insert into the end. delede(),delete data(index),0;change(),input index_i and index_...
用linked list来写priority queue
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→...
a very comprehensive tutorial on circular linked list
We introduce a method to dynamically construct highly concurrent linked lists on modern graphics processors. Once constructed, these data structures can be used to implement a host of algorithms ...
该代码的主要功能是根据双向链表中的数据大小排序,排序方式是选择法和冒泡法两种,其中选择法是用交换指针来实现的。
THIS IS DOUBLY LINKED LIST SOURCE
在https://github.com/gramoli/synchrobench上进行修改更适于Synchrobench’s Lazy Linked-List进行内存回收,解决了内存泄漏问题
this is a template design for doubly linked list. it is just a reference for one who want to study template programming.
C doubly linked list
java 内置迭代器的linked list例题
Linked List Template连接列表模板
infix to postfix using linked list
在linux环境下已测试,放心使用,这个是根据stanford cs library的linked list文章里的代码敲出来的
In the module entry point, create a linked list containing five struct birthday elements. Traverse the linked list and output its contents to the kernel log buffer. Invoke the dmesg command to ensure ...