Prove safety offer: 3-7 Record

Find an array duplicate numbers.


All numbers in an array of length nums n's are in the range of 0 ~ n-1. Some digital array is duplicated, but do not know how many numbers repeat, do not know each number was repeated several times. Please find an array of any one of the duplicate numbers.

Example 1:

Input:
[2, 3, 1, 0, 2, 5, 3]
Output: 2 or 3 

Ideas: a record can be set.

class Solution {
    public int findRepeatNumber(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int num : nums) {
            if (!set.add(num)) {
                return num;
            }
        }
        return -1;
    }
}

In a two-dimensional array of n * m, each row from left to right in order of ascending sort, to sort each column from top to bottom in increasing order. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

 

Example:

Existing matrix multiplied as follows:

[
  [1, 4, 7, 11, 15],
  [2, 5, 8, 12, 19],
  [3, 6, 9, 16, 22],
  [10, 13, 14, 17, 24],
  [ 18, 21, 23, 26, 30]
]
given target = 5, returns true.

Given target = 20, returns false.

 

limit:

0 <= n <= 1000

0 <= m <= 1000

Ideas: from lower left or upper right corner of the investigation, each row or one can be excluded.

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int len = matrix.length;
        int row = 0, column = matrix[0].length - 1;
        while (row < len && column >= 0) {
            int num = matrix[row][column];
            if (num == target) {
                return true;
            } else if (num > target) {
                column--;
            } else {
                row++;
            }
        }
        return false;
    }
}

Please implement a function, replace each space in the string s to "20%."

 

Example 1:

Input: s = "We are happy. "
Output: "We% 20are% 20happy. "

Ideas: From back to front fill, the number of unfilled will not be covered, each letter assigned only once. (Otherwise replace front to back to keep moving behind the character)

class Solution {
    public String replaceSpace(String s) {
        int length = s.length();
        char[] array = new char[length * 3];
        int size = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c == ' ') {
                array[size++] = '%';
                array[size++] = '2';
                array[size++] = '0';
            } else {
                array[size++] = c;
            }
            
        }
        String newStr = new String(array, 0, size);
        return newStr;
    }
}

Enter a head of the list nodes, each node in turn, the return value (Return an array) from the tail to the head.

 

Example 1:

Input: head = [1,3,2]
Output: [2,3,1]
 

limit:

0 <= chain length <= 10000

Idea: put the stack can be poured out.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

class Solution {
    public int[] reversePrint(ListNode head) {
        Stack<ListNode> stack = new Stack<ListNode>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        int size = stack.size();
        int[] arr = new int[size];
        for (int i = 0; i < size; i++) {
            arr[i] = stack.pop().val;
        }
        return arr;
    }
}

And enter the result in a preorder traversal of a binary tree in preorder traversal of the binary tree rebuild. Suppose Results preorder traversal order and input of duplicate numbers are free.

 

For example, given

Preorder traversal preorder = [3,9,20,15,7]
preorder inorder = [9,3,15,20,7]
returns the following binary tree:

    3
   / \
  9  20
    /  \
   15   7
 

limit:

0 <= the number of nodes <= 5000

Ideas:

1, the first element of the first sequence is the sequence root

2, in order to find the root position in the sequence

3, the above process is repeated.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;
        if (n == 0)
            return null;
        int rootVal = preorder[0], rootIndex = 0;
        for (int i = 0; i < n; i++) {
            if (inorder[i] == rootVal) {
                rootIndex = i;
                break;
            }
        }
        TreeNode root = new TreeNode(rootVal);
        root.left = buildTree(Arrays.copyOfRange(preorder, 1, 1 + rootIndex), Arrays.copyOfRange(inorder, 0, rootIndex));
        root.right = buildTree(Arrays.copyOfRange(preorder, 1 + rootIndex, n), Arrays.copyOfRange(inorder, rootIndex + 1, n));

        return root;
    }
}

 

Published 618 original articles · won praise 10000 + · views 1.54 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104742357