Get into the habit of writing together! This is the third day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .
Daily quiz 2021.04.04
- leetcode original title link: leetcode-cn.com/problems/so…
- Difficulty: Moderate
- Method: bubble sort
topic
- Input an array of non-negative integers, concatenate all the numbers in the array into a number, and print the smallest one of all the numbers that can be concatenated.
Example
- Example 1
输入: [10,2]
输出: "102"
复制代码
- Example 2
输入: [3,30,34,5,9]
输出: "3033459"
复制代码
Problem solving ideas
- According to the meaning of the question: the sorting algorithm needs to be used, but the sorting conditions need to be customized
- Key Point: Custom Sort Order
- analyze:
[3,30,34,5,9]
- Take
0和1
the element with the subscript and compare it in combination =>330 与 303
, if the subscript is a small number spliced before, you need to exchange the value1
of the element with the subscript .0和1
- Because the bubble sort will
n - 1
traverse several times, each time the largest is placed at the end of the array.
- analyze:
- After the
n - 1
round traversal, the elements in the array will be neatly arranged in a custom order.
expand
- The so-called stability: is the relative position of two equal elements in the array, whether it changes after sorting
- No change: stable sorting
- Changed: unstable sorting
Bubble sort (whoever is older stands on the right)
- The numbers in the array are compared in pairs, each time the largest number is moved to the end of the array.
- A variable is also required to record whether a swap has occurred this round .
- If there is no exchange in this round, then the data in the array has all been arranged in order, and jump out of the loop directly
break
- On the contrary, it has not been completely
- If there is no exchange in this round, then the data in the array has all been arranged in order, and jump out of the loop directly
- Time complexity: o(n^2)
- Space complexity: o(1)
swap
exchange skills
- Requirement: Swap two numbers without using a third intermediate variable
- The way of adding and then subtracting
- Bit operation:
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[j] ^ arr[i];
arr[i] = arr[i] ^ arr[j];
复制代码
Quick sort (classic topic)
AC
code
var minNumber = function(nums) {
// 排序好了之后再拼接在一起
function bubbleSort(arr) {
let alen = arr.length, isChange = false;
for(let i = 0; i < alen; i++) {
if(isChange) break;
isChange = true;
for(let j = 0; j < alen - 1; j++) {
// console.log(arr[j + 1] + ''+ arr[j],arr[j] + '' + arr[j + 1])
if((arr[j + 1] + '' + arr[j]) <= (arr[j] + '' + arr[j + 1])){
arr[j + 1] = arr[j + 1] + arr[j];
arr[j] = arr[j + 1] - arr[j];
arr[j + 1] = arr[j + 1] - arr[j];
isChange = false;
}
}
}
}
bubbleSort(nums);
// console.log(nums)
let len = nums.length, ans = '';
for(let i = 0; i < len; i++) {
ans += nums[i];
}
return ans;
};
复制代码
Summarize
- It does not matter what sorting algorithm is used, the important test point is: the conversion of the conditions of the sorting algorithm.