剑指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]);
    }
}

猜你喜欢

转载自blog.csdn.net/xdzhouxin/article/details/81124059