Write sorting today. Among the more efficient sorting are merge sorting and quick sorting. Those that are simple to write but with high time complexity include bubble sorting, selection sorting, insertion sorting, etc.
Sort linked list
Give you the head node of the linked list head , Please arrange them in ascending order and return the sorted linked list.
Input: head = [4,2,1,3] Output:[1,2,3,4]
Tips:
- The number of nodes in the linked list is in the range [0, 5 * 104] within
- -105 <= Node.val <= 105
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def sortList(self, head: ListNode) -> ListNode: h_head = ListNode(-1, head) mem = [] while (head is not None): next_h = head.next head.next = None mem.append(head) head = next_h mem = sorted(mem, key=lambda x: x.val) n = len(mem) if n == 0: return None h_head.next = mem[0] for i in range(n - 1): mem[i].next = mem[i + 1] return h_head.next
Search rotation sort array
The integer array nums is arranged in ascending order, and the values in the array are different from each other.
Before passing to the function, Num rotates on a previously unknown subscript k (0 < = k < num.length), making the array [num [k], Num [K + 1],..., Num [n-1], Num [0], Num [1],..., Num [k-1]] (subscripts count from 0). For example, [0,1,2,4,5,6,7] may become after rotation at subscript 3 [4,5,6,7,0,1,2] .
Give you the rotated array nums and an integer target. If the target value target exists in nums, its subscript will be returned. Otherwise, it will be returned - one .
Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Input: nums = [4,5,6,7,0,1,2], target = 3 Output:-1
Tips:
1 <= nums.length <= 5000
-10^4 <= nums[i] <= 10^4
Each value in num is unique
The title data ensures that nums rotates on a subscript unknown in advance
-10^4 <= target <= 10^4
from typing import Optional, List class Solution: def search(self, nums: List[int], target: int) -> int: length = len(nums) left = 0 right = length - 1 while (left <= right): mid = int((left + right) / 2) if (nums[mid] == target): return mid elif (nums[mid] < nums[right]): if (nums[mid] < target and target <= nums[right]): left = mid + 1 else: right = mid - 1 else: if (nums[left] <= target and target < nums[mid]): right = mid - 1 else: left = mid + 1 return -1
If the number in the middle is less than the rightmost number, the right half is ordered, and vice versa. Judge whether the target is within the range in the ordered interval, so as to determine which half to keep
The kth largest element in the array
Given the integer array nums and integer k, please return the K largest element in the array.
Note that you need to find the K largest element after the array is sorted, not the k different element.
input: [3,2,1,5,6,4] and k = 2 output: 5 input: [3,2,3,1,2,4,5,5,6] and k = 4 output: 4
Tips:
- 1 <= k <= nums.length <= 104
- -104 <= nums[i] <= 104
High energy ahead:
class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: nums = sorted(nums) return nums[len(nums)-k]
Unordered arrays can only be sorted in addition to traversal to find the most XX, so that's right! This writing method is very simple. The library function is written for people to adjust. Of course, if you write it carefully, you can sort it by the writing method of the first question just now.