New Ket Net Sword refers to offer, some small question worth remembering (iii) __java

Source: Internet
Author: User

13. Enter a string that prints all the permutations of the characters in the string in dictionary order. For example, the input string ABC prints out all strings abc,acb,bac,bca,cab and CBA that can be arranged by the character A,b,c.

Import java.util.*;
public class Solution {public
    arraylist<string> permutation (String str) {
        arraylist<string> list= New Arraylist<string> ();
        if (Str.length () ==0) {return
            list;
        } 
        Char [] Temp=str.tochararray ();
        hashset<string > S = new hashset<string> ();
        Fund (s,temp,0);
        List.addall (s);
        Collections.sort (list);
        return list;
    }
    void Fund (Hashset<string > s,char[] str,int k) {
        if (str.length==k) {
            s.add (new String (str));
            return;
        } 
        for (int i=k;i<str.length;i++) {
            Swap (str,i,k);
            Fund (s,str,k+1);
            Swap (str,i,k);
         }    
    }
    void Swap (char [] str,int I,int j) {
        if (i!=j) {
            char temp = str[i];
            STR[I]=STR[J];
            Str[j]=temp
        }}
    }

Analysis: The following image I looked for on the internet, the point of knowledge is the whole arrangement, Fund (hashset<string> s,char[] str,int k), the function means for the STR in the beginning of a full order from K, and exist in S, Because it is possible to have duplicate numbers, so you can add a judgment condition, that is, the same number does not swap (), where I used to put hashset into the list and then reorder (delete duplicate elements) processing.


14. The number appearing in the array is more than half the length of the array, please figure this out. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Because the number 2 appears in the array 5 times, it exceeds half the length of the array, so output is 2. Output 0 If it does not exist.

