劍指offer 34-66題

面試題34:二叉樹中和為某一值的路徑

import java.util.Stack;
public class test34 {
    public static Stack<Node> path = new Stack<>();
    public static class Node{
        int item;
        Node left, right;
        public Node(int item){
            this.item = item;
        }
    }
    public static void findPath(Node h, int target, int weight){
        if (h == null || weight > target) return;
        path.push(h);
        weight += h.item;
        if (weight == target && h.left == null && h.right == null) {
            for (Node x: path){
                System.out.printf("%d ", x.item);
            }
            System.out.println();
            path.pop();
        }
        else if (weight < target){
            findPath(h.left,target,weight);
            findPath(h.right,target,weight);
            path.pop();
        }
    }
    public static void main(String[] args){
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(12);
        root.left.left = new Node(4);
        root.left.right = new Node(7);
        findPath(root,22,0);
    }
}

面試題35:復(fù)雜鏈表的復(fù)制

public class test35 {
    public class Node{
        int val;
        Node next, random;
    }
    public static Node clone(Node h) {
        if (h == null) return null;
        Node current = h;
        while (current != null) {
            Node temp = new Node(current.item);
            temp.next = current.next;
            current.next = temp;
            current = temp.next;
        }
        current = h;
        while (current != null) {
            Node temp = current.next;
            if (current.random != null) {
                temp.random = current.random.next;
            }
            current = temp.next;
        }
        current = h;
        Node cloneHead = current.next;
        Node clonecurrent = current.next;
        while (current != null) {
            current.next = clonecurrent.next;
            clonecurrent.next = current.next.next;
            current = current.next;
            clonecurrent = clonecurrent.next;
        }
        return cloneHead;
    }
}

面試題36:二叉搜索樹與雙向鏈表

public class test36 {
    private static Node root;
    private static class Node{
        int item;
        Node left, right;
        public Node(int item){
            this.item = item;
        }
    }
    public static Node convert(Node h){
        if (h == null) return null;
        Node last = null;
        last = convert(h, last);
        Node head = last;
        while (head.left != null)
            head = head.left;
        return head;
    }
    public static Node convert(Node root, Node last) {
        if (root == null) return last;
        last = convert(root.left, last);
        if (last != null) {
            last.right = root;
            root.left = last;
        }
        last = root;
        last = convert(root.right, last);
        return last;
    }
}

面試題37:序列化二叉樹

import java.util.LinkedList;
import java.util.Queue;
public class test37 {
    public static class TreeNode{
        int val;
        TreeNode left, right;
        public TreeNode(int val){
            this.val = val;
        }
    }
    public static String Serialize(TreeNode h){
        if (h == null) return null;
        StringBuilder str = new StringBuilder();
        Serialize(h, str);
        return str.toString();
    }
    public static void Serialize(TreeNode h, StringBuilder str){
        if (h == null){
            str.append("#,");
            return;
        }
        str.append(h.val + ",");
        Serialize(h.left, str);
        Serialize(h.right, str);
    }
    public static void Deserialize(String str){
        if (str == null) return;
        String[] string = str.split(",");
        Queue<String> queue = new LinkedList<>();
        for (int i = 0; i < string.length; i++){
            queue.add(string[i]);
        }
        Deserialize(queue);
    }
    public static TreeNode Deserialize(Queue<String> queue){
        if (queue.isEmpty()) return null;
        String s = queue.poll();
        if (s.equals("#")) return null;
        TreeNode root = new TreeNode(Integer.valueOf(s));
        root.left = Deserialize(queue);
        root.right = Deserialize(queue);
        return root;
    }
}

面試題38:字符串的排列

類似的字符串排列參見面試題17的方案2

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class test38 {
    public ArrayList<String> Permutation(String str){
        if (str == null) return null;
        List<String> list = new ArrayList<>();
        char[] s = str.toCharArray();
        resolve(s, 0, list);
        Set<String> set = new TreeSet<>(list);
        list = new ArrayList<>(set);
        return (ArrayList<String>) list;
    }
    private void resolve(char[] str, int index, List<String> list){
        if (index == str.length-1){
            list.add(String.valueOf(str));
            return;
        }
        for (int i = index; i < str.length; i++){
            char temp = str[index];
            str[index] = str[i];
            str[i] = temp;
            resolve(str, index+1, list);
            temp = str[index];
            str[index] = str[i];
            str[i] = temp;
        }
    }
    public static void main(String[] args){
        String str = new String("abc");
        for (String s: new Solution().Permutation(str))
            System.out.println(s);
    }
}

