wish面经

two sum add all results

 

国人 239. Sliding Window Maximum

输入是两个string,然后要求返回一个string是这两个string的sum。
比较难的是输入的string可能有任意字符,所以需要自己先check validation,
然后长度也是没有限制,所以不能convert成int或者long之后直接算,得自己遍历字符串来一位一位的算。
输入的string也是可以是负数的。最后也没完全写完,太多corner case了,负数计算还有点问题,但是正数的写完了。

class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.add("0", "-9"));
        System.out.println(solution.add("10", "-9"));
        System.out.println(solution.add("29", "99"));
        System.out.println(solution.add("-10", "-9"));
        System.out.println(solution.add("-1000", "2"));
        System.out.println(solution.add("-1000", "1000"));
    }
    
    public String add(String num1, String num2) {
        // 1st case: both are positive
        if (!isNegative(num1) && !isNegative(num2)) return addStrings(num1, num2);
        // 2nd case: both are negative
        else if (isNegative(num1) && isNegative(num2)) return "-" + addStrings(num1.substring(1), num2.substring(1));
        // 3rd case: one of number is negative, let's make num1 > 0, non-negative
        else if (isNegative(num1)) { 
            String temp = num1; num1 = num2; num2 = temp; 
        }
        num2 = num2.substring(1); // call out, remove sign before doing calculation
        int compareRes = compareNums(num1, num2);
        if (compareRes == 0) return "0";
        else if (compareRes == 1) return subtractString(num1, num2);
        else return "-" + subtractString(num2, num1);
    }


    private String addStrings(String num1, String num2) {
        // assert num1 >= 0, num2 >= 0
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        int i = num1.length() - 1, j = num2.length() - 1;
        while (i >= 0 || j >= 0 || carry == 1){
            int x = i < 0 ? 0 : Character.digit(num1.charAt(i), 10);
            int y = j < 0 ? 0 : Character.digit(num2.charAt(j), 10);
            int digitSum = x + y + carry;
            // call out, record carry first, then update digitSum
            carry = digitSum / 10;
            digitSum = digitSum % 10;
            sb.append(digitSum);
            i--;
            j--;
        }
        return sb.reverse().toString();
    }

    private String subtractString(String num1, String num2) {
        // assert num1 >= num2 >= 0
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        int i = num1.length() - 1, j = num2.length() - 1;
        while (i >= 0 || j >= 0 || carry == 1){
            int x = i < 0 ? 0 : Character.digit(num1.charAt(i), 10);
            int y = j < 0 ? 0 : Character.digit(num2.charAt(j), 10);
            int digitRes = x - y - carry;
            if (digitRes < 0) {
                carry = 1;
                digitRes += 10;
            } else {
                carry = 0;
            }
            sb.append(digitRes);
            i--;
            j--;
        }
        // call out, there might be leading zeros
        return sb.reverse().toString().replaceFirst("^0+(?!$)", ""); 
    }

    private int compareNums(String num1, String num2) {
        // compare len
        int len1 = num1.length(), len2 = num2.length();
        if (len1 < len2) return -1;
        else if (len1 > len2) return 1;
        else { // (len1 == len2) 
            // compare first char
            if (len1 == 0) return 0;
            else if (num1.charAt(0) < num2.charAt(0)) return -1;
            else if (num1.charAt(0) > num2.charAt(0)) return 1;
            else return compareNums(num1.substring(1), num2.substring(1));
        }
    }

    private boolean isNegative(String num) {
        // assert num not empty and is valid
        return num.charAt(0) == '-';
    }
}

 

蠡口 71 Simplify Path

Input: path = "/home//foo/"
Output: "/home/foo"


139
Word Break 45.3% Medium 能否切开
153
Find Minimum in Rotated Sorted Array 48.4% Medium
154
Find Minimum in Rotated Sorted Array II 43.4% 去重复唯一的区别,在153也能用:Hard nums[m] == nums[hi]) hi--
289
Game of Life 66.5% Medium 用dir[0][1]数组 
380
Insert Delete GetRandom O(1) 51.9% Medium 用list/map/random
403
Frog Jump 43.1% Hard 步数 用set
436
Find Right Interval 50.3% Medium 用treemap的ceilingEntry,寻找下一个
934
Shortest Bridge 53.8% Medium,一开始的大岛屿要先标记为2

posted @ 2022-09-29 01:46  苗妙苗  阅读(93)  评论(0编辑  收藏  举报