面试题49,50,51,52

面试题49,50,51,52

/**
 * 面试题49:丑数
 *      我们把只含有因子2,3,5的数成为丑数。求按从小到大的顺序的第1500个丑数。
 *      例如,6,8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当作第一个丑数
 */
public class GetUglyNumber {
    /**
     *判断是否是丑数
     */
    public static boolean isUgly(int number){
        while(number % 2 == 0){
            number /= 2;
        }

        while(number % 5 == 0){
            number /= 5;
        }

        while(number % 3 == 0){
            number /= 3;
        }

        if(number == 1){
            return true;
        }else{
            return false;
        }

    }

    public static int getUglyNumberSolution1(int index){
        int uglyNumber = 0;
        if(index <= 0){
            return 0;
        }
        int number = 0;
        while(uglyNumber < index){
            number++;
            if(isUgly(number)){
                uglyNumber++;
            }
        }
        return number;
    }



    public static int getUglyNumberSolution2(int index){
        if(index <= 0){
            return 0;
        }
        int[] uglyNumbers = new int[index];
        uglyNumbers[0] = 1;
        int uglyNumberIndex = 1;
        int indexOf2 = 0;
        int indexOf3 = 0;
        int indexOf5 = 0;
        while(uglyNumberIndex < index){
            int min = min(uglyNumbers[indexOf2] * 2 , uglyNumbers[indexOf3] * 3 , uglyNumbers[indexOf5] * 5);
            uglyNumbers[uglyNumberIndex] = min;

            while(uglyNumbers[indexOf2] * 2 <= uglyNumbers[uglyNumberIndex]){
                indexOf2 ++ ;
            }

            while(uglyNumbers[indexOf3] * 3 <= uglyNumbers[uglyNumberIndex]){
                indexOf3 ++ ;
            }

            while(uglyNumbers[indexOf5] * 5 <= uglyNumbers[uglyNumberIndex]){
                indexOf5 ++ ;
            }

            uglyNumberIndex ++ ;
        }
        return uglyNumbers[uglyNumberIndex - 1];
    }

    public static int min(int num1,int num2,int num3){
        int num = num1 < num2 ? num1 : num2;
        return num < num3 ? num : num3 ;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        System.out.println(getUglyNumberSolution1(1500));
        System.out.println(System.currentTimeMillis() - start);
        long start2 = System.currentTimeMillis();
        System.out.println(getUglyNumberSolution2(1500));
        System.out.println(System.currentTimeMillis() - start2);
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 面试题50:第一个只出现一次的字符
 *      找出字符串中第一个只出现一次的字符。如输入"abaccdeff",输出‘b’
 *
 */
public class FirstNotRepeatingChar {
    public static char firstNotRepeatingChar(String s,int length){
        if(s == null || length <= 0){
            return '\0';
        }
        Map<Character,Integer> map = new HashMap<>();
        int index = 0;
        while(index < length){
            if(map.containsKey(s.charAt(index))){
                Integer integer = map.get(s.charAt(index));
                map.put(s.charAt(index),integer+1);
            }else{
                map.put(s.charAt(index),1);
            }
            index ++ ;
        }
        Set<Character> characters = map.keySet();
        for(char c:characters){
            if(map.get(c) == 1){
                return c;
            }
        }
        return '\0';
    }

    public static void main(String[] args) {
        String s = "abaccdeff";
        System.out.println(firstNotRepeatingChar(s, s.length()));

    }
}
/**
 * 面试题51:数组中的逆序对
 *      在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。
 *      输入一个数组,求出这个数组中的逆序对的总数。例如,在数组【7,5,6,4】中,一共存在5个逆序对。分别是(7,6)
 *      (7,4),(7,5),(6,4)和(5,4)
 */
public class InversePairs {
    public static int inversePairs(int[] data,int length){
        if(length <= 0 || data == null){
            return -1;
        }
        int[] copy = new int[length];
        for(int i = 0; i < length; i++){
            copy[i] = data[i];
        }
        return inversePairsCore(data, copy,0,length - 1);
    }

    public static int inversePairsCore(int[] data, int[] copy, int start, int end){
        if(start == end){

            return 0;
        }

        int length = (end - start)/2;
        int left = inversePairsCore(copy, data, start,start + length);
        int right = inversePairsCore(copy, data,start + length + 1, end);

        int i = start + length;
        int j = end;
        int count = 0;
        int copyIndex = end;
        while(i >= start && j >= start + length + 1){
            if(data[i] > data[j]){
                count += j - length - start;
                copy[copyIndex--] = data[i--];
            }else{
                copy[copyIndex--] = data[j--];
            }
        }
        for(; i >= start; i--){
            copy[copyIndex--] = data[i];
        }
        for(; j >= start + length + 1; j--){
            copy[copyIndex--] = data[j];
        }
        return left + right + count;
    }

    public static void main(String[] args) {
        int[] data = {7,5,6,4};
        System.out.println(inversePairs(data, data.length));
    }
}
/**
 * 面试题52:两个链表的第一个公共节点:
 *      输入两个链表,找出它们的第一个公共节点。
 */
public class FindFirstCommonNode {
    public static ListNode findFirstCommonNode(ListNode node1,ListNode node2){
        if(node1 == null || node2 == null){
            return null;
        }

        int length1 = 0;
        int length2 = 0;
        ListNode tmpNode1 = node1;
        ListNode tmpNode2 = node2;
        while(tmpNode1 != null && tmpNode2 != null){
            if(tmpNode1 != null){
                tmpNode1 = tmpNode1.next;
                length1++;
            }
            if(tmpNode2 != null){
                tmpNode2 = tmpNode2.next;
                length2++;
            }
        }

        tmpNode1 = node1;
        tmpNode2 = node2;
        int differ ;
        int index = 0;
        if(length1 >= length2){
            differ = length1 - length2;
            while(index < differ){
                index ++ ;
                tmpNode1 = tmpNode1.next;
            }
            while(tmpNode1 != null && tmpNode2 != null){
                if(tmpNode1 != tmpNode2){
                    tmpNode1 = tmpNode1.next;
                    tmpNode2 = tmpNode2.next;
                }else{
                    return tmpNode1;
                }
            }
            return null;
        }else{
            differ = length2 - length1;
            while(index < differ){
                index ++ ;
                tmpNode2 = tmpNode2.next;
            }
            while(tmpNode1 != null && tmpNode2 != null){
                if(tmpNode1 != tmpNode2){
                    tmpNode1 = tmpNode1.next;
                    tmpNode2 = tmpNode2.next;
                }else{
                    return tmpNode1;
                }
            }
            return null;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值