LeetCode---Longest Continuous Increasing Subsequence、1-bit and 2-bit Characters、 Find Pivot Inde

674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4. 

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1. 

给定一个未经排序的整数数组,找到最长且连续的的递增序列。

思路:设置一个统计数和一个结果数,遍历数组,如果前一位小于后一位,统计数就自加1,让结果数等于其自身和统计数之间的最大值;如果前一个数不比后一个数小,则统计数重置为0。最后的返回结果则应该是结果数再加1,因为连续递增的序列里最后那个大的数没加。

class Solution:
    def findLengthOfLCIS(self, nums):
        j=0
        res=0
        if len(nums)==0:
            return 0
        for i in range(len(nums)-1):
            if nums[i]<nums[i+1]:
                j+=1
                res=max(j,res)
            else:
                j=0
        return res+1

《LeetCode---Longest Continuous Increasing Subsequence、1-bit and 2-bit Characters、 Find Pivot Inde》

717. 1-bit and 2-bit Characters

We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).

Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.

Example 1:

Input: 
bits = [1, 0, 0]
Output: True
Explanation: 
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.

Example 2:

Input: 
bits = [1, 1, 1, 0]
Output: False
Explanation: 
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.

Note:

  • 1 <= len(bits) <= 1000.
  • bits[i] is always 0 or 1.

有两种特殊字符。第一种字符可以用一比特0来表示。第二种字符可以用两比特(10 或 11)来表示。

现给一个由若干比特组成的字符串。问最后一个字符是否必定为一个一比特字符。给定的字符串总是由0结束。

存在两类数,一类是10或11,一类是0,将一个数组拆分为这两类数,判断最后一组数是否为0。例如例1, [1, 0, 0],一组数是10,一组数0,最后一组数是0.例如例2,[1, 1, 1, 0],一组数是11,一组数是10,最后一组数不是0

思路:对于数字1,后面的数字是1或者0都行,所以可以跳过1后面的值,遇到0,则移动1位。

class Solution:
    def isOneBitCharacter(self, bits):
        i=0
        while i < len(bits):
            if bits[i]==1:
                i+=2
                if i==len(bits):
                    return False
            else:
                i+=1
        return True

《LeetCode---Longest Continuous Increasing Subsequence、1-bit and 2-bit Characters、 Find Pivot Inde》

724. Find Pivot Index

Given an array of integers nums, write a method that returns the “pivot” index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:

Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.

Example 2:

Input: 
nums = [1, 2, 3]
Output: -1
Explanation: 
There is no index that satisfies the conditions in the problem statement.

Note:

  • The length of nums will be in the range [0, 10000].
  • Each element nums[i] will be an integer in the range [-1000, 1000].

给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法。

我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。

如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。

思路:先求整个数组的和,然后遍历数组,如果左边等于右边,则返回下标

class Solution:
    def pivotIndex(self, nums):
        sums=sum(nums)
        l=0
        for i in range(0,len(nums)):
            l+=nums[i]
            if (sums-l)==(sums-(sums-l)-nums[i]):
                return i
        return -1

《LeetCode---Longest Continuous Increasing Subsequence、1-bit and 2-bit Characters、 Find Pivot Inde》

点赞