還有一種解法,用到了隊(duì)列和棧

public class test38 {
    
    public static void main(String[] args) {
        String input = "abc";
        Queue<Character> queue = new LinkedList<>();
        for (int i = 0; i < input.length(); i++) {
            queue.add(input.charAt(i));
        }
        permutation(queue, new Stack<>());
    }

    public static void permutation(Queue<Character> queue, Stack<Character> stack) {
        if (queue.isEmpty()) {
            System.out.println(stack);
        }
        for (int i = 0; i < queue.size(); i++) {
            char element = queue.poll();
            stack.push(element);
            permutation(queue, stack);
            queue.add(element);
            stack.pop();
        }
    }
    
}
擴(kuò)展問題:求字符的所有組合
public class test38 {
    
    public static void main(String[] args) {
        String input = "abc";
        for (int j = 1; j <= input.length(); j++) {
            Queue<Character> queue = new LinkedList<>();
            for (int i = 0; i < input.length(); i++) {
                queue.add(input.charAt(i));
            }
            permutation2(j, queue, new Stack<>());
        }
    }

    public static void permutation2(int N, Queue<Character> queue, Stack<Character> stack) {
        if (N == 0) {
            System.out.println(stack);
            return;
        }
        if (queue.isEmpty()) {
            return;
        }
        char element = queue.poll();
        stack.push(element);
        permutation2(N-1, queue, stack);
        stack.pop();
        permutation2(N, queue, stack);
    }
}

面試題39:數(shù)組中出現(xiàn)次數(shù)超過一半的數(shù)字

public class test39 {
    public static void main(String[] args){
        int[] a = new int[]{1,2,3,2,2,2,5,4,2};
        resolve(a);
    }
    public static void resolve(int[] a){
        int count = 1;
        int number = a[0];
        for (int i = 1; i < a.length; i++){
            if (a[i] == number) {
                count++;
            }
            else {
                count--;
                if (count == 0){
                    number = a[i];
                    count = 1;
                }
            }
        }
        if (!CheckMoreThanHalf(a, number)) {
            System.out.println("null");
            return;
        }
        System.out.println(number);
    }
    public static boolean CheckMoreThanHalf(int[] a, int number){
        int count = 0;
        for (int i = 0; i < a.length; i++){
            if (a[i] == number)
                count++;
            if (count*2>=a.length)
                return true;
        }
        return false;
    }
}

面試題40:最小的k個數(shù)

方案1:基于快速排序的partition方法胖眷,時間復(fù)雜度為O(n)

