劍指Offer【31-40】Java實現

31、求出1~13的整數中1出現的次數,並算出100~1300的整數中1出現的次數?爲此他特別數了一下1~13中包含1的數字有1、10、11、12、13因此共出現6次,但是對於後面問題他就沒轍了。ACMer希望你們幫幫他,並把問題更加普遍化,可以很快的求出任意非負整數區間中1出現的次數(從1 到 n 中1出現的次數)。

public class Solution {
    public int NumberOf1Between1AndN_Solution(int n) {
        int num = 0;
        for(int i=0;i<=n;i++){
            String str = String.valueOf(i);
            for(int j=0;j<str.length();j++){
                if(str.charAt(j) == '1'){
                    num++;
                }
            }
        }
        return num;
    }
}

32、輸入一個正整數數組,把數組裏所有數字拼接起來排成一個數,打印能拼接出的所有數字中最小的一個。例如輸入數組{3,32,321},則打印出這三個數字能排成的最小數字爲321323。

/**
 * 輸入一個正整數數組,把數組裏所有數字拼接起來排成一個數,
 * 打印能拼接出的所有數字中最小的一個。
 * 例如輸入數組{3,32,321},則打印出這三個數字能排成的最小數字爲321323。
 * @author zhx
 *
 */
public class Solution {
    public String PrintMinNumber(int [] numbers) {
        String str = "";
        for(int i=0;i<numbers.length;i++){
            int min = numbers[i];
            int flag = i;
            for(int j=i+1;j<numbers.length;j++){
                if(cmp(numbers[j],min)){
                    min = numbers[j];
                    flag = j;
                }

            }
            int tmp = numbers[i];
            numbers[i] = min;
            numbers[flag] = tmp;
        }
        for(int i=0;i<numbers.length;i++){
            str = str + numbers[i];
        }
        return str;
    }

    private boolean cmp(int value1, int value2) {
        // TODO Auto-generated method stub
        /**
         * 321 32
         * 32132 32321
         */
        String str1 = "" + value1 + value2;
        String str2 = "" + value2 + value1;
        Integer int1 = Integer.valueOf(str1);
        Integer int2 = Integer.valueOf(str2);
        if(int1.intValue()<int2.intValue()){
            return true;
        }
        return false;
    }
    public static void main(String args[]){
        int arr[] = {3,5,1,4,2};
        System.out.println(new Solution().PrintMinNumber(arr));
    }

}

33、把只包含質因子2、3和5的數稱作醜數(Ugly Number)。例如6、8都是醜數,但14不是,因爲它包含質因子7。 習慣上我們把1當做是第一個醜數。求按從小到大的順序的第N個醜數。

34、在一個字符串(0<=字符串長度<=10000,全部由字母組成)中找到第一個只出現一次的字符,並返回它的位置, 如果沒有則返回 -1.

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

/**
 * 在一個字符串(0<=字符串長度<=10000,全部由字母組成)中
 * 找到第一個只出現一次的字符,並返回它的位置, 如果沒有則返回 -1.
 * @author zhx
 *
 */
public class Solution {
    public int FirstNotRepeatingChar(String str) {
        if(str.length() == 0){
            return -1;
        }
        LinkedHashMap<Character,Integer> hs = new LinkedHashMap<>();
        for(int i=0;i<str.length();i++){
            char c = str.charAt(i);
            if(!hs.containsKey(c)){
                hs.put(c, 1);
            }
            else{
                int value = hs.get(c);
                hs.put(c, value+1);
            }
        }
        char ch=' ';
        for(char c:hs.keySet()){
            if(hs.get(c) == 1){
                ch = c;
                break;
            }
        }
        int index = -1;
        for(int i=0;i<str.length();i++){
            if(ch == str.charAt(i)){
                index = i;
            }
        }
        return index;
    }
}

35、在數組中的兩個數字,如果前面一個數字大於後面的數字,則這兩個數字組成一個逆序對。輸入一個數組,求出這個數組中的逆序對的總數P。並將P對1000000007取模的結果輸出。 即輸出P%1000000007

36、輸入兩個鏈表,找出它們的第一個公共結點。

import java.util.LinkedHashSet;

/**
 * 輸入兩個鏈表,找出它們的第一個公共結點
 * @author zhx
 *
 */
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode node = null;
        LinkedHashSet hs = new LinkedHashSet();
        while(pHead1!=null){
            hs.add(pHead1);
            pHead1 = pHead1.next;
        }
        while(pHead2!=null){
            if(hs.contains(pHead2)){
                node = pHead2;
                break;
            }
            pHead2 = pHead2.next;
        }
        return node;
    }
}

37、統計一個數字在排序數組中出現的次數。

import java.util.Arrays;

/**
 * 統計一個數字在排序數組中出現的次數。
 * @author zhx
 *
 */
public class Solution {
    public int GetNumberOfK(int [] array , int k) {
       int index = Arrays.binarySearch(array, k);
       if(index < 0){
           return 0;
       }
       int num = 0;
       for(int i = index;i<array.length;i++){
           if(k == array[i]){
               num++;
           }
           else{
               break;
           }
       }
       for(int i = index;i>=0;i--){
           if(k == array[i]){
               num++;
           }
           else{
               break;
           }
       }
       return num-1;
    }
    public static void main(String args[]){
        int[] arr = {1,2,3,3,3,3,4,5};
        System.out.print(new Solution().GetNumberOfK(arr, 6));
    }
}

38、輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度爲樹的深度。

import java.util.LinkedList;

/**
 * 輸入一棵二叉樹,求該樹的深度。
 * 從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度爲樹的深度。
 * @author zhx
 *
 */
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        int num = 0;
        list.offer(root);
        TreeNode last = root;
        TreeNode nlast = null;
        while(!list.isEmpty()){
            TreeNode node = list.poll();
            if(node.left!=null){
                list.add(node.left);
                nlast = node.left;
            }
            if(node.right!=null){
                list.add(node.right);
                nlast = node.right;
            }
            if(node == last&&!list.isEmpty()){
                num++;
                last = nlast;
            }
        }
        return num+1;
    }
}

39、輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。

/**
 * 輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。
 * @author zhx
 *
 */
class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null){
            return true;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        if(height(left) - height(right) > 1 || height(right) - height(left) > 1){
            return false;
        }
        return IsBalanced_Solution(left)&&IsBalanced_Solution(right);
    }
    public static int height(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        int leftheight = 0;
        int rightheight = 0;
        leftheight = height(root.left);
        rightheight = height(root.right);
        return leftheight<rightheight?rightheight+1:leftheight+1;
    }
}

40、一個整型數組裏除了兩個數字之外,其他的數字都出現了偶數次。請寫程序找出這兩個只出現一次的數字。

import java.util.Arrays;
import java.util.Stack;

/**
 * 一個整型數組裏除了兩個數字之外,其他的數字都出現了偶數次。
 * 請寫程序找出這兩個只出現一次的數字。
 * @author zhx
 *
 */
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        Arrays.sort(array);
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i<array.length;i=i+1){
            if(stack.isEmpty()){
                stack.add(array[i]);
            }
            else{
                if(stack.peek() == array[i]){
                    stack.pop();
                    continue;
                }
                else{
                    stack.add(array[i]);
                }
            }
        }
        num1[0] = stack.pop();
        num2[0] = stack.pop();
    }
    public static void main(String args[]){
        int arr[] = {2,4,3,6,3,2,5,5};
        Solution s = new Solution();
        int[] num1 = new int[1];
        int[] num2 = new int[1];
        s.FindNumsAppearOnce(arr, num1, num2);
        System.out.println(num1[0]);
        System.out.println(num2[0]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章