python 类内函数互调的两种用法
来源:互联网 发布:java变成学到什么程度 编辑:程序博客网 时间:2024/06/01 10:51
以leetcode 279题 Perfect Squares为例
题目:
第一种:
在之前加类名:
class Solution(object):
import math
def is_square(self,n):
s=int(math.sqrt(n))
return s*s==n
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
if Solution.is_square(self,n)==True:
return 1
while n%4==0:
n=n/4
if n%8==7:
return 4
i=1
while i*i<n:
if Solution.is_square(self,n-i*i)==True:
return 2
i+=1
return 3
用时52ms,其中is_square函数的定义和调用参数都有self
第二种,之前加self:
class Solution(object):
import math
def is_square(self,n):
s=int(math.sqrt(n))
return s*s==n
def numSquares(self, n):
"""
:type n: int
:rtype: int
"""
if self.is_square(n)==True:
return 1
while n%4==0:
n=n/4
if n%8==7:
return 4
i=1
while i*i<n:
if self.is_square(n-i*i)==True:
return 2
i+=1
return 3
用时39ms
- python 类内函数互调的两种用法
- Python内建函数用法!
- Python内建函数用法!
- Java匿名内部类的两种经典用法
- 【转载】TRUNC函数的两种用法
- Linux函数getopt_long_only的两种用法
- python的内建函数
- Python 的内建函数
- python的内建函数
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- jQuery
- Java 8 习惯用语,第 7 部分 函数接口
- 欢迎使用CSDN-markdown编辑器
- Hibernate、Mybatis的作用和区别
- Problem2:addTwoNumbers
- python 类内函数互调的两种用法
- 传送门
- Android学习笔记之资源访问
- 网站维护组姓名201X.XX.XX-201X.XX.XX
- Web 通信 之 长连接、长轮询(long polling)
- ServerSideIncludeModule不是可识别的本机模块
- 快速在Excel2007中输入以0开头的数字
- java excel读取的时候手机号码变成了数值类型的值,怎么防止这种问题
- 大数据WEB阶段(十三)JSP(二)JSP标签、EL表达式、JSTL