劍指offer 31~40

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) {
        if(n <= 0){
            return 0;
        }
        int count = 0;
        for(int i = 1; i <= n;i++){
            int t = i;
            while(t >= 10){
                if(t % 10 == 1){
                    count++;
                }
                t /= 10;
            }
            if(t == 1){
                count++;
            }
        }
        return count;
    }
}

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

import java.util.*;
public class Solution {
    public String PrintMinNumber(int [] numbers) {
        if(numbers.length == 0){
            return "";
        }
        if(numbers.length == 1){
            return ""+numbers[0];
        }
        String str = "";
        List<Integer> list = new ArrayList<>();
        for(int i = 0;i < numbers.length ;i++){
            list.add(numbers[i]);
        }
        Collections.sort(list,new Comparator<Integer>(){
            @Override
            public int compare(Integer s1,Integer s2){
                String c1 = s1 +""+ s2;
                String c2 = s2 +""+ s1;
                return c1.compareTo(c2);
            }
        });
        for(int i:list){
            str += i;
        }
        return str;
            
    }
}

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

public class Solution {
    public int GetUglyNumber_Solution(int index) {
        if(index <= 0){
            return 0;
        }
        int[] num = new int[index];
        num[0] = 1;
        int count = 0;
        int i = 0;
        int j = 0;
        int k = 0;
        while(count < index -1){
            int tmp = Math.min(num[i]*2,Math.min(num[j]*3,num[k]*5));
            if(tmp == num[i]*2) i++;
            if(tmp == num[j]*3) j++;
            if(tmp == num[k]*5) k++;
            num[++count] = tmp;
        }
        return num[index-1];
    }
}

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

import java.util.*;
class Solution {
    public int FirstNotRepeatingChar(String str) {
        if(str.length() == 0){
            return -1;
        }
        String tmp = str;
        List<Character> list = new ArrayList<>();
        for(int i = 0; i < str.length();i++){
            char ch = str.charAt(i);
            if(!list.contains(ch)){
                list.add(Character.valueOf(ch));
            }else{
                list.remove(Character.valueOf(ch));
                tmp = tmp.replaceAll(String.valueOf(ch),"");
            }
        }
        if(tmp.length() <= 0){
            return -1;
        }
        return str.indexOf(tmp.charAt(0));
    }
}

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

import java.util.*;
public class Solution {
    private int count = 0;
    public int InversePairs(int [] array) {
        mergeSort(0,array.length-1,array);
        return count;
    }
    public void mergeSort(int l,int r,int[] array){
        if(l >= r){
            return;
        }
        int mid = (l + r)/2;
        mergeSort(l,mid,array);
        mergeSort(mid+1,r,array);
        merge(l,mid,r,array);
    }
    public void merge(int l,int mid,int r,int[] array){
        int[] arr = new int[r-l+1];
        int ind = 0;
        int i = l;
        int j = mid+1;
        while(i <= mid && j <= r){
            if(array[i] <= array[j]){
                arr[ind++] = array[i++];
            }else{
                arr[ind++] = array[j++];
                count = (count + (mid - i+1))%1000000007;
            }
        }
        while (i <= mid){
            arr[ind++] = array[i++];
        }
        while(j <= r){
            arr[ind++] = array[j++];
        }
        for(int k = 0;k < ind;k++){
            array[l+k] = arr[k];
        }
    }
}

36.輸入兩個鏈表,找出它們的第一個公共結點。(注意因爲傳入數據是鏈表,所以錯誤測試數據的提示是用其他方式顯示的,保證傳入數據是正確的)

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode p1 = pHead1;
        ListNode p2 = pHead2;
        int k = 0;
        while(p1 != null){
            p1 = p1.next;
            k++;
         }
        while(p2 != null){
            p2 = p2.next;
            k--;
        }
        if(k>0){
            for(int i = 0 ;i < k;i++){
                pHead1 = pHead1.next;
            }
        }else{
            k = -k;
            for(int i = 0 ;i < k;i++){
                pHead2 = pHead2.next;
            }
        }
        while(pHead1 != null){
            if(pHead1.val == pHead2.val){
                return pHead1;
            }
            pHead1 = pHead1.next;
            pHead2 = pHead2.next;
        }
        return null;
    }
}

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

public class Solution {
    public int GetNumberOfK(int [] array , int k) {
        if(array.length == 0){
            return 0;
        }
        int count = 0;
        for(int i = 0;i < array.length;i++){
            if(array[i] == k){
                count ++;
            }
        }
        return count;
    }
}

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

public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root == null) return 0;
        return Math.max(1+TreeDepth(root.left),1+TreeDepth(root.right));
    }
}

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

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        return isp(root) != -1;
    }
    public int isp(TreeNode root){
        if(root == null) return 0;
        int left = isp(root.left);
        if(left == -1) return -1;
        int right = isp(root.right);
        if(right == -1) return -1;
        return Math.abs(left-right)>1?-1:1+Math.max(left,right);
    }
}

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

//num1,num2分別爲長度爲1的數組。傳出參數
//將num1[0],num2[0]設置爲返回結果
import java.util.*;
public class Solution {
    public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
        List<Integer> list = new ArrayList<>();
        for(int i = 0;i< array.length ;i++){
            int tmp = array[i];
            if(list.contains(tmp)){
                list.remove(Integer.valueOf(tmp));
            }else{
                list.add(tmp);
            }
        }
        num1[0] = list.get(0);
        num2[0] = list.get(1);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章