Supervise yourself-three programming questions of a certain guest network (Java)-string, set, array

Three questions on a certain Kewang programming (Java)-string, set, array

The first question:
Title description
Find the first character that appears only once in the string
Input description:
Input several non-empty strings
Output description:
Output the first character that appears only once, if it does not exist, output -1
Example 1
Enter
asdfasdfo
aabb
copy
o
-1

import java.util.*;
public class Main{
    
    
    public static void main(String[]args){
    
    
        Scanner input=new Scanner(System.in);
        ArrayList<String>list=new ArrayList<>();
        while(input.hasNext()){
    
    
           list.add(input.nextLine());
        }
        for(String str:list){
    
    
           Map<Character,Integer>map=new HashMap<>();
            for(int i=0;i<str.length();++i){
    
    
                char x=str.charAt(i);
                if(!map.containsKey(x)){
    
    
                    map.put(x,1);
                }else{
    
    
                    map.put(x,map.get(x)+1);
                }
            }
            boolean flag=false;
            for(int i=0;i<str.length();++i){
    
    
                char x=str.charAt(i);
                if(map.get(x)==1){
    
    
                    System.out.println(x);
                    flag=true;
                    break;
                }
            }
            if(!flag){
    
    
                System.out.println(-1);
            }
        }
    }
}

Basic idea: Use HashMap to map one by one, add one to the number found, and enter the Map if it is not found. In the end, you only need to find the number one in the Map.
Why choose HashMap?
Because the time complexity of adding, deleting, checking and modifying HashMap is O(1).
Insert picture description here

The second question:
Title description
Input n integers and output the smallest k.
There are multiple sets of input examples for this question, please use loop reading, such as while (cin>>), etc.
Input description:
input two integers n and k in the
first line, input an integer array in the second line
Output description:
output one from childhood To large sorted integer array
Example 1
Input
5 2
1 3 5 7 2
Output
1 2

import java.util.*;
public class Main{
    
    
    public static void main(String[]args){
    
    
        Scanner input=new Scanner(System.in);
        while(input.hasNext()){
    
    
            int n=input.nextInt();
            int k=input.nextInt();
            PriorityQueue<Integer>queue=new PriorityQueue<>(
            new Comparator<Integer>(){
    
    
                public int compare(Integer o1,Integer o2){
    
    
                    return o2-o1;
                }
            }
            );
            for(int i=0;i<n;++i){
    
    
                int x=input.nextInt();
                if(i<k){
    
    
                    queue.offer(x);
                }else{
    
    
                    if(x<queue.peek()){
    
    
                        queue.poll();
                        queue.offer(x);
                    }
                }
            }
            ArrayList<Integer>list=new ArrayList<>();
            for(int x:queue){
    
    
                list.add(x);
            }
            Collections.sort(list);
            for(int x:list){
    
    
                System.out.print(x+" ");
            }
            System.out.println();
        }
    }
}

The basic idea: create a large root heap, first enter K values, and then the top of the heap must be the largest, so you only need to compare the top element with the element after K, the top element of the heap will be out of the heap, and then the current element will be added to the heap. , In the end, there are only K smallest elements left in the heap, and only sorted output will be used next.
Note: The PriorityQueue in the collection is a small root heap by default, so the comparator needs to be rewritten.
Insert picture description here

Question 3:
Title description
Enter two integers represented by character strings and find the sum of the numbers they represent.
The length of the string does not exceed 10,000.
This question contains multiple sets of sample input.
Input description:
Input two strings. Ensure that the string contains only '0'~'9' characters.
Output description:
output the result of the summation.
Example 1
input
9876543210
1234567890 and
output
11111111100

import java.util.*;
public class Main{
    
    
    public static void main(String[]args){
    
    
        Scanner input=new Scanner(System.in);
        ArrayList<String>list=new ArrayList<>();
        while(input.hasNext()){
    
    
          String str1=input.next();
            StringBuilder s1=new StringBuilder();
            s1.append(str1);
          String str2=input.next();
            StringBuilder s2=new StringBuilder();
            s2.append(str2);
            StringBuilder end=new StringBuilder();
            while(s1.length()<s2.length()){
    
    
                s1.insert(0,'0');
            }
            while(s1.length()>s2.length()){
    
    
                s2.insert(0,'0');
            }
            int c=0;
            for(int i=s1.length()-1;i>=0;i--){
    
    
                char x=s1.charAt(i);
                char y=s2.charAt(i);
                
                int re=(x-'0')+(y-'0')+c;
                if(re<10){
    
    
                  end.insert(0,re);
                    c=0;
                }else{
    
    
                    end.insert(0,re%10);
                    c=1;
                    if(i==0){
    
    
                        end.insert(0,c);
                    }
                }
            }
            System.out.println(end.toString());
        }
    }
}

The basic idea:
Since the length of the input string may be very long, the length of the Long type number is not too long, so it is excluded to take the conversion through Integer.valueOf and then calculate it. Is there no way? Do not! In fact, it's very simple. You only need to count the string from back to front, one by one, but you must consider whether there is a carry. Connect one bit for each bit of calculation to get the final result.
Insert picture description here

Urge yourself every day, come on together! ! ! ! ! ! ! ! ! ! !
At the same time, there are better practices, welcome to discuss.

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/115218934