头像

QAQAQAQ


访客:5626

离线:2天前



QAQAQAQ
3个月前
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func lowestCommonAncestor(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode {
    if (root == nil || root == p || root == q) {
        return root
    }
    left := lowestCommonAncestor(root.Left, p, q)
    right := lowestCommonAncestor(root.Right, p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}



QAQAQAQ
3个月前
func strToInt(str string) int {
    if len(str) == 0 {
        return 0
    }
    const (
        INT32_MAX = int(^uint32(0) >> 1)
        INT32_MIN = ^INT32_MAX
    )
    // fmt.Println(INT_MAX, INT_MIN)
    st := []byte(str)
    var is_minus bool = false
    var res int = 0
    // fmt.Printf("%T %d", res, unsafe.Sizeof(res))
    k := 0
    for st[k] == ' '{
        k++
    }
    // fmt.Println(k)
    if st[k] == '+'{ 
        k++
    } else if st[k] == '-' {
        k++
        is_minus = true
    } else if !(st[k] >= '0' && st[k] <='9'){
        return 0
    }
    for k < len(st) && st[k] >= '0' && st[k] <= '9' {
        if is_minus != true && res  >= INT32_MAX / 10 {
            return INT32_MAX
        }
        if is_minus && res*-1 <= INT32_MIN / 10 {
            return INT32_MIN
        }
        // fmt.Printf("%c %d\n", st[k], res)
        res = res * 10 + int(st[k] - '0')
        k++

    }
    if is_minus {
        res *= -1
    }
    return int(res);
}



QAQAQAQ
3个月前

go 代码

func strToInt(str string) int {
    if len(str) == 0 {
        return 0
    }
    const (
        INT32_MAX = int(^uint32(0) >> 1)
        INT32_MIN = ^INT32_MAX
    )
    // fmt.Println(INT_MAX, INT_MIN)
    st := []byte(str)
    var is_minus bool = false
    var res int = 0
    // fmt.Printf("%T %d", res, unsafe.Sizeof(res))
    k := 0
    for st[k] == ' '{
        k++
    }
    // fmt.Println(k)
    if st[k] == '+'{ 
        k++
    } else if st[k] == '-' {
        k++
        is_minus = true
    } else if !(st[k] >= '0' && st[k] <='9'){
        return 0
    }
    for k < len(st) && st[k] >= '0' && st[k] <= '9' {
        if is_minus != true && res  >= INT32_MAX / 10 {
            return INT32_MAX
        }
        if is_minus && res*-1 <= INT32_MIN / 10 {
            return INT32_MIN
        }
        // fmt.Printf("%c %d\n", st[k], res)
        res = res * 10 + int(st[k] - '0')
        k++

    }
    if is_minus {
        res *= -1
    }
    return int(res);
}



QAQAQAQ
3个月前
class Solution {
public:
    int strToInt(string str) {
        long long res = 0;
        bool is_minus = false;
        int k = 0;
        while(str[k] == ' ')k++;
        if (str[k] == '-') k++, is_minus = true;
        else if (str[k] == '+') k++;
        while(k < str.size() && isdigit(str[k])) {
            if (!is_minus&&res >= INT_MAX / 10) return INT_MAX;
            if (is_minus && res * -1 <= INT_MIN / 10) return INT_MIN;
            res = res * 10 + str[k] - '0';
            k++;
        }
        if (is_minus) res *= -1;
        return res;
    }
};


活动打卡代码 AcWing 86. 构建乘积数组

QAQAQAQ
3个月前
func multiply(A []int) []int {
    if len(A) == 0 {
        return []int{}
    }
    res := make([]int, len(A))
    res[0] = 1;
    for i, p := 1, A[0]; i < len(A); i ++ {
        res[i] = p
        p *= A[i]
    }

    for i, p := len(A) - 2, A[len(A)-1]; i >= 0; i-- {
        res[i] *= p
        p *= A[i]
    }
    return res
}



QAQAQAQ
3个月前
func multiply(A []int) []int {
    if len(A) == 0 {
        return []int{}
    }
    res := make([]int, len(A))
    res[0] = 1;
    for i, p := 1, A[0]; i < len(A); i ++ {
        res[i] = p
        p *= A[i]
    }

    for i, p := len(A) - 2, A[len(A)-1]; i >= 0; i-- {
        res[i] *= p
        p *= A[i]
    }
    return res
}



QAQAQAQ
3个月前
func add(num1 int, num2 int) int {
    for num2 != 0{
        sum := num1 ^ num2
        carry := (num1 & num2) << 1
        num1 = sum
        num2 = carry
    }
    return num1
}


活动打卡代码 AcWing 84. 求1+2+…+n

QAQAQAQ
3个月前
func getSum(n int) int {
    var f func(sum *int, n int) bool
    f = func(sum *int, n int) bool{
        *sum += n
        return n != 0 && f(sum, n-1)
    }
    var ans int = 0
    f(&ans, n)
    return ans
}



QAQAQAQ
3个月前
func maxDiff(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    res := 0
    for i, minv := 1, nums[0]; i < len(nums); i++ {
        if res < nums[i] - minv {
            res = nums[i] - minv
        }
        if minv > nums[i] {
            minv = nums[i]
        }
    }
    return res
}



QAQAQAQ
3个月前
func lastRemaining(n int, m int) int {
    if n == 0 {
        return 0
    }
    return (lastRemaining(n-1, m) + m) % n
}