public class test40 {
    public static void main(String[] args){
        int[] a = new int[]{4,5,1,6,2,7,3,8};
        int n = 4;
        int start = 0, end = a.length-1;
        int index = partition(a, start, end);
        while (index != n){
            if (index < n){
                end = index - 1;
                index = partition(a, start, end);
            }
            else {
                start = index + 1;
                index = partition(a, start, end);
            }
        }
    }
    public static int partition(int[] a, int lo, int hi){
        int v = a[lo];
        int i = lo, j = hi+1;
        while (true){
            while (a[++i] < v){
                if (i == hi) break;
            }
            while (v < a[--j]){
                if (j == lo) break;
            }
            if (i >= j) break;
            exch(a, i, j);
        }
        exch(a, lo, j);
        return j;
    }
    public static void exch(int[] a, int i, int j){
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

方案2:基于紅黑樹或優(yōu)先隊(duì)列武通,適合于海量數(shù)據(jù),時間復(fù)雜度為O(nlogk)

import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Queue;
public class test40 {
    public static ArrayList<Integer> main(String[] args){
        int[] a = new int[]{4,5,1,6,2,7,3,8};
        int n = 4;
        return resolve(a, 4);
    }
    public static ArrayList<Integer> resolve(int[] a, int k){
        ArrayList<Integer> list = new ArrayList<>();
        if (a == null || k <= 0 || k > a.length) return null;
        Queue<Integer> queue = new PriorityQueue<>(k, Collections.reverseOrder());
        for (int i = 0; i < a.length; i++){
            if (queue.size() < k){
                queue.add(a[i]);
            }
            else {
                if (a[i] < queue.peek()){
                    queue.remove();
                    queue.add(a[i]);
                }
            }
        }
        for (int i: queue){
            list.add(i);
        }
        return list;
    }
}

面試題41:數(shù)據(jù)流中的中位數(shù)

public class Solution {
    BST tree = new BST();
    static final boolean RED = true, BLACK = false;

    public void Insert(Integer num) {
        tree.put(num);
    }

    public Double GetMedian() {
        return tree.getmid();
    }
    
    public class BST{
        private Node root;
        private class Node{
            int val;
            Node left, right;
            int N;
            boolean color;
            public Node(int val, int N, boolean color){
                this.val = val;
                this.N = N;
                this.color = color;
            }
        }
        public boolean isEmpty(){return root == null;}
        public boolean isRED(Node h){
            if (h == null) return false;
            return h.color == RED;
        }
        public int size(Node h){
            if (h == null) return 0;
            return h.N;
        }
        public double getmid(){
            if (root == null) return 0.0;
            int l = size(root.left);
            int r = size(root.right);
            int mid = (l+r+1)/2;
            if ((l+r+1)%2!=0) return (double) select(mid);
            else return (double) (select(mid-1)+select(mid))/2;
        }
        public int select(int k){
            return select(root, k);
        }
        public int select(Node h, int k){
            if (h == null) return 0;
            int t = size(h.left);
            if (t > k) return select(h.left, k);
            else if (t == k) return h.val;
            else {
                return select(h.right, k-t-1);
            }
        }
        public Node rotateLeft(Node h){
            Node x = h.right;
            h.right = x.left;
            x.left = h;
            x.N = h.N;
            x.color = h.color;
            h.N = size(h.left) + size(h.right) + 1;
            h.color = RED;
            return x;
        }
        public Node rotateRight(Node h){
            Node x = h.left;
            h.left = x.right;
            x.right = h;
            x.N = h.N;
            x.color = h.color;
            h.N = size(h.left) + size(h.right) + 1;
            h.color = RED;
            return x;
        }
        public void flipColors(Node h){
            h.color = !h.color;
            h.left.color = !h.left.color;
            h.right.color = !h.right.color;
        }
        public void put(int val){
            root = put(root, val);
            root.color = BLACK;
        }
        private Node put(Node h, int val){
            if (h == null) return new Node(val, 1, RED);
            if (val < h.val) h.left = put(h.left, val);
            else if (val == h.val) h.val = val;
            else h.right = put(h.right, val);
            if (!isRED(h.left) && isRED(h.right)) h = rotateLeft(h);
            if (isRED(h.left) && isRED(h.left.left)) h = rotateRight(h);
            if (isRED(h.left) && isRED(h.right)) flipColors(h);
            h.N = size(h.left) + size(h.right) + 1;
            return h;
        }
    }
}

面試題42:連續(xù)子數(shù)組的最大和

方案1:舉例分析數(shù)組規(guī)律

public class test42 {
    public static void main(String[] args){
        int[] a = new int[]{1,-2,3,10,-4,7,2,-5};
        resolve(a);
    }
    public static void resolve(int[] a){
        if (a == null){
            System.out.println(0);
            return;
        }
        int maxsum = a[0];
        int sum = a[0];
        for (int i = 1; i < a.length; i++){
            if (sum <= 0){
                sum = a[i];
            }
            else {
                sum += a[i];
            }
            if (sum > maxsum){
                maxsum = sum;
            }
        }
        System.out.println(maxsum);
    }
}

方案2:動態(tài)規(guī)劃

public class test42 {
    public static void main(String[] args){
        int[] a = new int[]{1,-2,3,10,-4,7,2,-5};
        resolve(a);
    }
    public static void resolve(int[] a){
        if (a == null) return;
        int[] dp = new int[a.length];
        for (int i = 0; i < a.length; i++){
            if (i == 0 || dp[i-1] < 0) dp[i] = a[i];
            else dp[i] = dp[i-1] + a[i];
        }
        int max = dp[0];
        for (int i = 1; i < a.length; i++){
            if (dp[i] > max)
                max = dp[i];
        }
        System.out.println(max);
    }
}

面試題43:1~n整數(shù)中1出現(xiàn)的次數(shù)

public class test43 {
    public static void main(String[] args){
        System.out.println(NumberOf1Between1AndN_Solution(12));
    }
    public static int NumberOf1Between1AndN_Solution(int n) {
        int ones = 0;
        for (int m = 1; m <= n; m*=10){
            int a = n/m, b = n%m;
            if (a%10==0) ones += a/10 * m;
            else if (a%10==1) ones += (a/10*m) + (b+1);
            else ones += (a/10+1)*m;
        }
        return ones;
    }
}

面試題44:數(shù)字序列中某一位的數(shù)字

面試題45:把數(shù)組排成最小的數(shù)

面試題46:把數(shù)字翻譯成字符串

面試題47:禮物的最大價值(動態(tài)規(guī)劃)

public class test47 {
    public static void main(String[] args){
        int[][] a = new int[][]{{1,10,3,8},{12,2,9,6},{5,7,4,11},{3,7,16,5}};
        resolve(a);
    }
    public static void resolve(int[][] a){
        if (a == null) return;
        int row = a.length, col = a[0].length;
        for (int i = row - 2; i >= 0; i--){
            a[i][col-1] += a[i+1][col-1];
        }
        for (int j = col - 2; j >= 0; j--){
            a[row-1][j] += a[row-1][j+1];
        }
        for (int i = row-2; i >= 0; i--){
            for (int j = col-2; j >= 0; j--){
                a[i][j] += Integer.max(a[i+1][j], a[i][j+1]);
            }
        }
        System.out.println(a[0][0]);
    }
}

面試題48:最長不含重復(fù)字符的子字符串(動態(tài)規(guī)劃)

public class test48 {
    public static void main(String[] args){
        String str = new String("arabcacfr");
        resolve(str);
    }
    public static void resolve(String str){
        if (s == null) {
            return -1;
        }
        int[] positionIndex = new int[26];
        for (int i = 0; i < 26; i++) {
            positionIndex[i] = -1;
        }
        int[] dp = new int[s.length()];
        dp[0] = 1;
        positionIndex[s.charAt(0) - 'a'] = 0;
        for (int i = 1; i < s.length(); i++) {
            int dist = i - positionIndex[s.charAt(i) - 'a'];
            if (positionIndex[s.charAt(i) - 'a'] == -1 || dist > dp[i-1]) {
                dp[i] = dp[i-1] + 1;
            } else if (dist == dp[i-1]){
                dp[i] = dp[i-1];
            } else {
                dp[i] = dist;
            }
            positionIndex[s.charAt(i) - 'a'] = i;
        }
        int max = dp[0];
        for (int i = 1; i < s.length(); i++) {
            if (dp[i] > max) {
                max = dp[i];
            }
        }
        return max;
    }
}

面試題49:丑數(shù)

public class test49 {
    public static void main(String[] args){
        int n = 1500;
        resolve(n);
    }
    public static void resolve(int n){
        if (n <= 0) return;
        int[] a = new int[n];
        a[0] = 1;
        int t2 = 0, t3 = 0, t5 = 0;
        for (int i = 1; i < n; i++){
            a[i] = Integer.min(a[t2]*2, Integer.min(a[t3]*3, a[t5]*5));
            if (a[i] == a[t2]*2) t2++;
            if (a[i] == a[t3]*3) t3++;
            if (a[i] == a[t5]*5) t5++;
        }
        System.out.println(a[n-1]);
    }
}

面試題50:第一次只出現(xiàn)一次的字符

無論是字符串還是字符流中第一次只出現(xiàn)一次的字符珊搀,解決辦法都基于哈希表冶忱,時間復(fù)雜度為O(n),空間復(fù)雜度為O(1)

import java.util.LinkedHashMap;
public class test50 {
    public static void main(String[] args){
        String str = new String("abaccdeff");
        resolve(str);
    }
    public static void resolve(String str){
        if (str == null) return;
        char[] s = str.toCharArray();
        LinkedHashMap<Character, Integer> hash = new LinkedHashMap<>();
        for (int i = 0; i < s.length; i++){
            if (hash.containsKey(s[i])){
                hash.put(s[i], hash.get(s[i])+1);
            }
            else hash.put(s[i], 1);
        }
        for (char key: hash.keySet()){
            if (hash.get(key) == 1){
                System.out.println(key);
                return;
            }
        }
    }
}

面試題51:數(shù)組中的逆序?qū)?/h1>

自底向上的歸并排序中的子問題

public class test51 {
    public static class Merge{
        int count = 0;
        public int[] aux;
        public void sort(int[] a){
            if (a == null) return;
            aux = new int[a.length];
            sort(a, 0, a.length-1);
        }
        private void sort(int[] a, int lo, int hi){
            if (lo >= hi) return;
            int mid = lo+(hi-lo)/2;
            sort(a,lo,mid);
            sort(a,mid+1,hi);
            merge(a, lo, mid, hi);
        }
        private void merge(int[] a, int lo, int mid, int hi){
            int i = lo, j = mid+1;
            for (int k = lo; k <= hi; k++){
                aux[k] = a[k];
            }
            for (int k = lo; k <= hi; k++){
                if (i == mid+1) a[k] = aux[j++];
                else if (j == hi+1) a[k] = aux[i++];
                else if (aux[i] > aux[j]) {
                    count+=(j-k);
                    a[k] = aux[j++];
                }
                else {
                    a[k] = aux[i++];
                }
            }
        }
    }
    public static void main(String[] args){
        int[] a = new int[]{7,5,6,4};
        Merge merge = new Merge();
        merge.sort(a);
        System.out.println(merge.count);
    }
}

面試題52:兩個鏈表的第一個公共節(jié)點(diǎn)

public class test52 {
    public static class Node{
        int key;
        Node next;
    }
    public static void main(String[] args){
        Node node1 = new Node();
        Node node2 = new Node();
        Node same = resolve(node1, node2);
    }
    public static Node resolve(Node node1, Node node2){
        if (node1 == null || node2 == null) return null;
        Node current = node1;
        int length1 = 1, length2 = 1;
        while (current.next != null){
            length1++;
            current = current.next;
        }
        current = node2;
        while (current.next != null){
            length2++;
            current = current.next;
        }
        for (int i = 0; i < Math.abs(length1-length2); i++){
            if (length1 > length2){
                node1 = node1.next;
            }
            else if (length2 > length1){
                node2 = node2.next;
            }
        }
        while (node1.next != null && node2.next != null && node1 != node2){
            node1 = node1.next;
            node2 = node2.next;
        }
        return node1;
    }
}

面試題53:在排序數(shù)組中查找數(shù)字

題目一:數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)

