zoukankan      html  css  js  c++  java
  • Leetcode 53,69,151,100

    53.最大子序和

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

    示例:

    输入: [-2,1,-3,4,-1,2,1,-5,4],
    输出: 6
    解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/maximum-subarray
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    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 的平方根

    实现 int sqrt(int x) 函数。

    计算并返回 x 的平方根,其中 x 是非负整数。

    由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

    示例 1:

    输入: 4
    输出: 2
    示例 2:

    输入: 8
    输出: 2
    说明: 8 的平方根是 2.82842...,
      由于返回类型是整数,小数部分将被舍去。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/sqrtx
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    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. 翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词。

    示例 1:

    输入: "the sky is blue"
    输出: "blue is sky the"
    示例 2:

    输入: "  hello world!  "
    输出: "world! hello"
    解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包

    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


    括。
    示例 3:

    输入: "a good   example"
    输出: "example good a"
    解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-words-in-a-string
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    100.相同的书

    给定两个二叉树,编写一个函数来检验它们是否相同。

    如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

    示例 1:

    输入: 1 1
    / /
    2 3 2 3

    [1,2,3], [1,2,3]

    输出: true
    示例 2:

    输入: 1 1
    /
    2 2

    [1,2], [1,null,2]

    输出: false
    示例 3:

    输入: 1 1
    / /
    2 1 1 2

    [1,2,1], [1,1,2]

    输出: false

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/same-tree
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    # 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)
  • 相关阅读:
    GridView, ListView 区别
    ActivityGroup和TabActiviy的差异性?
    Java加密解压
    Android代码中实现WAP方式联网
    SVN创建资源库和远程连接配置
    高仿优酷Android客户端图片左右滑动(自动切换)
    andoid 多线程断点下载
    Android中用Java代码实现zip文件解压缩
    JAVA两种实现二分查找方式
    三种JAVA编程方法实现斐波那契数列
  • 原文地址:https://www.cnblogs.com/xqy-yz/p/11408646.html
Copyright © 2011-2022 走看看