高级编程技术leetcode作业1

27. Remove Element

Given an array nums and a value val, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example 1:

Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.

It doesn't matter what you leave beyond the returned length.

Example 2:

Given nums = [0,1,2,2,3,0,4,2], val = 2,

Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.

Note that the order of those five elements can be arbitrary.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

思路:建立一个快指针一个慢指针,慢指针始终指向第一个val,快指针遍历到不是val时,把不是val的那个变量赋给慢指针所指的val

class Solution:
	def removeElement(self, nums, val):
		i=0
		for j in range(0,len(nums)):
			if nums[j]!=val:
				nums[i]=nums[j]
				i=i+1

		return i
	


88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]
class Solution:
	def merge(self, nums1, m, nums2, n):
		j=0

		for i in range(m,len(nums1)):
			nums1[i]=nums2[j]
			j+=1
			if j==len(nums2):
				break
		nums1.sort()


33. Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

思路:由于时间复杂度为O(logn),所以采取二分查找的方式来进行,第一步找出支点,支点即数组中唯一一个后面元素比自己小的元素,没有支点返回-1,此时数组已经有序,找到支点将数组分为两个有序数组,分别应用二分查找查找目标,此时可将时间复杂度降为O(logn)

class Solution:
	def findPivot(self,nums):
		low=0
		high=len(nums)-1

		#注意列表可能为空
		if not nums:
			return -1

		while low<=high:
			middle=int((low+high)/2)

			if middle+1<=high and nums[middle]>nums[middle+1]:
				return middle
			elif middle-1>=low and nums[middle]nums[low]:
				low=middle+1
			elif nums[middle]target:
				high=middle-1
			elif nums[middle]=nums[0]:#注意仅有一个元素的情况
			return self.binary_search(nums,target,0,pivot)
		else:
			return self.binary_search(nums,target,pivot+1,len(nums)-1)

你可能感兴趣的:(高级编程技术作业)