LeetCode之“数学”:Happy Number

  题目链接

  题目要求:

  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 @mithmatt and @ts for adding this problem and creating all test cases.

  这道题比较需要注意的一点是要在合适的时候跳出死循环。程序如下:

 1 class Solution {
 2 public:
 3     bool isHappy(int n) {
 4         vector<int> vec;
 5         vec.push_back(n);
 6         while(n!= 1)
 7         {
 8             int sum = 0;
 9             int tmpN = n;
10             while(tmpN != 0)
11             {
12                 sum += (tmpN % 10) * (tmpN % 10);
13                 tmpN /= 10;
14             }
15             n = sum;
16
17             if(find(vec.begin(), vec.end(), n) != vec.end())
18                 break;
19             else
20                 vec.push_back(n);
21         }
22
23         return n == 1;
24     }
25 };
时间: 2024-11-09 00:53:21

LeetCode之“数学”:Happy Number的相关文章

leetcode第一刷_Valid Number

又是那种看上去非常简单,但非常难过的问题,主要是所有的测试用例很难考虑全面.下面我列举出所有的情况: 1. 字符串前面是可以含有空格的,但是全部都是空格是不行的. 2. 一个数字开头可以是哪些字符呢?很容易想到的是"+"和"-",但是别忘记还有小数点. 3. 一个数字中间也是可以含有字符的,比如科学计数法的"e",而且e之后是可以接"+"或者"-"的.但是要注意e是不是作为数字的开头或者结尾的. 4. 一个

leetcode第一刷_Single Number II

其他出现两次,只有一个出现一次的那道题我就不更了,直接抑或,最后的结果就是那个数.为什么可以这样做呢?因为一个32位int,如果所有数都出现了两次,那么为1的那些位统计的个数一定是2的倍数,抑或之后全变成0.一个数出现了一次,它为1的那些位上,1的个数必定是奇数,抑或之后一定还是1. 我之前知道出现两次这个题的解法,但是理解的不够深,以为抑或是关键,其实不是,出现了偶数次才是关键.理解了这点,推广到出现3次上,如果所有的出现了三次,那么为1的那些位1的个数一定是三的倍数,那如果有一个数出现了一次

Leetcode 位运算 Single Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Single Number Total Accepted: 20063 Total Submissions: 44658 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find >the two elements that a

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5]. Note: The or

leetcode Find the Duplicate Number

题目连接 https://leetcode.com/problems/find-the-duplicate-number/ Find the Duplicate Number Description Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist.

LeetCode 笔记26 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si

LeetCode 414. Third Maximum Number (第三大的数)

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). Example 1: Input: [3, 2, 1] Output: 1 Explanation: The third maximum is 1. Examp

【leetcode】Valid Triangle Number

题目: Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: Input: [2,2,3,4] Output: 3 Explanation: Valid c