1. Divide Two Integers

public class Solution {

    public int divide(int dividend, int divisor){

        if (dividend == 0) return 0;
        if (divisor == 0)  return Integer.MAX_VALUE;

        int sign = 1;
        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0))
            sign = -1;

        long d1 = Math.abs((long)dividend);
        long d2 = Math.abs((long)divisor);

        long ans = divideLong(d1, d2);

        if (ans > Integer.MAX_VALUE)
            return sign == 1? Integer.MAX_VALUE: Integer.MIN_VALUE;
        else
            return sign * (int)ans;
    }

    public long divideLong(long d1, long d2){

        if (d1 < d2) return 0;

        long sum = d2;
        long multi = 1;
        while (sum + sum <= d1) {
            sum += sum;
            multi += multi;
        }

        return multi + divideLong(d1 - sum, d2);
    }
}

2. Multiply Two Integers

public class Solution {

    public int multiply(int a, int b){
        int bigger = a > b? a: b;
        int smaller= a > b? b: a;
        return mult(bigger, smaller);
    }

    public int mult(int bigger, int smaller) {

        if(smaller == 0) return 0;
        if(smaller == 1) return bigger;

        int half = mult(bigger, smaller/2);

        if(smaller % 2 == 0){
            return 2 * half;
        }
        else{
            return 2 * half + bigger;
        }
    }
}

3. Pow(x, n)

public class Solution {
    public double myPow(double x, int n) {

        if(n == 0) return 1;

        if(n < 0){
            x = 1/x;
            if(n == Integer.MIN_VALUE){
                n = n + 1;
                n = -n;
                return x * x * myPow(x * x, n/2);
            }
            n = -n;
        }

        return n%2 == 0? myPow(x * x, n/2): x * myPow(x * x, n/2);
    }
}

4. Sqrt(x)

public class Solution {

    public int mySqrt(int x) {

        if(x == 0) return 0;

        int left = 1, right = x;

        while(left <= right) {

            int mid = (left + right) / 2;

            if(mid == x / mid)
                return mid;
            if(mid < x / mid)
                left = mid + 1;
            else
                right= mid - 1;
        }
        return right;//return small
    }
}

5. Plus One

Given a non-negative integer represented as anon-emptyarray of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

public class Solution {
    public int[] plusOne(int[] digits) {

        int n = digits.length;

        for(int i = n - 1; i >= 0; i --){

            if(digits[i] < 9){
                digits[i]++;
                return digits;
            }
            digits[i] = 0;
        }

        int[] newDigits = new int[n + 1];
        newDigits[0] = 1;
        return newDigits;
    }
}

6. Add Binary

Given two binary strings, return their sum (also a binary string).

public class Solution {
    public String addBinary(String a, String b) {

        StringBuilder res = new StringBuilder();

        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        int sum;

        while(i >= 0 || j >= 0 || carry == 1){

            sum = carry;
            if(i >= 0) sum += a.charAt(i--) - '0';
            if(j >= 0) sum += b.charAt(j--) - '0';

            res.append(sum%2);
            carry = sum / 2;
        }

        return res.reverse().toString();

    }
}

7 Multiply Strings

Given two non-negative integers represented by strings

public class Solution {
    public String multiply(String num1, String num2) {

        int m = num1.length();
        int n = num2.length();
        int[] nums = new int[n + m];

        for(int i = m - 1; i >= 0; i --) {
            for(int j = n - 1; j >= 0; j --) {

                int mult = (num1.charAt(i) - '0')*(num2.charAt(j) - '0');
                int p1 = i + j, p2 = i + j + 1; 
                int sum = nums[p2] + mult;
                nums[p1] += sum / 10;
                nums[p2] = sum % 10;
            }
        }

        StringBuilder res = new StringBuilder();
        for(int i = 0; i < nums.length; i++){
            if(!(res.length() == 0 && nums[i] == 0)) 
                res.append(nums[i]);
        }
        return res.length() == 0? "0": res.toString();
    }
}

.

8 Fraction to Recurring Decimal

public class Solution {

    public String fractionToDecimal(int numerator, int denominator) {

        StringBuilder res = new StringBuilder();

        if((numerator > 0 && denominator < 0) || (numerator < 0 && denominator > 0)){
            res.append("-");
        }

        long n = Math.abs((long)numerator);
        long d = Math.abs((long)denominator);

        res.append(n / d);
        long r = n % d;
        if(r == 0)
            return res.toString();

        res.append(".");
        Map<Long, Integer> map = new HashMap<>();
        map.put(r, res.length());
        while(r != 0){
            r *= 10;
            res.append(r / d);
            r %= d;

            if(map.containsKey(r)){
                int index = map.get(r);
                res.insert(index, "(");
                res.append(")");
                break;
            }
            else{
                map.put(r, res.length());
            }
        }

        return res.toString();

    }
}

results matching ""

    No results matching ""