public class test53 {
    public static void main(String[] args){
        int[] a = new int[]{1};
        int target = 3;
        System.out.println(resolve(a, target));
    }
    public static int resolve(int[] a, int n){
        if (a == null) return -1;
        int first = getFirst(a, n, 0, a.length-1);
        int last = getLast(a, n, 0, a.length-1);
        if (first <= last && first >= 0 && last >= 0) return last - first + 1;
        else return -1;
    }
    public static int getFirst(int[] a, int target, int start, int end){
        while (start <= end){
            int mid = start + (end - start)/2;
            if (a[mid] > target){
                end = mid-1;
                continue;
            }
            else if (a[mid] < target){
                start = mid+1;
                continue;
            }
            else {
                if (a[mid-1] == target){
                    end = mid-1;
                    continue;
                }
                else return mid;
            }
        }
        return -1;

    }
    public static int getLast(int[] a, int target, int start, int end){
        while (start <= end && start>=0 && end>=0){
            int mid = start + (end - start)/2;
            if (a[mid] > target){
                end = mid+1;
                continue;
            }
            else if (a[mid] < target){
                start = mid-1;
                continue;
            }
            else {
                if (a[mid+1] == target){
                    start = mid+1;
                    continue;
                }
                else return mid;
            }
        }
        return -1;
    }
}

