Decode Algorithms & LeetCode
  • Decode Algorithms & LeetCode
  • G家练习
  • Array
    • Two Sum
    • Two Pointers
    • PrefixSum
    • Matrix
    • Interval & Sweepline
    • Sort
    • Iterator
    • Segment Tree
  • Binary Search
    • 教学
    • Binary Search on Matrix
    • Binary Search To Find An Answer
  • String
    • Parenthesis
    • Sliding Window
    • Trie
  • LinkedList
  • Tree
    • Level Order Traversal
    • BST or Inorder
    • Construst Tree
  • Stack
  • Heap
  • Greedy
  • BFS
  • DFS
    • DFS on String
    • Backtracking
    • DFS+Memo
  • Graph
    • Union Find
    • Topological Sort
    • Dijkstra
    • Bipartite Graph
  • Dynamic Programing
    • DP on String
    • Knapsack Problem
  • Design
    • Concurrency
  • Math
  • Bit Manipulation
Powered by GitBook
On this page

Was this helpful?

Math

PreviousConcurrencyNextBit Manipulation

Last updated 4 years ago

Was this helpful?

Stop the loop before res exceeded the limit.

def reverse(self, x: int) -> int:
    limit = pow(2, 31) - 1
    neg = x < 0
    res = 0
    x = abs(x)
    while x > 0:
        if res > limit / 10 and int(x % 10) != 0:
            return 0
        res = res * 10 + int(x % 10)
        x = x // 10
    if neg:
        return res * -1
    return res

Store int value and its corresponding symbol values in two arrays. While(num >= arr[i]) append result.

int[] arr = { 1,4,5,9,10,40,50,90,100,400, 500, 900, 1000};
String[] symbol = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
public String intToRoman(int num) {
    StringBuilder sb = new StringBuilder();
    for (int i = arr.length - 1; i >= 0; i--) {
        while (num >= arr[i]) {
            sb.append(symbol[i]);
            num -= arr[i];
        }
    }
    return sb.toString();
}
    public int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>();
        map.put('M', 1000);
        map.put('D', 500);
        map.put('C', 100);
        map.put('L', 50);
        map.put('X', 10);
        map.put('V', 5);
        map.put('I', 1);
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i + 1 < s.length()) {
                if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
                    res += map.get(s.charAt(i + 1)) - map.get(s.charAt(i));
                    i++;
                } else {
                    res += map.get(s.charAt(i));
                }
            } else {
                res += map.get(s.charAt(i));
            }
        }
        return res;
    }
public String addBinary(String a, String b) {
    int i = a.length() - 1;
    int j = b.length() - 1;
    StringBuilder sb = new StringBuilder();
    int carry = 0;
    while (i >= 0 || j >= 0) {
        int one = i >= 0 ? a.charAt(i) - '0': 0;
        int two = j >= 0 ? b.charAt(j) - '0': 0;
        int sum = one + two + carry;
        sb.append(sum % 2);
        carry = sum / 2;
        i--;
        j--;
    }
    if (carry == 1) sb.append(1);
    return sb.reverse().toString();
}

2^10 = 2*2*2*2*2*2*2*2*2*2

(2*2)*(2*2)*(2*2)*(2*2)*(2*2)

4^5 = (4*4)*(4*4)*4

16*2 * 4

public double myPow(double x, int n) {
    if (n == 0) return 1.0;
    if (n == 1) return x;
    double res = 1.0;
    long longn = n;
    longn = longn < 0 ? -longn : longn;
    while (longn > 0) {
        if (longn % 2 != 0) {
            res *= x;
        } 
        x *= x;
        longn /= 2;
    }
    return n < 0 ? 1.0 / res : res;
}
def myPow(self, x: float, n: int) -> float:
    negative = n < 0
    i = abs(n)
    res = 1.0
    while i > 0:
        if i % 2 != 0:
            res *= x
        x *= x
        i = i // 2
    if negative:
        return 1.0 / res
    return res
    String[] LESS20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    String[] TENs = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    String[] THOUSANDs = {"", "Thousand", "Million", "Billion"};
    public String numberToWords(int num) {
        if (num == 0) return "Zero";
        int i = 0;
        String res = "";
        while (num != 0) {
            if (num % 1000 != 0) {
                res = helper(num % 1000) + THOUSANDs[i] + " " + res;
            }
            i++;
            num /= 1000;
        }
        return res.trim();
    }
    
    private String helper(int n) {
        if (n == 0) return "";
        if (n < 20) return LESS20[n] + " ";
        else if (n < 100) return TENs[n / 10] + " " + helper(n % 10);
        return LESS20[n / 100] + " Hundred " + helper(n % 100);
    }

1131. Maximum of Absolute Value Expression

Solve the equation by:

Then detect the maximum result of max - min

public int maxAbsValExpr(int[] arr1, int[] arr2) {
    int max1 = Integer.MIN_VALUE;
    int max2 = Integer.MIN_VALUE;
    int max3 = Integer.MIN_VALUE;
    int max4 = Integer.MIN_VALUE;
    int min1 = Integer.MAX_VALUE;
    int min2 = Integer.MAX_VALUE;
    int min3 = Integer.MAX_VALUE;
    int min4 = Integer.MAX_VALUE;
    for (int i = 0; i < arr1.length; i++) {
        max1 = Math.max(max1, arr1[i] + arr2[i] + i);
        max2 = Math.max(max2, arr1[i] + arr2[i] - i);
        max3 = Math.max(max3, arr1[i] - arr2[i] - i);
        max4 = Math.max(max4, arr1[i] - arr2[i] + i);
        min1 = Math.min(min1, arr1[i] + arr2[i] + i);
        min2 = Math.min(min2, arr1[i] + arr2[i] - i);
        min3 = Math.min(min3, arr1[i] - arr2[i] - i);
        min4 = Math.min(min4, arr1[i] - arr2[i] + i);
    }
    return Math.max(Math.max(max1 - min1, max2 - min2), Math.max(max3 - min3, max4 - min4));
}
public int rand10() {
    int res = 40;
    while (res >= 40) {
        res = (rand7() - 1) * 7 + rand7() - 1;
    }
    return res % 10 + 1;
}

7. Reverse Integer
12. Integer to Roman
13. Roman to Integer
67. Add Binary
50. Pow(x, n)
273. Integer to English Words
470. Implement Rand10() Using Rand7()
How to do question like implement RandomX by RandomY.