The article directories

      • preface
      • Topic 1: Merge table record, set frame, map to do two-dimensional array
      • Topic 2: Unordered de-duplication of strings
      • Problem 3: Statistics on the number of characters in a string
      • Problem 4: String in reverse order
      • Problem 5: Sentence reverse order
      • Problem 6: string sort
      • The end of the

preface

Start mastering common interview algorithms.

Topic 1: Merge table record, set frame, map to do two-dimensional array

Summary: Data table records contain table indexes and values (positive integers in the int range). Please merge the records with the same index. That is, sum the values of the same index.

Input requirements: Enter the number of key-value pairs, and then enter pairs of index and value values separated by Spaces

Because the index and value involved in the input must be stored using a map

Output requirements: Output merged key-value pairs (multiple lines)

Input example:

4, 0, 1, 0, 2, 1, 2, 3, 4Copy the code

Example output:

0, 3, 1, 2, 3, 4Copy the code

Code:

import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Test1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // To receive inputwhile(scanner.hasNext()) {// Boolean judge int next = scanner.nextint (); // This is the number TreeMap<Integer, Integer> resultMap = new TreeMap(); // Built-in collator, default is key ascending order, meet output requirementsfor (int i = 0; i < next; i++) {
                int key = scanner.nextInt();
                int value = scanner.nextInt();
                if (resultMap.containsKey(key)) {
                    resultMap.put(key, value + resultMap.get(key));
                } else{ resultMap.put(key, value); }}for(Map.Entry entry : Resultmap.entryset ()) {// Iterate through entry=key+ Value entry.getKey() entry.getValue() system.out.println (entry.getKey() +""+ entry.getValue()); }}}}Copy the code

Topic 2: Unordered de-duplication of strings

Enter an int and return a new integer that contains no duplicate digits in the order read from right to left. Ensure that the last digit of the input integer is not 0.

Input description: Enter an int integer

Output description: Returns a new integer that does not contain repeated digits, in reading order from right to left

The input

9876673
Copy the code

The output

37689
Copy the code

code

To ensure order and non-repetition, we use the LinkedHashSet collection framework and output in reverse order

import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Scanner; Public class Test2 {public class Test2 {public class Test2 {public class Test2 {public class Test2 public class Test2 {public class Test2 public class Test2 public class Test2 public class Test2 public class Test2 public class Test2 public class Test2 public class Test2 // Ensure that the last digit of the input integer is not 0. Public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) { Use stringBuffer Scanner Scanner =new Scanner(system.in);while(scanner.hasNext()){
            String string=scanner.next();
            LinkedHashSet set=new LinkedHashSet<>();
            for(int i=string.length()-1; i>=0; i--){ set.add(string.charAt(i)); Iterator = set.iterator(); Iterator = set.iterator();while(iterator.hasNext()) { System.out.print(iterator.next()); }}}}Copy the code

This is a bit of a mistake on niuke.com, I don’t know if the collection framework is too large or something, you can also use strings

class Test2_String{
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        while (scanner.hasNext()){
            String input=scanner.next();
            String[] num = input.split("");
            HashSet resultMap=new HashSet();
            StringBuffer stringBuffer=new StringBuffer();
            for(int i=num.length-1; i>=0; i--){if(! resultMap.contains(num[i])){ stringBuffer.append(num[i]); Resultmap. add(num[I]); / / usesetPrintln (integer.valueof (stringBuffer+)"")); }}}Copy the code

Problem 3: Statistics on the number of characters in a string

Write a function to count the number of different characters in a string. A character is in the ACSII code range (0 to 127). A newline indicates the end character and is not counted as a character. Those not in the range will not be counted. Multiple identical characters are counted only once. For example, for the string abaca, there are three different characters a, B, and C, so it prints 3.

Input description: Enter a string without Spaces.

Output description: The value contains 0 to 127 characters.

The input

abc
Copy the code

The output

3
Copy the code

Just count the number, just like de-weight, and use a set

import java.util.HashSet;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String input = scanner.next();
            String[] nums = input.split("");
            HashSet countSet = new HashSet();
            for (int i = 0; i < nums.length; i++) {
                if(! countSet.contains(nums[i])) { countSet.add(nums[i]); } } System.out.println(countSet.size()); }}}Copy the code

Anything that involves de-counting can be done with bitmaps. Because each different piece of data only needs to be stored in binary bits, the storage space used for statistics is greatly reduced

class Test3_BitSet {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) { String input = scanner.next(); char[] nums = input.toCharArray(); // Bitsets can only hold ints and char, not strings, so input.split()""); Into the input. ToCharArray (); BitSet countSet = new BitSet(128);for (int i = 0; i < nums.length; i++) {
                if(! countSet.get(nums[i])) { countSet.set(nums[i]); } } System.out.println(countSet.cardinality()); // BitSet can only be usedsetGet cardinality, size()}}}Copy the code

Problem 4: String in reverse order

Input an integer and output the integer in reverse order as a string. The program does not consider the case of negative numbers. If the number contains 0, the reverse form also contains 0. If the input is 100, the output is 001.

Input description: Enter an int integer

Output description: Output this integer in reverse order as a string

Example 1 Input

1516000
Copy the code

The output

0006151
Copy the code

Code:

import java.util.Scanner; Public class Test4 {// Input an integer, output the integer as a string in reverse order. // If the number contains 0, then the reverse order also contains 0. If the input is 100, output is 001. Public static void main(String[] args) {Scanner Scanner =new Scanner(system.in);while (scanner.hasNext()){
            String input = scanner.next();
            String[] exchange=input.split("");
            StringBuffer stringBuffer=new StringBuffer();
            for(int i=exchange.length-1; i>=0; i--){ stringBuffer.append(exchange[i]); } System.out.println(stringBuffer+""); }}}Copy the code

