771. 宝石与石头
题目:
给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。
解答:
class Solution:
def numJewelsInStones(self, J, S):
count=0
for i in S:
index=list(J)
if i in J:
count+=1
return count
709. 转换成小写字母
题目:
实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
解答:
class Solution:
def toLowerCase(self, str):
return str.lower()
905. 按奇偶校验排序数组
题目:
给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素。
你可以返回满足此条件的任何数组作为答案。
解答:
class Solution:
def sortArrayByParity(self, A):
a=[]
b=[]
for i in A:
if i%2==0:
a.append(i)
else:
b.append(i)
A=a+b
return A
461. 汉明距离###
题目:
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
解答:
class Solution:
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
c=list(bin(x^y))[2::]
return sum(list(map(int,c)))
617. 合并二叉树
题目:
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。
你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。
解答:
class Solution:
def mergeTrees(self, t1, t2):
"""
:type t1: TreeNode
:type t2: TreeNode
:rtype: TreeNode
"""
def concat(t1,t2,new):
if t1.left!=None and t2.left!=None:
new.left=TreeNode(t1.left.val+t2.left.val)
concat(t1.left,t2.left,new.left)
elif t1.left==None and t2.left!=None:
new.left=t2.left
elif t1.left!=None and t2.left==None:
new.left=t1.left
else:
new.left=None
if t1.right!=None and t2.right!=None:
new.right=TreeNode(t1.right.val+t2.right.val)
concat(t1.right,t2.right,new.right)
elif t1.right==None and t2.right!=None:
new.right=t2.right
elif t1.right!=None and t2.right==None:
new.right=t1.right
else:
new.right=None
if t1==None:
return t2
if t2==None:
return t1
head=TreeNode(t1.val+t2.val)
concat(t1,t2,head)
return head
832. 翻转图像
题目:
给定一个二进制矩阵 A,我们想先水平翻转图像,然后反转图像并返回结果。
水平翻转图片就是将图片的每一行都进行翻转,即逆序。例如,水平翻转 [1, 1, 0] 的结果是 [0, 1, 1]。
反转图片的意思是图片中的 0 全部被 1 替换, 1 全部被 0 替换。例如,反转 [0, 1, 1] 的结果是 [1, 0, 0]。
解答:
class Solution:
def flipAndInvertImage(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
new=[]
part=[1]
for i in A:
i=i[::-1]
new.append([j^1 for j in i])
return new
104. 二叉树的最大深度###
题目:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
解答:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root==None: return 0
left_max=self.maxDepth(root.left)+1
right_max=self.maxDepth(root.right)+1
return max(left_max,right_max)
def max(a,b):
if a>b:return a
else:return b
657. 机器人能否返回原点###
题目:
在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。
移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
解答:
class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
a=[0,0]
for i in list(moves):
if i=='R':
a[0]+=1
if i=='L':
a[0]-=1
if i=='U':
a[1]+=1
if i=='D':
a[1]-=1
if a[0]==0 and a[1]==0:
return bool(1)
else:
return bool(0)
804. 唯一摩尔斯密码词
题目:
国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: “a” 对应 “.-”, “b” 对应 “-…”, “c” 对应 “-.-.”, 等等。
解答:
class Solution:
def uniqueMorseRepresentations(self, words):
"""
:type words: List[str]
:rtype: int
"""
index=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
result=[]
for i in words:
ever_str=[]
for j in i:
k=ord(j)-ord('a')
ever_str.append(index[k])
result.append(''.join(ever_str))
result=set(result)
return len(result)
852. 山脉数组的峰顶索引
题目:
我们把符合下列属性的数组 A 称作山脉:
A.length >= 3
存在 0 < i < A.length - 1 使得A[0] < A[1] < … A[i-1] < A[i] > A[i+1] > … > A[A.length - 1]
给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < … A[i-1] < A[i] > A[i+1] > … > A[A.length - 1] 的 i 的值。
解答:
class Solution:
def peakIndexInMountainArray(self, A):
"""
:type A: List[int]
:rtype: int
"""
return A.index(max(A))
226. 翻转二叉树
题目:
翻转一棵二叉树。
解答:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root==None:
return root
def reverse(root,new):
if root.left!=None:
new.right=TreeNode(root.left.val)
reverse(root.left,new.right)
if root.right!=None:
new.left=TreeNode(root.right.val)
reverse(root.right,new.left)
return new
new=TreeNode(root.val)
reverse(root,new)
return new
476. 数字的补数
题目:
给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。
解答:
class Solution:
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
if num>0:
return (num^(2**(len(bin(num))-2)-1))
344. 反转字符串
题目:
编写一个函数,其作用是将输入的字符串反转过来。
解答:
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]
728. 自除数
题目:
自除数 是指可以被它包含的每一位数除尽的数。
例如,128 是一个自除数,因为 128 % 1 == 0,128 % 2 == 0,128 % 8 == 0。
还有,自除数不允许包含 0 。
给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数。
解答:
class Solution:
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
result=[]
for i in range(left,right+1):
if str(i).find('0')!=-1:
continue
index=list(str(i))
flag=1
for j in index:
if i%int(j)!=0:
flag=0
break
if flag==1:
result.append(i)
return result
258. 各位相加
题目:
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数
解答:
class Solution:
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
result=0
for i in str(num):
result+=int(i)
while len(str(result))>1:
k=0
for i in str(result):
k+=int(i)
result=k
return result
867. 转置矩阵
题目:
给定一个矩阵 A, 返回 A 的转置矩阵。
矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引。
解答:
class Solution:
def transpose(self, A):
"""
:type A: List[List[int]]
:rtype: List[List[int]]
"""
result=[]
for i in range(len(A[0])):
new=[]
for j in range(len(A)):
new.append(A[j][i])
result.append(new)
return result
292. Nim游戏
题目:
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。
你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
解答:
class Solution:
def canWinNim(self, n):
"""
:type n: int
:rtype: bool
"""
if n%4==0:
return bool(0)
else:
return bool(1)
171. Excel表列序号
题目:
给定一个Excel表格中的列名称,返回其相应的列序号。
解答:
class Solution:
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
s=s[::-1]
score=0
for k,ch in enumerate(s):
score+=(ord(ch)-ord('A')+1)*(26**k)
return score
821. 字符的最短距离
题目:
给定一个字符串 S 和一个字符 C。返回一个代表字符串 S 中每个字符到字符串 S 中的字符 C 的最短距离的数组。
解答:
class Solution(object):
def shortestToChar(self, S, C):
“”"
:type S: str
:type C: str
:rtype: List[int]
“”"
index=[]
beg=0
while(S.find(C,beg)!=-1):
index.append(S.find(C,beg))
beg = S.find(C, beg) + 1
result=[]
for i,_ in enumerate(S):
part=[]
for j in index:
part.append(abs(i-j))
result.append(min(part))
return result
559. N叉树的最大深度
题目:
给定一个N叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
这道题引用了网上大神的程序,链接
解答:
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, children):
self.val = val
self.children = children
"""
class Solution(object):
def maxDepth(self, root):
"""
:type root: Node
:rtype: int
"""
if not root:
return 0
if not root.children:
return 1
return 1+max(self.maxDepth(child) for child in root.children)
237. 删除链表中的节点
题目:
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
解答:
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val=node.next.val
node.next=node.next.next
463. 岛屿的周长
题目:
给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。
解答:
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
result=0
indexs=[[1,0], [0,1], [-1,0], [0,-1]]
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j]==1:
result+=4
for index in indexs:
x=i+index[0]
y=j+index[1]
if x>=0 and y>=0 and x<len(grid) and y<len(grid[0]):
if grid[x][y]==1:
result-=1
return result
371. 两整数之和
题目:
不使用运算符 + 和 - ,计算两整数 a 、b 之和。
解答:
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
mod=a^b
next=(a&b)<<1
return sum([mod,next])
806. 写字符串需要的行数
题目:
我们要把给定的字符串 S 从左到右写到每一行上,每一行的最大宽度为100个单位,如果我们在写某个字母的时候会使这行超过了100 个单位,那么我们应该把这个字母写到下一行。我们给定了一个数组 widths ,这个数组 widths[0] 代表 ‘a’ 需要的单位, widths[1] 代表 ‘b’ 需要的单位,…, widths[25] 代表 ‘z’ 需要的单位。
解答:
class Solution(object):
def numberOfLines(self, widths, S):
"""
:type widths: List[int]
:type S: str
:rtype: List[int]
"""
numbers=[]
for i in S:
numbers.append(widths[ord(i)-ord('a')])
lenth=1
remain=0
for number in numbers:
remain+=number
if remain>=100:
lenth+=1
if remain==100:
remain=0
else:
remain=number
return [lenth,remain]
922. 按奇偶排序数组 II
题目:
给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
你可以返回任何满足上述条件的数组作为答案。
解答:
class Solution(object):
def sortArrayByParityII(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
oven_list=[]
odd_list=[]
result=[]
for i in A:
if i%2==0:
oven_list.append(i)
else:
odd_list.append(i)
for i in range(len(oven_list)):
result.append(oven_list[i])
result.append(odd_list[i])
return result
693. 交替位二进制数
题目:
给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等
解答:
class Solution(object):
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
bin_number=list(bin(n))[2::]
for i in range(len(bin_number)-1):
if bin_number[i]==bin_number[i+1]:
return bool(0)
return bool(1)
题目:
解答:
题目:
解答:
题目:
解答:
题目:
解答:
题目:
解答:
题目:
解答:
题目:
解答: