485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array.
Example 1:
Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
Note:
- The input array will only contain
0
and1
. - The length of input array is a positive integer and will not exceed 10,000
给定一个二进制数组,计算数组中出现的最大连续1的个数。
注意:
- 输入数组只包含0和1
- 数组长度是正整数并且不会超过10000
思路:设置两个计数器:结果值和计数值。以此访问list,如果值为1,计数值加一,结果值取结果值和计数值中的大值。如果值为0,计数值复为0。
class Solution:
def findMaxConsecutiveOnes(self, nums):
count=0
result=0
for i in range(len(nums)):
if nums[i]==1:
count+=1
result=max(count,result)
else:
count=0
return result
509. Fibonacci Number
The Fibonacci numbers, commonly denoted F(n)
form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0
and 1
. That is,
F(0) = 0, F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1.
Given N
, calculate F(N)
.
Example 1:
Input: 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Note:
0 ≤ N
≤ 30.
斐波那契数列
class Solution:
def fib(self, N):
if N==0:
return 0
elif N<=2:
return 1
else:
return self.fib(N-1)+self.fib(N-2)
561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Example 1:
Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
- n is a positive integer, which is in the range of [1, 10000].
- All the integers in the array will be in the range of [-10000, 10000].
给定一个2n个整数的数组,你的任务是把这些整数分组成n对整数,比如(a1, b1), (a2, b2),…, (an, bn)使得所有i从1到n的最小值(ai, bi)之和尽可能大。
思路:排序(Sort) 将数组从小到大排序,取下标为偶数的元素求和即为答案。
class Solution:
def arrayPairSum(self, nums):
nums.sort()
return sum(nums[::2])
566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.
You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.
Example 1:
Input: nums = [[1,2], [3,4]] r = 1, c = 4 Output: [[1,2,3,4]] Explanation: The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Example 2:
Input: nums = [[1,2], [3,4]] r = 2, c = 4 Output: [[1,2], [3,4]] Explanation: There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.
Note:
- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.
在MATLAB中,有一个非常有用的函数 reshape
,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。
给出一个由二维数组表示的矩阵,以及两个正整数r
和c
,分别表示想要的重构的矩阵的行数和列数。
重构后的矩阵需要将原始矩阵的所有元素以相同的行遍历顺序填充。
如果具有给定参数的reshape
操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
思路:重塑矩阵前后元素个数相同,若元素个数不同,则直接返回原矩阵;若个数相同,则按行开始遍历,先按行存储到一个列表里,再生成矩阵。
class Solution:
def matrixReshape(self, nums, r , c):
r1=len(nums)
c1=len(nums[0])
ret=[]
if r1*c1 < r*c:
return nums
else:
t=[]
for i in range(r1):
for j in range(c1):
t.append(nums[i][j])
m=0
for i in range(r):
ret.append(t[m:m+c])
m+=c
return ret