(303)给定一个整数数组 nums,求出数组从索引 i到 j(i ≤ j) 范围内元素的总和,包含 i, j两点。

示例:

给定 nums = [-2, 0, 3, -5, 2, -1],求和函数为 sumRange()

sumRange(0, 2) -> 1

sumRange(2, 5) -> -1

sumRange(0, 5) -> -3

答:

class NumArray(object):

def __init__(self, nums):

"""

:type nums: List[int]

"""

self.nums=nums

def sumRange(self, i, j):

"""

:type i: int

:type j: int

:rtype: int

"""

sums=sum(self.nums[i:j+1])

return sums

# Your NumArray object will be instantiated and called as such:

# obj = NumArray(nums)

# param_1 = obj.sumRange(i,j)

(326)给定一个整数,写一个函数来判断它是否是 3 的幂次方。

示例 1:

输入: 27

输出: true

示例 2:

输入: 0

输出: false

示例 3:

输入: 9

输出: true

示例 4:

输入: 45

输出: false

答:

class Solution:

def isPowerOfThree(self, n):

"""

:type n: int

:rtype: bool

"""

while n>=3:

n=n/3

return n==1.0

(342)给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。

示例 1:

输入: 16

输出: true

示例 2:

输入: 5

输出: false

答:

class Solution:

def isPowerOfFour(self, num):

"""

:type num: int

:rtype: bool

"""

while num>=4:

num/=4

return num==1.0

(344)编写一个函数,其作用是将输入的字符串反转过来。

示例 1:

输入: "hello"

输出: "olleh"

示例 2:

输入: "A man, a plan, a canal: Panama"

输出: "amanaP :lanac a ,nalp a ,nam A"

答:

class Solution:

def reverseString(self, s):

"""

:type s: str

:rtype: str

"""

return s[::-1]

(345)编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"

输出: "holle"

示例 2:

输入: "leetcode"

输出: "leotcede"

答:

class Solution:

def reverseVowels(self, a):

"""

:type s: str

:rtype: str

"""

b=[]

c=''

for i in a:

if i in "aeiouAEIOU":

b.append(i)

for i in a:

if i in "aeiouAEIOU":

c+=b.pop()

else:

c+=i

return c

(349)给定两个数组,编写一个函数来计算它们的交集。

示例 1:

输入: nums1 = [1,2,2,1], nums2 = [2,2]

输出: [2]

示例 2:

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

输出: [9,4]

答:

class Solution:

def intersection(self, nums1, nums2):

"""

:type nums1: List[int]

:type nums2: List[int]

:rtype: List[int]

"""

a=[]

nums1=set(nums1)

nums2=set(nums2)

if len(nums1)>=len(nums2):

for i in nums2:

if i in nums1:

a.append(i)

if len(nums2)>len(nums1):

for i in nums1:

if i in nums2:

a.append(i)

return a

(350)给定两个数组,编写一个函数来计算它们的交集。

示例 1:

输入: nums1 = [1,2,2,1], nums2 = [2,2]

输出: [2,2]

示例 2:

输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

输出: [4,9]

答:

class Solution:

def intersect(self, nums1, nums2):

"""

:type nums1: List[int]

:type nums2: List[int]

:rtype: List[int]

"""

res = []

for k in nums1:

if k in nums2:

res.append(k)

nums2.remove(k)

return res

(367)给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。

注意:不要使用任何内置的库函数,如 sqrt。

示例 1:

输入: 16

输出: True

示例 2:

输入: 14

输出: False

答:

class Solution:

def isPerfectSquare(self, n):

"""

:type num: int

:rtype: bool

"""

if n**0.5-int(n**0.5)==0:

return True

else:

return False

(371)不使用运算符 + 和-,计算两整数a 、b之和。

示例:

若 a = 1 ,b = 2,返回 3。

答:

class Solution:

def getSum(self, a, b):

"""

:type a: int

:type b: int

:rtype: int

"""

return sum([a,b])

(374)我们正在玩一个猜数字游戏。 游戏规则如下:

我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。

每次你猜错了,我会告诉你这个数字是大了还是小了。

你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0):

-1 : 我的数字比较小

1 : 我的数字比较大

0 : 恭喜!你猜对了!

示例:

n = 10, 我选择 6.

返回 6.

答:

# The guess API is already defined for you.

# @param num, your guess

# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0

# def guess(num):

class Solution(object):

def guessNumber(self, n):

"""

:type n: int

:rtype: int

"""

left=0

right=n

while left<=right:

middle=(left+right)//2

num=guess(middle)

if num==-1:

right=middle-1

if num==0:

return middle

if num==1:

left=middle+1

(383)给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)

注意:

你可以假设两个字符串均只含有小写字母。

canConstruct("a", "b") -> false

