leetcood学习笔记-202-快乐数】的更多相关文章

题目描述: 方法一:比较笨的办法,根据题意,如果变成1返回True,如果出现重复返回False 看到下面有位朋友用的是dict,我用了list,两个都跑了一下似乎list快一点? class Solution: def isHappy(self, n: int) -> bool: l = [] while True: l.append(n) n = sum(int(i)**2 for i in str(n)) if n == 1:#n==1 和 n in l 的判断顺序不能相反 如:输入1 re…
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - Leetcode 202. 快乐数 - 题解 Leetcode 202.Happy Number 在线提交: https://leetcode-cn.com/problems/happy-number/ 或 LintCode 488 https://www.lintcode.com/problem/h…
202. 快乐数 编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. 示例: 输入: 19 输出: true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 PS: 这题可以用快慢指针的思想去做,有点类似于检…
写作背景 笔者前年开始撰写的<Node.js学习笔记> github star 数突破了1000,算是个里程碑吧. 从第一次提交(2016.11.03)到现在,1年半过去了.突然有些感慨,想要写点东西,谈谈这期间的收获.心路历程,以及如何学习Node.js. 心路历程 笔者一直有做技术笔记的习惯,前几年零零散散的也写了不少Node.js的东西,只不过都存在evernote里.写着写着,觉得有必要系统地整理下,于是就有了这个项目. 粗略统计了下,总共提交了约60篇教程,以及将近300个范例脚本.…
编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. 示例: 输入: 19 输出: true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 思路 可以将数字n转换为字符串遍历一遍求出每位数字的平方,然后加起来…
编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. 示例: 输入: 19 输出: true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 思路1 经测试,如果一个数不是快乐数,那么至多经过八次就会进入循环.…
#python学习笔记 17/07/10 # !/usr/bin/evn python3 # -*- coding:utf-8 -*- import math #函数 函数的 定义 #定义一个求绝对值函数 def abstruct(a): if not isinstance (a, (int, float)): raise TypeError("param must be a int or float type") if a >= 0: return a else: return…
题目: 编写一个算法来判断一个数是不是 "快乐数". 一个 "快乐数" 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. Write an algorithm to determine if a number is "happy". A happy number is a number defined by the…
编写一个算法来判断一个数是不是“快乐数”. 一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1.如果可以变为 1,那么这个数就是快乐数. 示例: 输入: 19 输出: true 解释: 1^2 + 9^2 = 82 8^2 + 2^2 = 68 6^2 + 8^2 = 100 1^2 + 0^2 + 0^2 = 1 public boolean isHappy(int n) { Set<Inte…
快乐数 编写一个算法来判断一个数 n 是不是快乐数. 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1.如果 可以变为 1,那么这个数就是快乐数. 如果 n 是快乐数就返回 True :不是,则返回 False . 示例: 输入:19 输出:true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1 题解思路 面向测试用例…
题目描述: 第一次提交: class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ a = 0 b = len(numbers)-1 while a != b: sum = numbers[a] + numbers[b] if sum > tar…
题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: cur_l1 = l1 cur_l2 = l2 cur = ListNode(0) he…
---恢复内容开始--- 题目描述: 第一次提交: class Solution: def countAndSay(self, n: int) -> str: f = " for i in range(n-1): count = 0 c = '' for j in range(len(f)): if j == 0 or (j - 1 > -1 and f[j]==f[j-1]): count += 1 else: c += str(count) + f[j - 1] count =…
 联合学习 Android 异步消息处理机制 让你深入理解 Looper.Handler.Message三者关系   1. 首先我们通过一个实例案例来引出一个异常: (1)布局文件activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" a…
python字符串与列表的相互转换   学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 str1 = "hi hello world" print(str1.split(" "))输出:['hi', 'hello', 'world'] 2. 列表转字符串 l = ["hi","hello","world"] print(" ".join(l))输出:hi hello…
笔记: python if not   判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法. 使用if not x这种写法的前提是:必须清楚x等于None,  False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()时对你的判断没有影响才行 链接:https://www.cnblogs.com/chenya/p/4218761.…
题目描述: 第一次提交:(会超时) class Solution: def mySqrt(self, x: int) -> int: if x==0 or x==1: return x for i in range(1,x): if i*i<=x and (i+1)**2>x: return i 方法二:牛顿迭代法(最优解)(泰勒展开式) class Solution: def mySqrt(self, x): """ :type x: int :rtype…
题目描述: 第一次提交: class Solution: def addBinary(self, a: str, b: str) -> str: list_a,list_b=[],[] for s in a: list_a.append(int(s)) for s in b: list_b.append(int(s)) if len(list_a)>=len(list_b): for i in range(len(list_a)-len(list_b)): list_b.insert(0,0)…
题目描述: 第一次提交: class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: j,x = 0,1 l = [] if matrix==[]: return [] m = len(matrix) n = len(matrix[0]) while x<=n*m: for i in range(j,n-j): l.append(matrix[j][i]) x += 1 for i in range(…
题目描述: 第一次提交;(超时): class Solution: def countPrimes(self, n: int) -> int: count = 0 for i in range(2,n): for j in range(2,i+1): if i%j == 0 and j!=i: break if j==i: count+=1 return count 别人家的: 这题搜到一个非常牛逼的算法,叫做厄拉多塞筛法. 比如说求20以内质数的个数,首先0,1不是质数.2是第一个质数,然后把…
题目描述: 方法:不断除以 5, 是因为每间隔 5 个数有一个数可以被 5 整除, 然后在这些可被 5 整除的数中, 每间隔 5 个数又有一个可以被 25 整除, 故要再除一次, ... 直到结果为 0, 表示没有能继续被 5 整除的数了 class Solution: def trailingZeroes(self, n: int) -> int: if n == 0: return 0 else: return int(n//5)+self.trailingZeroes(int(n/5))…
思想: 对输入数据 把每个位数平方求和 得到结果如果是1 就返回真 否则 对这个结果递归 啥时候事后返回假: 返回假 说明进入无限循环了. 啥时候会无限循环? 某一次求平方和的结果,之前得到过这个结果,那就会无限循环了. 所以,我把每次得到的结果 都存起来,如果后面发现某一次得到结果 在之前得到过,那就肯定无限循环了 返回假 class Solution: def isHappy(self, n): """ :type n: int :rtype: bool "&q…
1. 接下来我们细讲分析一下前面一讲中,c功能实现的代码: (1)hello.c : #include <jni.h> char* getHello() { //////// return "hello Android From C"; } //jni协议给java调用 // 返回类型 方法名(Java_包名(使用"_"把包名分割)_类名_方法名 (参数) //JNIEnv* env jni的结构体数据,jni系统实现 ; obj java对象 调用该j…
题目: 第一次提交; class Solution: def searchInsert(self, nums: List[int], target: int) -> int: for i in range(len(nums)): if nums[i] >= target : return i if i == (len(nums)-1) and nums[i]<target: return i+1 法二: class Solution: def searchInsert(self, num…
题目: 第一次提交: class Solution: def strStr(self, haystack: str, needle: str) -> int: if not len(needle): return 0 for i in range(len(haystack)): if i+len(needle)<=len(haystack): if haystack[i:(i+len(needle))]==needle: return i return -1 方法二: Sunday 平均O(N…
题目: 第一次提交: class Solution: def removeElement(self, nums, val: int) -> int: for i in range(len(nums)-1,-1, -1):#此处中间为range(,中间值为-1,) if nums[i] == val: nums.remove(nums[i])#或nums.pop(i) return len(nums) 方法二:正序 class Solution: def removeElement(self, n…
题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums) -> int: for i in range(len(nums)-1,0,-1):#注意要倒序** if nums[i]==nums[i-1]: del(nums[i]) return len(nums) 另: class Solution: def removeDuplicates(self, nums: List[int]) -> int: i = 0 for num…
题目描述: 方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = ListNode(None) node = res while l1 and…
错误记录 class Solution: def romanToInt(self, s: str) -> int: d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} r=0 for i in range(len(s)): if d[s[i]]<d[s[i+1]] and i<len(s)-1: r-=d[s(i)] else: r+=d[s(i)] return r 会报错:TypeError: 'str' object i…
题目描述 方法一:转换为字符串 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False else: y=str(x)[::-1] return y==str(x) 方法二;数字反转 class Solution: def isPalindrome(self, x: int) -> bool: if x<0: return False a,res=x,0 while x: x,mod =…