Import java.util.ArrayList;
Import Java.util.Arrays;
public class Solution {public
    int morethanhalfnum_solution (int [] array) {
        arrays.sort (array);
        for (int i=0;i<array.length%2+array.length/2;i++) {
            if (Array[i]==array[i+array.length/2]) {return  Array[i];
            }
        return 0;
    }
}
Analysis: This is not difficult, but I feel very new, the idea is to sort the array, if a number and half the length of the array after the number is equal, then return this number.


15. Find out the number of occurrences of 1 in the 1~13 integer and calculate the number of occurrences of 1 in the 100~1300 integer. He counted it for a special reason. 1~13 contains 1 of the number 1, 10, 11, 12, 13, so there are 6, but for the back of the problem he has no way. Acmer wants you to help him, and to make the problem more universal, you can quickly find out the number of 1 occurrences in any nonnegative integer interval.

public class Solution {public
    int numberof1between1andn_solution (int n) {
        int count=0;
        for (int i=1;i<=n;i*=10) {
            int a=n/i;
            int b=n%i;
            if (a%10==1) {
                count=count+ (a+8)/10*i+ (b+1);
            } else{
                count=count+ (a+8)/10*i;
            }
        return count;
    }
}
Analytical:

For example: 2312 this number
Let's see how many 1 in a single-digit.
To judge 10 digits, if this is 1 (ie a%10==1), then the number of digits after this position is mainly added.
Then judge the hundred, if this is 1 (ie a%10==1), then the number of the number after this position, if not then count+100
Then judge the thousand, if this is 1 (ie a%10==1), then mainly add the number of the number after this position, if not then count+1000

This is the standard of the cattle online some of the saying:

Link: https://www.nowcoder.com/questionTerminal/bd7f978302044eee894445e244c7eee6
Source: Niu Ke Net

/* Set n = ABCDE, where ABCDE is the number on each of you in decimal. If you want to calculate the number of times a 1 appears on the hundred, it is affected by 3: the number on the hundred, the number below the Hundred (low), and the number above the Hundred (high). ① if the number on the hundred is 0, the number of 1 on the hundred may be determined by the higher. For example: 12013, you can know that hundreds of 1 of the situation may be: 100~199,1100~1199,2100~2199,,...,11100~11199, altogether 1200. It can be seen that the higher digits (12) are determined and equal to the higher number (12) multiplied by the current number (100). ② if the number on the hundred is 1, the number of 1 on the hundred may not only be affected by the higher levels but also by the lows. For example: 12113, you can know that the impact of hundreds of high levels of the situation is: 100~199,1100~1199,2100~2199,,....,11100~11199, altogether 1200. Same as above, and equal to the higher number (12) multiplied by the current number (100). But at the same time it is also affected by the low, hundreds of 1 of the situation is: 12100~12113, a total of 114, equal to the low number (113) +1. ③ if the number on the hundred is greater than 1 (2~9), the 1 on the hundred is only determined by a higher level, such as 12213, then 1 of the Hundred appears: 100~199,1100~1199,2100~2199,...,11100~11199,12100~12199, A total of 1300, and equal to the higher number +1 (12+1) multiplied by the current number of digits (100). */


16. Enter a positive integer array, put all the numbers in the array together to form a number, printing can be spliced out of the smallest of all the numbers. For example, enter the array {3,32,321}, and print out the smallest number that the three numbers can line up to be 321323.

Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.Comparator;
public class Solution {public
    String printminnumber (int [] numbers) {
     if (numbers.length==0| | Numbers==null) {return
            "";
        }
        string[] str = new String[numbers.length];
        StringBuilder ss = new StringBuilder ();
        for (int i=0;i<numbers.length;i++) {
            str[i]=string.valueof (numbers[i]);
        }
        Arrays.sort (str,new comparator<string> () {public
        	int compare (string str1,string str2) {
                string s1= STR1+STR2;
                String s2=str2+str1;
                return S1.compareto (S2);
            }
        );
        for (int i=0;i<str.length;i++) {
            ss.append (str[i]);
        }
        return ss.tostring ();
    }
Parsing: The key is to define the comparison way, Array (Str,new comparator<string>), after this function,
The smallest book is in the form of an array in str (sorted) and then placed in the SS.


17. The number containing only factors 2, 3 and 5 is called the Ugly number. For example, 6, 8 are ugly numbers, but 14 is not because it contains factor 7. We used to think of 1 as the first ugly number. Find the nth number of ugly numbers in the order of small to large.

Import java.util.ArrayList;
public class Solution {public
    int getuglynumber_solution (int index) {
        if (index<7) {return
            index;
        }
        arraylist<integer> list = new arraylist<integer> ();
        List.add (0,1);
        int a=0,b=0,c=0;
        for (int i=1;i<index;i++) {
            list.add (i,min (List.get (a) *2,min (List.get (b) *3,list.get (c) *5)));
            if (List.get (i) ==list.get (a) *2) a++;
            if (List.get (i) ==list.get (b) *3) b++;
            if (List.get (i) ==list.get (c) *5) C + +;
        }
        Return List.get (index-1);
    }
    int min (int i,int j) {return
        (i<j) i:j;
    }
}
Parsing: The ugly number is actually 1 respectively *2,*3,*5 and these numbers are *2,*3,*5 after the number of the set (7 before all the ugly), and each ugly number is *2,*3,*5 after the ugly number, so first put 1 into the list, and then start the loop, a point to the number of the current number * 2 is not added to the list, B points to the number indicates that the current number *3 has not been added to the list, C points to the number of the current number of *5 has not been added to the list.


18. Finds the first occurrence of a character in a string (1<= string length <=10000, all made up of uppercase letters), and returns its position

Import Java.util.HashSet;
public class Solution {public
    int Firstnotrepeatingchar (String str) {
        if (str.length () ==0) {
            return-1;
        }
        hashset<character> set = new Hashset<character> ();
        char [] strlist = Str.tochararray ();
        for (int i=0;i<str.length (); i++) {
        	if (Set.contains (strlist[i)) {
        		continue;
        	} else{
        		Int J;
        		int flag=0;
        		For (J=i+1;j<str.length (); j + +) {
        			if (Strlist[i]==strlist[j]) {
        				set.add (strlist[i]);
        				flag=1;
        				break;
        			}
        		}
        		if (flag==0) {return
        			i
        }}} Return-1
    }
}

The idea is to put the repeated numbers into the list, the first number that appears in the list, and then iterate through all the numbers after the number, and if there are duplicates, continue to add the list, and if not, return the number.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.