題目二:0~n-1中缺失的數(shù)字

public class test53 {
    public static void main(String[] args){
        int[] a = new int[]{};
        System.out.println(resolve(a));
    }
    public static int resolve(int[] a){
        if (a == null) return -1;
        int start = 0, end = a.length-1;
        while (start <= end){
            int mid = start + (end-start)/2;
            if (a[mid] == mid){
                start = mid+1;
                continue;
            }
            else if (a[mid] > mid){
                if (a[mid-1] == mid-1) return mid;
                end = mid-1;
                continue;
            }
        }
        return -1;
    }
}

面試題54:二叉搜索樹的第k大節(jié)點(diǎn)

public class test54 {
    public static class Node{
        int val;
        Node left, right;
    }
    public static void main(String[] args){
        Node root = new Node();
        int k = 4;
        resolve(root, k);
    }
    public static void resolve(Node h, int k){
        if (h == null) return;
        resolve(h.left, k);
        if (--k == 0) System.out.println(h.val);
        resolve(h.right, k);
    }
}

面試題55:二叉樹的深度

題目一:二叉樹的深度

public class test55 {
    public static class Node{
        int val;
        Node left, right;
    }
    public static void main(String[] args){
        Node root = new Node();
        System.out.println(depth(root));
    }
    public static int depth(Node h){
        if (h == null) return 0;
        int l = depth(h.left);
        int r = depth(h.right);
        return l>r?(l+1):(r+1);
    }
}

