基本排序算法-插入、选择、冒泡排序
插入排序
特点:
- 所需时间受输入元素初始顺序的影响
- 如果数据是局部有序的,那么用插入排序会很高效。
- 也很适合小规模数组,在V8引擎里调用Array.prototype.sort()时,当数组长度大于10个的情况下,采用的是快速排序,而当长度小于等于10个时则切换为插入排序,以提高运算效率。
1 | /* |
特点:
1 | /* |
Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0.
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
"ace" is a subsequence of "abcde".A common subsequence of two strings is a subsequence that is common to both strings.
Example 1:
1 | Input: text1 = "abcde", text2 = "ace" |
Example 2:
1 | Input: text1 = "abc", text2 = "abc" |
Example 3:
1 | Input: text1 = "abc", text2 = "def" |
Constraints:
1 <= text1.length, text2.length <= 1000text1 and text2 consist of only lowercase English characters.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.
Example 1:
1 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]] |
Example 2:
1 | Input: intervals = [[1,4],[4,5]] |
Constraints:
1 <= intervals.length <= 104intervals[i].length == 20 <= starti <= endi <= 104Given an integer array nums, return the length of the longest strictly increasing subsequence.
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Example 1:
1 | Input: nums = [10,9,2,5,3,7,101,18] |
Example 2:
1 | Input: nums = [0,1,0,3,2,3] |
Example 3:
1 | Input: nums = [7,7,7,7,7,7,7] |
Constraints:
1 <= nums.length <= 2500-104 <= nums[i] <= 104Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
Example 1:
1 | Input: nums = [1,1,2] |
Example 2:
1 | Input: nums = [1,2,3] |
Constraints:
1 <= nums.length <= 8-10 <= nums[i] <= 10Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example :
1 | Input: nums = [1,2,3] |
Constraints:
1 <= nums.length <= 6-10 <= nums[i] <= 10nums are unique.