1. 程式人生 > >Python刷題:簡單陣列(五)

Python刷題:簡單陣列(五)

21.Max Consecutive Ones

    Given a binary array, find the maximum number of consecutive 1s in this array.

    給定一個二進位制陣列,找出由該陣列中連續出現的數字一的最大個數。

示例:nums=[1,1,0,1,1,1],輸出3

程式:

def fm(nums):
	current = 0
	max_n = 0
	for num in nums:
		if num == 1:
			current += 1
			max_n = max(max_n, current)
		else:
			current = 0
	return current

22.K-diff Pairs in an Array

    Given an array of integers and an integer K, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

    給定一個整數陣列和一個整數K,你需要在該陣列中找出獨一無二的k差異對。K差異對由整數對(i, j)組成,i與j都是陣列中的數字,並且它們的絕對值差值等於K。

示例:給定[3,1,4,1,5]與k=2,輸出2(因為(1,3)與(3,5))

程式:

import collections
def ij(nums,k):
	if k>0:
		return len(set(nums) + set(num+k for num in nums))
	elif k=0:
		return sum(v>1 for v in collections.Counter(nums).values())/2
	else:
		return 0

23.Array Partition

    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.

    給定一個由2n個整陣列成的陣列,你的任務是把這些整數劃分為n對,如(a1, b1), (a2, b2),…, (an,bn),最後使得min(ai,bi)的總和最大。

示例:

給定[1,4,3,2],輸出4

程式:

def partition(nums):
	return sum(sorted(nums)[::2])

24.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.

    在MATLAB中,有一個常用函式名叫reshape,該函式可以將矩陣重塑為大小不一的新矩陣且不改變原始數值。你需要將一個矩陣使用二維矩陣表示出來,兩個正整數r和c分別代表重構矩陣的行數和列數。重構矩陣需要以相同的行遍歷順序遍歷原矩陣中的所有元素。如果給定重構矩陣的引數可用且合法,輸出新的重構矩陣;否則,輸出原始矩陣。

示例:

輸入 nums=[[1,2],[3,4]],r=1,c=4

輸出 [[1,2,3,4]]

程式:

def reshape(nums, r, c):
	if(len(nums)*len(nums[0]) == r*c):
		mid = [nums[i][j] for i in range(len(nums)) for j in range(len(nums[0]))]
		return [mid[t*c:(t+1)*c] for t in range(r)]
	else:
		return nums

25.Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shorted such subarray and output its length.

    給定一個整數陣列,你需要找出一個連續的子陣列,如果你僅採取升序排序對該子陣列排序,整個原陣列也會呈現升序狀態。你需要找出最短的子陣列,並且輸出其長度。

示例:

輸入 [2,6,4,8,10,9,15]    輸出5

程式:

def reshape(nums, r, c):
	if(len(nums)*len(nums[0]) == r*c):
		mid = [nums[i][j] for i in range(len(nums)) for j in range(len(nums[0]))]
		return [mid[t*c:(t+1)*c] for t in range(r)]
	else:
		return nums