題目二:平衡二叉樹

public class test55 {
    public static boolean isbalance = true;
    public static class Node{
        int val;
        Node left, right;
    }
    public static void main(String[] args){
        Node root = new Node();
        if (root == null) {
            System.out.println("false");
            return;
        }
        System.out.println(isBalanced(root, 0));
    }
    public static int isBalanced(Node h, int depth){
        if (h == null) {
            return 0;
        }
        int l = isBalanced(h.left, depth);
        int r = isBalanced(h.left, depth);
        if (Math.abs(l-r) > 1){
            isbalance = false;
        }
        return l>r?(l+1):(r+1);
    }
}

面試題56:數(shù)組中數(shù)字出現(xiàn)的次數(shù)

題目一:數(shù)組中只出現(xiàn)一次的兩個數(shù)字

public class test56 {
    public static void main(String[] args){
        int[] a = new int[]{2,4,3,6,3,2,5,5};
        resolve(a);
    }
    public static void resolve(int[] a){
        if (a==null) return;
        int result = 0;
        for (int i = 0; i < a.length; i++){
            result ^= a[i];
        }
        int index = find(result);
        int num1 = 0, num2 = 0;
        for (int i = 0; i < a.length; i++){
            if (isbit(a[i], index)) num1 ^= a[i];
            else num2 ^= a[i];
        }
        System.out.printf("%d, %d", num1, num2);
    }
    public static int find(int num){
        int index = 0;
        while ((num&1)==0 && index < 32){
            num=num>>1;
            index++;
        }
        return index;
    }
    public static boolean isbit(int num, int index){
        num = num >> index;
        return (num&1) == 0;
    }
}

題目二:數(shù)組中唯一只出現(xiàn)一次的數(shù)字

public class test56 {
    public static void main(String[] args){
        int[] a = new int[]{2,4,4,4,3,3,3,5,5,5};
        resolve(a);
    }
    public static void resolve(int[] a) {
        if (a == null) return;
        int[] result = new int[32];
        for (int i = 0; i < a.length; i++){
            int bit = 1;
            for (int j = 31; j >= 0; j--){
                if ((a[i]&bit) != 0){
                    result[j]+=1;
                }
                bit=bit<<1;
            }
        }
        int num = 0;
        for (int i = 0; i < result.length; i++){
            if (result[i] % 3 != 0){
                num++;
                num=num<<1;
            }
        }
        System.out.println(num);
    }
}

面試題57:和為s的數(shù)字

題目一:和為s的兩個數(shù)字

public class test57 {
    public static void main(String[] args){
        int[] a = new int[]{1,2,4,7,11,15};
        int n = 15;
        resolve(a, n);
    }
    public static void resolve(int[] a, int n){
        if (a == null || a[0]+a[a.length-1] < n || a[0] > n) return;
        int lo = 0, hi = a.length-1;
        while (lo < hi){
            if (a[lo] + a[hi] == n){
                System.out.printf("%d, %d", a[lo], a[hi]);
                return;
            }
            else if (a[lo] + a[hi] < n){
                lo++;
                continue;
            }
            else {
                hi--;
                continue;
            }
        }
        return;
    }
}

