辣鸡刘的Leetcode之旅7【Length of Last Word,Maximum Subarray,count-and-say,Plus One】

7 篇文章 0 订阅
4 篇文章 0 订阅

58. Length of Last Word

题目描述:

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

Example:

Input: "Hello World"
Output: 5

这个很简单:

class Solution:
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        str = s.split()
        if len(str)==0:
            return 0
        else:
            return len(str[-1])

一行代码也可以:

def lengthOfLastWord(self, s):
    return len(s.rstrip(' ').split(' ')[-1])

## 53. Maximum Subarray ##```
题目描述:

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.


动态规划,这个我不太会:

class Solution:
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        for i in range(1,len(nums)):
            if nums[i-1]>0:
                nums[i]+=nums[i-1]
        return max(nums)
        

注释:动态规划,汉诺塔之类的字眼,师兄师姐做笔试题成天遇到,貌似很重要,嘿嘿

## count-and-say ##

The count-and-say sequence is the sequence of integers with the first five terms as following:

  1. 1
    
  2. 11
    
  3. 21
    
  4. 1211
    
  5. 111221
    

1 is read off as “one 1” or 11.
11 is read off as “two 1s” or 21.
21 is read off as “one 2, then one 1” or 1211.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.

Note: Each term of the sequence of integers will be represented as a string.

题目稍微有点不容易理解,就是1读作1个1,11读作2个1既是:21;21读作1个2和1个1:1211,以此类推......
class Solution:
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        s = '1'
        for _ in range(n - 1):
            s = ''.join(str(len(list(group))) + digit
                        for digit, group in itertools.groupby(s))
        return s

``

66. Plus One

题目:

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

简单翻译:将给出的列表转为数字,然后加1,再转为列表;
重点注意str和int之间的转换;
我的代码如下:

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        if digits[0]==0 and len(digits)>1:
            digits=digits[1:len(digits)+1]
        num=int(''.join(str(i) for i in digits))
        num+=1
        nums=list(str(num))
        int_nums=[int(i) for i in nums]
        print(int_nums)
        return int_nums

s=Solution()
s.plusOne([0,1,2,3])

最快的是大神用指针做的,学习一下:

class Solution:
    def plusOne(self, digits):
        """
        :type digits: List[int]
        :rtype: List[int]
        """
        ans = []
        rem = 1
        
        while digits:
            val = digits.pop()
            temp = val + rem
            next = temp % 10
            rem = temp // 10
            ans.insert(0, next)
            
            if not digits:
                while rem:
                    temp = rem
                    next = temp % 10
                    rem = temp // 10
                    ans.insert(0, next)
                
        return ans
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值