53. The maximum and subsequence
Given the nums an array of integers, and find a maximum of the successive sub-array (sub-array comprising a minimum element) having its maximum and returns.
Example:
Input: [-2,1, -3,4, -1,2,1, -5,4],
output: 6
Explanation: continuous subarray [4, -1,2,1], and the maximum was 6 .
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/maximum-subarray
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
class Solution:
def maxSubArray(self, nums):
if max(nums) < 0:
return nums
local_max, global_max = 0, 0
for num in nums:
local_max = max(0, local_max + num)
global_max = max(global_max, local_max)
return global_max
69. x square root
Achieve int sqrt (int x) function.
Computes and returns the square root of x, where x is a non-negative integer.
Since the return type is an integer, the integer part of the result to retain only the fractional part is rounded down.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Description: 8 is the square root of 2.82842 ...,
since the return type is an integer, the fractional part is rounded down.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/sqrtx
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
class Solution:
def mySqrt(self, x: int) -> int:
if x < 2:
return x
left, right = 1, x // 2
while left <= right:
mid = left + (right - left) // 2
if mid > x / mid:
right = mid - 1
else:
left = mid + 1
return left - 1
151. flip words in strings
Given a string, each individually flip word string.
Example 1:
Input: "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: "hello world!"
Output: "! World hello"
Explanation: The input string may include extra spaces in front or back, but can not undertake the reversed character
class Solution:
def reverseWords(self, s):
if s == "":
return s
ls = s.split()
if ls == []:
return ""
result = ""
for i in range(0, len(ls)-1):
result += ls[len(ls)-1-i] + " "
result += ls[0]
return result
include.
Example 3:
Input: "a good example"
Output: "example good a"
explanation: If there are two extra spaces between words, to reduce the space between the word inversion to only containing one.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/reverse-words-in-a-string
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
100. The same book
Given two binary tree, write a function to check whether they are identical.
If the two are identical in structure tree, and the nodes have the same value, they are considered identical.
Example 1:
Input: 11
/ \ / \
2323
[1,2,3], [1,2,3]
Output: true
Example 2:
Input: 11
/ \
22
[1,2], [1,null,2]
Output: false
Example 3:
Input: 11
/ \ / \
2112
[1,2,1], [1,1,2]
Output: false
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/same-tree
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if p is None and q is None:
return True
if p is not None and q is not None:
return p.val == q.val and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)