Sword finger offer plan 16 (simple sorting) — Java


1.1 topic 1

Sword finger offer 45. Arrange the array into the smallest number

1.2 solution

Looking at the solution of this problem, I found myself thinking wrong.
Let’s take the big man’s solution directly here.
At the beginning, create an array of strings to store the int array
String.valueof converts int to string and stores it in the array.
It should be sorted here. Lambda expression is used in the problem solution to judge whether x + y is greater than y + X
Then add Jin StringBuilder and turn it into a string to return.

1.3 code

class Solution {
    public String minNumber(int[] nums) {
        String[] strs = new String[nums.length];
        for(int i = 0; i < nums.length; i++)
            strs[i] = String.valueOf(nums[i]);
        Arrays.sort(strs, (x, y) -> (x + y).compareTo(y + x));
        StringBuilder res = new StringBuilder();
        for(String s : strs)
        return res.toString();

2.1 topic 2

Sword finger offer 61. Shunzi in playing cards

2.2 solution

This is by judging whether the maximum minus the minimum of the five values is less than 5,
The hash table is used to determine whether there are duplicates. If there are duplicate values, they are not CIS children.
You can also sort the array first and then compare the maximum value with the minimum value except zero after sorting.

2.3 code

class Solution {
    public boolean isStraight(int[] nums) {
        Set s = new HashSet();
        int max=0,min=14;
        for(int i=0;i