LeetCode--Happy Number、Power of Two、 Add Digits、Ugly Number

202. 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.

编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。

思路:

使用一个求余和int保留整数的小技巧,取平方和的每个位置上的数字。新建一个list,每次求和的结果放进去。每求一次和判断是否等于1,等于1的话True。不等于1的话在list里检索是否有重复的值。有的话False。

class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        dict=[]
        while True:
            sum=0
            while n>0:
                sum+=(n%10)**2
                n=int(n//10)
            n=sum
            if sum==1:
                return True
            elif sum in dict:
                return False
            else:
                dict.append(sum)

231. Power of Two

Given an integer, write a function to determine if it is a power of two.

给定一个整数,编写一个函数来判断它是否是 2 的幂次方。

思路:比较直观暴力的想法是,如果每次将这个数除以2没有余数,直到得到数字1,那么这个数就是2的若干次幂。

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n<1:
            return False
        while n%2==0:
            n/=2
        if n==1:
            return True
        else:
            return False

258. Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。

class Solution(object):
    def addDigits(self, num):
        """
        :type num: int
        :rtype: int
        """
        while num>9:
            s=0
            while num>=1:
                s+=num%10
                num/=10
            num=s
        return num

263. Ugly Number

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

Example 1:

Input: 6
Output: true
Explanation: 6 = 2 × 3

Example 2:

Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2

Example 3:

Input: 14
Output: false 
Explanation: 14 is not ugly since it includes another prime factor 7

编写一个程序判断给定的数是否为丑数。

丑数就是只包含质因数 2, 3, 5 的正整数

思路:依次将所给的数除以 2,3,5 直至无法除尽,如果这时得到1则说明所给的数的质因子不超出2,3,5三个数,否则说明有其他质因数。

class Solution(object):
    def isUgly(self, num):
        """
        :type num: int
        :rtype: bool
        """
        if num<=0:
            return False
        for i in [2,3,5]:
            while num%i==0:
                num/=i
        if num==1:
            return True
        else:
            return False

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值