Huawei Online Programming Questions Series-9-Extracting Unique Integers


Problem Description:

Problem Description

1. The question involves knowledge points.

string.contains(key)and string.substring()use.

2. Solve it yourself.

  • I solved it according to the idea of ​​string.
  • StringBuilderCreate a new sb, and use it to string.containsjudge whether it is included or not, and add it if it is not included StringBuilder.add.
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class HuaWei_9_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int key = scanner.nextInt();
            System.out.println(Convert(key));
        }
    }
    private static String Convert(int key) {
        String strKey = key+"";
        StringBuilder sb = new StringBuilder("");
        for(int i=0;i<strKey.length();i++){
            char tmpChar = strKey.charAt(strKey.length()-1-i);
            if(!sb.toString().contains(tmpChar+"")) {
                sb.append(tmpChar);
            }
        }
        return sb.toString();
    }

}

3. Quality answers.

Solution 1: Use a marker array.
- Use a ten-digit array as a marker to store the characters that have appeared.
- Use ascii to subtract 48 each time, and directly convert char to int.

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            String s=scanner.nextLine();
            int len=s.length();
            int []arr1=new int[10];//标志数组
            for(int i=len-1;i>=0;i--){
                if(arr1[s.charAt(i)-48]==0){
                    System.out.print(s.charAt(i)-48);//直接打印
                    arr1[s.charAt(i)-48]++;//标记出现过的字符.
                }
            }
        }
    }
}

Solution 2: Use arrayListeach pass list.contarinsto determine whether it is included.
It should be noted here that the HashSetoutput order is determined by the Hash algorithm, and it cannot be guaranteed to be the same as your input order, so it cannot be used.

package com.chaoxiong.niuke.huawei;

import java.util.*;

/**
 * Create by tianchaoxiong on 18-4-9.
 */
public class HuaWei_9_3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int key = scanner.nextInt();
            Convert(key);
        }
    }
    private static void Convert(int key) {
        String string = String.valueOf(key);
        List<Character>charList = new ArrayList<Character>();
        int N = string.length();
        for(int i=0;i<N;i++){
            char tmp = string.charAt(N-1-i);
            if(!charList.contains(tmp))
                charList.add(tmp);
        }
        for (char each:charList){
            System.out.print(each);
        }
    }
}

4. Summary of this question.

What this question gave me is:
- According to the meaning of the question, no printing will be done where there is no need to print. Simplify the operation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325967798&siteId=291194637