LeetCode刷题---简单组(五)

简介: LeetCode刷题---简单组(五)

🍒题目一 58. 最后一个单词的长度

给你一个字符串 s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 最后一个 单词的长度。

单词 是指仅由字母组成、不包含任何空格字符的最大子字符串

示例 1:

输入:s = “Hello World”

输出:5

解释:最后一个单词是“World”,长度为5。

示例 2:

输入:s = " fly me to the moon "

输出:4

解释:最后一个单词是“moon”,长度为4。

示例 3:

输入:s = “luffy is still joyboy”

输出:6

解释:最后一个单词是长度为6的“joyboy”。


🍒解法一

自己写的,比较der

class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        count = 0
        ls = []
        s = s.strip()
        if ' ' not in s:
            return len(s)
       
        for i in range(-1,-len(s),-1):
            if s[i] == ' ':
                break
            ls.append(s[i])
            count +=1
        str1 = ''.join(ls[::-1])
        return count

使用strip给字符串收尾去空格,之后判断如果去重后的字符串里没有空格了,俺就说明是一个单词,返回单词长度即可,否则从后进行遍历,遇到第一个空格退出循环,使用count进行长度计数


🍒解法二

class Solution(object):
  def lengthOfLastWord(self, s):
      """
      :type s: str
      :rtype: int
      """
      return len(s.strip().split(" ")[-1])

strip去收尾空格后使用split进行分割为一个列表,选择最后一个,妙哉~


🍒题目二 66. 加一

给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入:digits = [1,2,3]

输出:[1,2,4]

解释:输入数组表示数字 123。

示例 2:

输入:digits = [4,3,2,1]

输出:[4,3,2,2]

解释:输入数组表示数字 4321。

示例 3:

输入:digits = [0]

输出:[1]

注意这里最好看英文,中文有点难理解

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0’s.

Increment the large integer by one and return the resulting array of digits.

Example 1:

Input: digits = [1,2,3]

Output: [1,2,4]

Explanation: The array represents the integer 123.

Incrementing by one gives 123 + 1 = 124.

Thus, the result should be [1,2,4].

Example 2:

Input: digits = [4,3,2,1]

Output: [4,3,2,2]

Explanation: The array represents the integer 4321.

Incrementing by one gives 4321 + 1 = 4322.

Thus, the result should be [4,3,2,2].

Example 3:

Input: digits = [9]

Output: [1,0]

Explanation: The array represents the integer 9.

Incrementing by one gives 9 + 1 = 10.

Thus, the result should be [1,0].

🍒解法一

来自某位大佬的题解

class Solution:
    def plusOne(self, digits):
        digits[-1]+= 1 
        for i in range(-1,-len(digits),-1):
            if digits[i] == 10:
                digits[i] = 0
                digits[i-1]+=1
        if digits[0] ==10: 
            digits[0]=0
            digits.insert(0,1)   
        return digits 

这里我解释一下,首先给最后一位+1

之后使用for循环,从后往前对每一位进行判断,是否等于10,若等于则将当前位置置为0,前一位置为1,若例子如999这样的,那么第一位在经过循环处理后为10,则需要使用insert开辟一个单位。

🍒题目三 67. 二进制求和

给你两个二进制字符串 a 和 b ,以二进制字符串的形式返回它们的和。

示例 1:

输入:a = “11”, b = “1”

输出:“100”

示例 2:

输入:a = “1010”, b = “1011”

输出:“10101”

🍒解法一

class Solution(object):
    def addBinary(self, a, b):
        len1 = len(a)
        len2 = len(b)
        if len1 < len2:
            a = '0' * (len2 - len1) + a
        elif len2 < len1:
            b = '0' * (len1 - len2) + b
        digits = list(a)  # 创建一个列表来存储结果
        carry = 0  # 初始化进位
        for i in range(len(digits) - 1, -1, -1):
            digit_sum = int(a[i]) + int(b[i]) + carry
            digits[i] = str(digit_sum % 2)  # 计算当前位的值
            carry = digit_sum // 2  # 计算进位
        if carry:  # 如果最高位有进位,需要在最前面加上 '1'
            digits.insert(0, '1')
        return ''.join(digits)  # 将列表转换为字符串


挑战与创造都是很痛苦的,但是很充实。

相关文章
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
27 6
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
27 4
|
13天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
40 2
|
13天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
16 7
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
13 4
|
14天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
32 5
|
13天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
25 3
|
13天前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
11 3
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
24 3
|
14天前
|
Python
【Leetcode刷题Python】剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Leetcode题目"剑指 Offer 21. 调整数组顺序使奇数位于偶数前面"的两种Python解决方案,一种是使用双端队列调整数组顺序,另一种是使用双指针法将奇数移到数组前半部分,偶数移到后半部分。
15 4