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; } }
相关推荐
python python_leetcode题解之203_Remove_Linked_List_Elements.py
leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的...Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
27. **Remove Linked List Elements**:移除链表中的特定元素。可以使用迭代方法,同时保持指向下一个非目标节点的引用。 28. **Remove Element**:从数组中移除特定元素。双指针法可以有效地完成这个任务。 29. *...
Elements 力扣 206 反转链表 | Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design ...
链表的基本类型包括单向链表(Singly Linked List)和双向链表(Doubly Linked List)。在单向链表中,每个节点只包含一个指向其后继节点的指针;而在双向链表中,除了包含指向后继节点的指针外,每个节点还包含一个...
关于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_...
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→...
用linked list来写priority queue
《Lazy Linked-List:一种高效的内存管理策略》 在计算机科学中,数据结构是构建算法的基础,而链表作为其中的一员,因其灵活的插入和删除操作,在许多应用场景中都有着广泛的应用。本文将深入探讨一种特殊的链表...
在给定的"double_linked_list.rar"文件中,包含了使用双向链表进行排序的实现。排序的目标是将链表中的元素按照数值大小进行排列。这里提到了两种排序算法:选择法和冒泡法。 1. **选择法排序**: 选择法排序是一...
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 ...
相比之下,单链表(Linked List)是一种线性数据结构,其中每个节点包含数据和指向下一个节点的引用。单链表不支持随机访问,但插入和删除操作通常比数组更快,因为无需移动后续元素。 4. **单链表操作** 在C#中,...
this is a template design for doubly linked list. it is just a reference for one who want to study template programming.
"Lab10_doublylinkedlist_" 这个标题暗示我们讨论的是关于链表数据结构的一个实验或练习,具体是双链表(Doubly Linked List)。双链表是一种线性数据结构,每个节点不仅包含数据,还包含两个指针,分别指向其前一个...
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 ...
"前端大厂最新面试题-linked-list.docx" 本文档主要涵盖了链表(Linked List)相关的知识点,旨在帮助读者更好地理解链表的实现、操作和应用。 1. 环形链表(Ring Linked List) 环形链表是一种特殊的链表结构,...