題目二:和為s的連續(xù)正數(shù)序列

public class test57 {
    public static void main(String[] args){
        int n = 15;
        resolve(n);
    }
    public static void resolve(int n){
        if (n<=2) return;
        int lo = 1, hi = 2;
        while (lo < hi && hi < n){
            if (sum(lo, hi) == n){
                System.out.printf("%d~%d\n", lo, hi);
                hi++;
            }
            else if (sum(lo, hi) < n){
                hi++;
                continue;
            }
            else {
                lo++;
                continue;
            }
        }
        return;
    }
    public static double sum(int lo, int hi){
        int count = hi - lo + 1;
        return (lo+hi)*count/2;
    }
}

面試題58:翻轉(zhuǎn)字符串

題目一:翻轉(zhuǎn)單詞順序

public class test58 {
    public static void main(String[] args){
        String s = new String("I am a student.");
        resolve(s);
    }
    public static void resolve(String s){
        if (s == null) return;
        String[] str = s.split(" ");
        for (int i = str.length-1; i >= 0; i--){
            System.out.print(str[i]);
            System.out.print(" ");
        }
    }
}

題目二:左旋轉(zhuǎn)字符串

public class test58 {
    public static void main(String[] args){
        String s = new String("abcdefg");
        int n = 2;
        resolve(s, n);
    }
    public static void resolve(String s, int n){
        if (s == null || n < 0 || n > s.length()) return;
        char[] string = s.toCharArray();
        Reverse(string, 0, n-1);
        Reverse(string, n, string.length-1);
        Reverse(string, 0, string.length-1);
        System.out.print(String.valueOf(string));
    }
    public static void Reverse(char[] string, int lo, int hi){
        while (lo < hi){
            char temp = string[lo];
            string[lo] = string[hi];
            string[hi] = temp;
            lo++;
            hi--;
        }
    }
}

面試題63:股票的最大利潤

public class test63 {

    public static void main(String[] args) {
        int[] input = new int[]{9, 11, 8, 5, 7, 12, 16, 14};
        int max = getMaxDiff(input);
        System.out.println(max);
    }

    public static int getMaxDiff(int[] input) {
        if (input == null || input.length <= 1) {
            return -1;
        }
        int min = input[0];
        int maxDiff = -1;
        for (int i = 1; i < input.length; i++) {
            if (input[i] - min > maxDiff) {
                maxDiff = input[i] - min;
            }
            if (input[i] < min) {
                min = input[i];
            }
        }
        return maxDiff;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末境析,一起剝皮案震驚了整個濱河市囚枪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌劳淆,老刑警劉巖链沼,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異沛鸵,居然都是意外死亡括勺,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門谒臼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來朝刊,“玉大人,你說我怎么就攤上這事蜈缤∈懊ィ” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵底哥,是天一觀的道長咙鞍。 經(jīng)常有香客問我,道長趾徽,這世上最難降的妖魔是什么续滋? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮孵奶,結(jié)果婚禮上疲酌,老公的妹妹穿的比我還像新娘。我一直安慰自己了袁,他們只是感情好朗恳,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著载绿,像睡著了一般粥诫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上崭庸,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天怀浆,我揣著相機(jī)與錄音谊囚,去河邊找鬼。 笑死执赡,一個胖子當(dāng)著我的面吹牛镰踏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播搀玖,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼余境,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了灌诅?” 一聲冷哼從身側(cè)響起芳来,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎猜拾,沒想到半個月后即舌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挎袜,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年顽聂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盯仪。...
    茶點(diǎn)故事閱讀 38,716評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡紊搪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出全景,到底是詐尸還是另有隱情耀石,我是刑警寧澤,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布爸黄,位于F島的核電站滞伟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏炕贵。R本人自食惡果不足惜梆奈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望称开。 院中可真熱鬧亩钟,春花似錦、人聲如沸鳖轰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脆霎。三九已至总处,卻和暖如春狈惫,著一層夾襖步出監(jiān)牢的瞬間睛蛛,已是汗流浹背鹦马。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留忆肾,地道東北人荸频。 一個月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像客冈,于是被迫代替她去往敵國和親旭从。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容