Problem 5: Sentence reverse order

Put an English statement in reverse order of words. For example, “I am a boy” is displayed as “boy a am I” in reverse order. All words are separated by a space and contain no other characters except English letters.

Input description: Enter an English statement, each word separated by a space. Ensure that the input contains only Spaces and letters.

Output description: Get the sentence in reverse order.

The input

I am a boy
Copy the code

The output

boy a am I
Copy the code

Code (both pieces of code are fine) :

import java.util.Scanner;

//public class Test5 {
//    public static void main(String[] args) {
//        Scanner scanner=new Scanner(System.in);
//        while(scanner.hasNextline ()){// Instead of looping one character at a time, loop one action at a time // String input = scanner.nextline (); [] nums=input.split()""); // StringBuffer StringBuffer =new StringBuffer(); //for(int i=nums.length-1; i>=0; i--){ // stringBuffer.append(nums[i]); //if(i! // stringbuffer.append ();""); // } // } // System.out.print(stringBuffer); // class Test5_Help { Public static void main(String[] args) {Scanner Scanner =new Scanner(system.in);while(scanner.hasNextline ()){// Instead of looping one character at a time, loop one behavior at a time String input = scanner.nextline (); NextLine String[] nums=input.split()""); // Separate with Spacesfor(int i=nums.length-1; i>=0; i--){ System.out.print(nums[i]+""); }}}}Copy the code

Problem 6: string sort

Given n strings, arrange n strings in lexicographical order.

Input description: The first value is a positive integer n(1≤n≤1000). The following values are n characters (length ≤100). The characters contain only upper and lower case letters.

Output description: N lines of data are output, and the output is a string in lexicographical order.

The input

9
cap
to
cat
card
two
too
up
boat
boot
Copy the code

The output

boat
boot
cap
card
cat
to
too
two
up
Copy the code

code

import java.util.Arrays; import java.util.Scanner; Public static void main(String[] args) {Scanner Scanner =new Scanner(System.in);while (scanner.hasNext()){
            int count=scanner.nextInt();
            String[] nums=new String[count];
            for(int i=0; i<count; i++){ nums[i] = scanner.next(); } array.sort (nums); // Use an array to sort thingsfor(int i=0; i<nums.length; i++){ System.out.println(nums[i]); }}}}Copy the code

The end of the

All are the entry level, Java write to choose a good set framework, set with its own to remove heavy properties, map with its own key value pair, array with its own alphabetic method, bitSet to reduce space complexity, brush a few master, and then go to see the binary tree, dynamic planning these, ten thousand hours of law should be deliberately learned, rather than simple repetition.

Source click here

Play code every day, progress every day!!