[leetCode刷题笔记]2017.04.03

本文介绍了一种高效实现整数除法的方法,采用位移扩大两倍策略达到 O(logn) 的时间复杂度,并提供了一个利用阶乘查找和列表索引来求解第 k 个排列的算法。此外,还详细说明了如何通过模拟笔算乘法来实现两个字符串的大数相乘。

29. Divide Two Integers

一次扩大两倍, O(logn)

public class Solution {
    public int divide(int dividend, int divisor) {
        if (divisor == 0) {
            return Integer.MAX_VALUE;
        }
        long ldividend = dividend;
        long ldivisor = divisor;
        boolean end = true;
        boolean sor = true;
        if (dividend <0) {
            end= false;
            ldividend = - ldividend;
        }
        if (divisor < 0) {
            sor = false;
            ldivisor = - ldivisor;
        }
        long res = cal(ldividend, ldivisor);
        if (res > Integer.MAX_VALUE)
            return end == sor ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        return end == sor ? (int)res : -(int)res;
    }
    private long cal(long ldividend, long ldivisor) {
        if (ldividend < ldivisor) {
            return 0;
        }
        long mul = 1;
        long sum = ldivisor;
        while (sum + sum <= ldividend) {
            sum += sum;
            mul += mul;
        }
        return mul + cal(ldividend - sum, ldivisor);
    }
}


60. Permutation Sequence

public class Solution {
    public String getPermutation(int n, int k) {
        int pos = 0;
        List<Integer> numbers = new ArrayList<>();
        int[] factorial = new int[n+1];
        StringBuilder sb = new StringBuilder();
        // create an array of factorial lookup
        int sum = 1;
        factorial[0] = 1;
        for(int i=1; i<=n; i++){
            sum *= i;
            factorial[i] = sum;
        }
        // factorial[] = {1, 1, 2, 6, 24, ... n!}
    
        // create a list of numbers to get indices
        for(int i=1; i<=n; i++){
            numbers.add(i);
        }
        // numbers = {1, 2, 3, 4}
    
        k--;
    
        for(int i = 1; i <= n; i++){
            int index = k/factorial[n-i];
            sb.append(String.valueOf(numbers.get(index)));
            numbers.remove(index);
            k-=index*factorial[n-i];
        }
    
        return String.valueOf(sb);
    }
}

43. Multiply Strings

public class Solution {
    public String multiply(String num1, String num2) {
        int m = num1.length();
        int n = num2.length();
        int[] pos = new int[m + n];
        
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                int mul = (num1.charAt(i) - '0') * (num2.charAt(j) - '0'); 
                int p1 = i + j, p2 = i + j + 1;
                int sum = mul + pos[p2];
                pos[p1] += sum / 10;
                pos[p2] = sum % 10;
            }
        }
        
        StringBuilder sb = new StringBuilder();
        for(int p : pos) if(!(sb.length() == 0 && p == 0)) sb.append(p);
        return sb.length() == 0 ? "0" : sb.toString();
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值