canConstruct("aa", "ab") -> false

canConstruct("aa", "aab") -> true

答:

class Solution(object):

def canConstruct(self, ransomNote, magazine):

"""

:type ransomNote: str

:type magazine: str

:rtype: bool

"""

for i in ransomNote:

if i in magazine:

magazine = magazine.replace(i,'',1)

else:

return False

return True

(387)给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

案例:

s = "leetcode"

返回 0.

s = "loveleetcode",

返回 2.

答:(超时)

class Solution:

def firstUniqChar(self, s):

"""

:type s: str

:rtype: int

"""

for i in s:

if s.count(i)==1:

return s.index(i)

return -1

答:

class Solution:

def firstUniqChar(self, s):

"""

:type s: str

:rtype: int

"""

dic=collections.Counter(s)

for i in range(len(s)):

if dic[s[i]]==1:

return i

return -1

(389)给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:

s = "abcd"

t = "abcde"

输出:

e

解释:

'e' 是那个被添加的字母。

答:

class Solution:

def findTheDifference(self, s, t):

"""

:type s: str

:type t: str

:rtype: str

"""

dic=collections.Counter(s)

di=collections.Counter(t)

for i in di:

if dic[i]!=di[i]:

return i

python输入hello输出olleh_leetcode上的python练习(6)相关推荐

  1. python输入生日输出生肖_用Python输入年月日 输出X年X月X日生肖是XX,xx岁的XX星座...

    # -*- coding: utf-8 -*- import datetime time1 = datetime.datetime.now().strftime('%Y') time2 = datet ...

  2. python3中文手册-Python 输入和输出

    Python 输入和输出 在前面几个章节中,我们其实已经接触了 Python 的输入输出的功能.本章节我们将具体介绍 Python 的输入输出. 输出格式美化 Python两种输出值的方式: 表达式语 ...

  3. [转载] Python输入,输出,Python导入

    参考链接: Python输入,输出和导入 In this tutorial we will cover the basics of Python Input/Output and also how t ...

  4. Python输入,输出,Python导入

    In this tutorial we will cover the basics of Python Input/Output and also how to import a module in ...

  5. python中倒着输出输入值_十五、深入Python输入和输出

    「@Author:By Runsen」 在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以使用input和print语句来完成这些功能. in ...

  6. Python 输入和输出

    输出 用print加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print 'hello, world' print语句也 ...

  7. python输入输出-python输入与输出

    python输出 python3中的输出 python3中的输出使用函数print(),示例如下: >>> print('hello kitty') print()也可接受多个参数, ...

  8. [Python]输入与输出

    1. 读取命令行选项 Python启动时,命令行选项放置在列表sys.argv中.例如: import sys if len(sys.argv) != 3:sys.stderr.write(" ...

  9. Python --- 输入、输出、运算符

    目录 前言: 一.输入--input()函数 二.输出 1.平平无奇的print 1)种瓜得瓜,种豆得豆 2)print() 默认以换行结束 3)一个 print() 输出多项内容,则需要在输入时每项 ...

最新文章

  1. 报名 | 2019年第六届清华大学大数据社会科学讲习班
  2. 使用mac pro电脑当tomcat端口被占用怎么解决?
  3. Android system server之WatchDog看门狗分析
  4. object-c html,object.html
  5. nhibernate处理多数据库
  6. jozj4010-我才不是萝莉控呢【哈夫曼树】
  7. 春春幼儿园堆积木大赛_春云边车
  8. Hive谓词解析过程分析
  9. autojs 云控_autojs websocket 核心示例代码,云控技术
  10. 看美文,记单词(6)
  11. strlensizeof
  12. 使用jQuery加载js脚本
  13. Golang 五种原子性操作的用法详解
  14. html页面保存到本地文件路径,js上传文件到指定路径 jQuery或者js保存文件到本地...
  15. 微信小程序怎么做店铺?
  16. 【论文解读】MacBERT: 中文自然语言预训练模型
  17. 日行一小步,坚持一大步
  18. linux 使用了哪个存储阵列卡,Linux中RAID概述及配置实验
  19. 七日年化收益率怎么算「知识普及」
  20. 铁威马远程samba服务器稳定,NAS网络存储的Samba访问

热门文章

  1. 逆向工程核心原理学习笔记(九):小端序标记法2
  2. setsockopt函数全面解析
  3. cocos2d-x游戏实例(15)-纵版射击游戏(2)
  4. 多进程和多线程的优缺点
  5. 重叠I/O之事件对象通知
  6. 灵魂画手:漫画图解 SSH
  7. python中的Xpath方法总结
  8. 圣诞前夜预告|深入理解Linux内核经验分享
  9. 大话ion系列(二)
  10. LiveVideoStack 2020 年度盘点