[6-10]leetcode 461. ハミング距離leetcode 226 leetcode 104 lletcode 942 leetcode 617
3101 ワード
461.漢明距離
時間:2019年5月25日10:15:16難易度:単純番号:6進度:6/5 20/52言語:python 3
構想:まず2つの数を、または、それから求めた数字の中でどれだけの1コードがありますか.
実行時間:52 ms,Hamming DistanceのPython 3コミットで84.71%のユーザメモリ消費量:13.2 MBを破り,Hamming DistanceのPython 3コミットで57.90%のユーザを破った
226.ツリーを反転
時間:2019年5月25日16:27:13難易度:単純番号:7進度:7/5 20/52言語:python 3
構想:再帰を直接利用し、pythonの小さなtrickを利用して1行のコードを節約することができる.
実行時間:52 ms、Invert Binary TreeのPython 3コミットで89.89%のユーザメモリ消費量:12.7 MBを破り、Invert Binary TreeのPython 3コミットで99.58%のユーザを破った
617.ツリーのマージ
時間:2019年5月25日16:53:08難易度:単純番号:8進度:8/5 20/52言語:python 3
考え方:最も簡単な再帰
104.ツリーの最大深さ
時間:2019年5月25日17:07:49難易度:単純番号:9進度:9/5 20/52言語:python 3タイプ:二叉樹
考え方:二叉木再帰
実行時間:72 ms、Maximum Depth of Binary TreeのPython 3コミットで56.32%のユーザメモリ消費量:14.9 MBを破り、Maximum Depth of Binary TreeのPython 3コミットで86.22%のユーザを破った
942.増減文字列マッチング
時間:2019年05月27日16:18:37難易度:単純番号:10進度:1/5 21/52言語:python 3タイプ:文字列
考え方:ダブルポインタ
実行時間:112 ms、DI String MatchのPython 3コミットで80.57%のユーザメモリ消費量:14.3 MBを破り、DI String MatchのPython 3コミットで21.85%のユーザを破った
時間:2019年5月25日10:15:16難易度:単純番号:6進度:6/5 20/52言語:python 3
構想:まず2つの数を、または、それから求めた数字の中でどれだけの1コードがありますか.
class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x^y).count('1')
実行時間:52 ms,Hamming DistanceのPython 3コミットで84.71%のユーザメモリ消費量:13.2 MBを破り,Hamming DistanceのPython 3コミットで57.90%のユーザを破った
226.ツリーを反転
時間:2019年5月25日16:27:13難易度:単純番号:7進度:7/5 20/52言語:python 3
構想:再帰を直接利用し、pythonの小さなtrickを利用して1行のコードを節約することができる.
# 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: TreeNode) -> TreeNode:
if root == None:
return
root.left,root.right = self.invertTree(root.right), self.invertTree(root.left)
return root
実行時間:52 ms、Invert Binary TreeのPython 3コミットで89.89%のユーザメモリ消費量:12.7 MBを破り、Invert Binary TreeのPython 3コミットで99.58%のユーザを破った
617.ツリーのマージ
時間:2019年5月25日16:53:08難易度:単純番号:8進度:8/5 20/52言語:python 3
考え方:最も簡単な再帰
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
if t1 == None and t2 == None:
return
root = TreeNode(0)
if t1 == None:
return t2
elif t2 == None:
return t1
else:
root.val = t1.val + t2.val
root.left = self.mergeTrees(t1.left,t2.left)
root.right = self.mergeTrees(t1.right,t2.right)
return root
104.ツリーの最大深さ
時間:2019年5月25日17:07:49難易度:単純番号:9進度:9/5 20/52言語:python 3タイプ:二叉樹
考え方:二叉木再帰
# 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: TreeNode) -> int:
if root == None:
return 0
if root.left ==None and root.right == None:
return 1
return max(0,self.maxDepth(root.left)+1,self.maxDepth(root.right)+1)
実行時間:72 ms、Maximum Depth of Binary TreeのPython 3コミットで56.32%のユーザメモリ消費量:14.9 MBを破り、Maximum Depth of Binary TreeのPython 3コミットで86.22%のユーザを破った
942.増減文字列マッチング
時間:2019年05月27日16:18:37難易度:単純番号:10進度:1/5 21/52言語:python 3タイプ:文字列
考え方:ダブルポインタ
class Solution:
def diStringMatch(self, S: str) -> List[int]:
start = 0
end = len(S)
ans = []
for each in S:
if each == 'I':
ans.append(start)
start +=1
else:
ans.append(end)
end -=1
ans.append(end)
return ans
実行時間:112 ms、DI String MatchのPython 3コミットで80.57%のユーザメモリ消費量:14.3 MBを破り、DI String MatchのPython 3コミットで21.85%のユーザを破った