【LeetCode】74. Search a 2D Matrix 解题记录
问题描述
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
测试样例
12Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3Output: true
12Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13Output: false
说明
1234m == matrix.lengthn == matrix[i].length1 <= m, n <= 1 ...
【LeetCode】278. First Bad Version 解题记录
问题描述
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which returns whether version is bad. Implem ...
【LeetCode】162. Find Peak Element 解题记录
问题描述
A peak element is an element that is strictly greater than its neighbors.
Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.
You may imagine that nums[-1] = nums[n] = -∞.
You must write an algorithm that runs in O(log n) time.
测试样例
123Input: nums = [1,2,3,1]Output: 2Explanation: 3 is a peak element and your function should return the index number 2.
说明
123Input: nums = [1,2,1,3,5,6,4]Output: ...
【LeetCode】1095. Find in Mountain Array 解题记录
问题描述
(This problem is an interactive problem.)
You may recall that an array A is a mountain array if and only if:
A.length >= 3
There exists some i with 0 < i < A.length - 1 such that:
A[0] < A[1] < … A[i-1] < A[i]
A[i] > A[i+1] > … > A[A.length - 1]
Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index doesn’t exist, return -1.
You can’t access the mountain array directly. You may only access ...
【LeetCode】33. Search in Rotated Sorted Array 解题记录
问题描述
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the rotation and an integer target, return the index of target if it ...
【LeetCode】34. Find First and Last Position of Element in Sorted Array 解题记录
问题描述
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.
测试样例
12Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]
12Input: nums = [5,7,7,8,8,10], target = 6Output: [-1,-1]
12Input: nums = [], target = 0Output: [-1,-1]
说明
12340 <= nums.length <= 10^5-10^9 <= nums[i] <= 10^9nums is a non-decreasi ...
【LeetCode】215. Kth Largest Element in an Array 解题记录
问题描述
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
测试样例
12Input: nums = [3,2,1,5,6,4], k = 2Output: 5
12Input: nums = [3,2,3,1,2,4,5,5,6], k = 4Output: 4
说明
121 <= k <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4
解题
思路
寻找第 K 大的数即为寻找递增序的第 length - K 个数。
使用二分法的思想来解决,那么如何确定基准?
根据快排的思想,每一步确定一个元素的位置,并使其左侧元素值不大于该元素,右侧元素值不小于该元素。以该元素作为基准。
若目标在基准点右侧 ...
【LeetCode】179. Largest Number 解题记录
问题描述
Given a list of non-negative integers nums, arrange them such that they form the largest number.
Note: The result may be very large, so you need to return a string instead of an integer.
测试样例
12Input: nums = [10,2]Output: "210"
12Input: nums = [3,30,34,5,9]Output: "9534330"
12Input: nums = [1]Output: "1"
12Input: nums = [1]Output: "1"
说明
121 <= nums.length <= 1000 <= nums[i] <= 10^9
解题
思路
将数组转换为字符串,并对其进行排序,规则为判断两字符串分别在前时的字母序大小,即 (str1 ...
【LeetCode】56. Merge Intervals 解题记录
问题描述
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
测试样例
123Input: intervals = [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
123Input: intervals = [[1,4],[4,5]]Output: [[1,5]]Explanation: Intervals [1,4] and [4,5] are considered overlapping.
说明
123Input: inter ...
【LeetCode】328. Odd Even Linked List 解题记录
问题描述
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in O(1) extra space complexity and O(n) time complexity.
测试样例
12Input: head = [1,2,3,4,5]Output: [1,3,5,2,4]
12Input: head = [2,1 ...