Sword refers to the offer topic JAVA version of Ideas and code (VII) __java

Source: Internet
Author: User

question 25th: Replication of complex lists
Topic Description:
Enter a complex list (each node has a node value, and two pointers, one point to the next node, and the other special pointer to any node). The result is the head of the complex linked list after replication. (Note that in the output, do not return the node reference in the parameter, or the judge program will return null directly.
Thinking:
First step: Scan the original linked list, the replicated node is connected to the back of the original node; for example: (original list) a->b->c-–> A->a '- >b->b '->c->c '
Second step: Scan the original linked list, if the node has random point, then the corresponding replication node random point to the corresponding replication node; For example, a points to C, then a ' points to C '. Because all are connected to a linked list, all can be found in the time Complexity of O (1).
Step three:
to detach two linked lists, and note the point of the null pointer.
Code:

/* public class Randomlistnode {int label;
    Randomlistnode next = null;

    Randomlistnode random = null;
    Randomlistnode (int label) {this.label = label;
            } */public class Solution {public Randomlistnode Clone (Randomlistnode phead) {if (Phead = null) {
        return null;
        Clone (Phead);
        Random (Phead);
    Return Getrandomlistnode (Phead); public void Clone (Randomlistnode phead) {while (Phead!= null) {Randomlistnode CloneNode = new
            Randomlistnode (Phead.label);
            Clonenode.next = Phead.next;
            Phead.next = CloneNode;
        Phead = Clonenode.next; } public void random (Randomlistnode phead) {while (Phead!= null) {if phead.random!= nul
            L) {pHead.next.random = PHead.random.next;
        } phead = PHead.next.next; } public Randomlistnode Getrandomlistnode (randomlistnOde phead) {randomlistnode P1 = Phead.next;
        Randomlistnode ret = Phead.next;
        Phead.next = P1.next;
        Phead = Phead.next;
            while (Phead!= null) {p1.next = Phead.next;
            P1 = P1.next;
            Phead.next = P1.next;
        Phead = Phead.next;
    return ret; }
}

question 26th: Two fork search tree and bidirectional linked list
Topic Description:
Enter a binary search tree and convert the two-fork search tree into a sorted two-way list. You cannot create any new nodes, you can only adjust the point of the node pointer in the tree.
Idea:
because it is a two-fork search tree, to be converted into a sorted doubly linked list, the sorted list of the binary tree is obtained by sequence traversal.
Code:

/** public class TreeNode {int val = 0;
    TreeNode left = null;

    TreeNode right = null;

    Public TreeNode (int val) {this.val = val;
    } */public class Solution {protected TreeNode lastleft = null;
        Public TreeNode Convert (TreeNode prootoftree) {if (Prootoftree = = null) {return null;
            } if (prootoftree.left = = NULL && Prootoftree.right = null) {lastleft = Prootoftree;
        return prootoftree;
        TreeNode left = Convert (Prootoftree.left);
            if (lastleft!= null) {prootoftree.left = Lastleft;
        Lastleft.right = Prootoftree;
        } lastleft = Prootoftree;
        TreeNode right = Convert (prootoftree.right);
            if (right!= null) {prootoftree.right = right;
        Right.left = Prootoftree; Return to left!= null?
    Left:prootoftree; }
}

question 27th: arrangement of Strings
Topic Description:
Enter a string and print out all 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.
Enter Description:
Enter a string with a length of not more than 9 (which may have characters to repeat), and the character includes only uppercase and lowercase letters.
Idea:
is to simulate an arrangement of the process, such as the string ABC, first of No. 0 number of A,b,c three-letter rotation, A is the first case, B and C combined after output, that is, output ABC,ACB; then B is the first case of AC combined output.
so it's natural to find that the whole process needs to use the Swap function, and it needs to be called recursively.
Code:

Import java.util.ArrayList;
Import java.util.Collections;  public class Solution {public arraylist<string> permutation (String str) {arraylist<string> ret =
        New Arraylist<string> ();
        if (str = NULL | | str.length () = = 0) {return ret;
        Permutation (ret, 0, Str.tochararray ());
        Collections.sort (ret);
    return ret;  public void permutation (arraylist<string> retarraylist, int index, char[] s) {if (index = = s.length-
        1) {Retarraylist.add (new String (s));
                    }else {for (int i = index; i < s.length i++) {if (i = = Index | | s[i]!= S[index]) {
                    Swap (s, index, i);
                    Permutation (Retarraylist, index+1, s);
                Swap (s, index, i);
        }}} public void Swap (char[] s, int begin, int end) {char tmp = S[begin];
        S[begin] = S[end]; s[end] = tmp; } 
}

question 28th: An array of more than half occurrences of the number
Topic Description:
The number of occurrences in the array is more than half the length of the array, please find this number. 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.
Train of thought:
Since this number appears more than half the number, then we can scan the entire array, get a number to record the number, and record the number of times 1, when the number of occurrences of this digit plus 1, when other numbers, if the number of times greater than 1 minus 1, otherwise a number, and record the number as 1, and if the array has a number more than half the number, it is likely to be the last number.
Code:

public class Solution {public int morethanhalfnum_solution (int [] array) {if (array = = NULL | | array.length
        = = 0) {return 0;
        } if (array.length = = 1) {return array[0];
        int tmp = array[0];
        int count = 1;
            for (int i = 1; i<array.length; i++) {if (TMP = = Array[i]) {count + +;
                }else {if (Count > 1) {count-;
                    }else{tmp = Array[i];
                Count = 1; }} if (!
        Checkmorethanhalf (array, tmp)) {return 0;
    return TMP;
        public boolean checkmorethanhalf (int[] array, int tmp) {int count = 0;
        boolean ret = false;
            for (int i = 0; i < Array.Length i++) {if (array[i] = = tmp) {count + +; } if (Count*2 > Array.Length){ret = true;
    return ret; }
}

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.