头像

丶Axe


访客:8902

离线:15天前



丶Axe
6个月前

Talk is cheap.

class Solution(object):
    def strToInt(self, s):
        s = s.strip()
        if not s:
            return 0
        sign = -1 if s[0] == '-' else 1
        s = s[1:] if s[0] in ('+', '-') else s
        ret = i = 0
        while i < len(s) and s[i].isdigit():
            ret = ret * 10 + ord(s[i]) - ord('0')
            i += 1
        return min(2**31-1, max(sign*ret, -2**31))



丶Axe
6个月前

Talk is cheap.

class Solution(object):
    def isNumber(self, s):
        s = s.strip()
        met_dot = met_e = met_digit = False
        for i, char in enumerate(s):
            if char in '+-':
                if i > 0 and s[i-1] not in 'eE':
                    return False
            elif char == '.':
                if met_dot or met_e: return False
                met_dot = True
            elif char == 'e' or char == 'E':
                if met_e or not met_digit:
                    return False
                met_e, met_digit = True, False
            elif char.isdigit():
                met_digit = True
            else:
                return False
        return met_digit
class Solution:
    def isNumber(self, s: str) -> bool:
        s = s.strip()
        validList = set(['+', '-', '.', 'e', 'E'])
        isFirst = True

        for c in s:
            if c.isdigit():
                isFirst = False
                continue
            if c not in validList:
                return False
            if c == 'e' or c == 'E':
                if isFirst:
                    return False
                isFirst = True
                validList = set(['+', '-'])
            if c == '.':
                validList = set(['e', 'E'])
            if c == '+' or c == '-':
                if not isFirst:
                    return False
                validList.remove('+')
                validList.remove('-')

        return True and not isFirst

最后 python大法好。

class Solution(object):
    def isNumber(self, s):
        try:
            float(s)
            return True
        except:
            return False



丶Axe
6个月前

救救孩子吧,从俭入奢易,从奢入俭难!

func hasPath(matrix [][]byte, str string) bool {
    if len(matrix) == 0 || len(matrix[0]) == 0 || len(str) == 0 {
        return false
    }
    m, n := len(matrix), len(matrix[0])
    strsp := []byte(str)
    state := make([][]int, m) 
    for i := range state {state[i] = make([]int, n)}
    for i := 0; i < m; i++ {
        for j := 0; j < n; j++ {
            if dfs(matrix, state, i, j, strsp, 0) {
                return true
            }
        }
    }
    return false
}

func dfs(matrix [][]byte, state [][]int, i int, j int, strsp []byte, pos int) bool {
    m, n := len(state), len(state[0])
    if 0 <= i && i < m && 0 <= j && j < n && state[i][j] == 0 {
        ret := false
        state[i][j] = 1
        if matrix[i][j] == strsp[pos] {
            if pos == len(strsp) - 1 {
                return true
            }
            ret = dfs(matrix, state, i, j+1, strsp, pos+1) ||
            dfs(matrix, state, i, j-1, strsp, pos+1) ||
            dfs(matrix, state, i+1, j, strsp, pos+1) ||
            dfs(matrix, state, i-1, j, strsp, pos+1)
        }
        if ret == false { state[i][j] = 0 }
        return ret
    }
    return false
}



丶Axe
6个月前

我不行了,如果有更好的麻烦救救孩子。
不能导入 math 包?顺带复习简单快速幂
Go 的类型真的很严格。

func digitAtIndex(n int) int {
    n -= 1
    for digit := 1; digit <= 10; digit ++ {
        first := pow(10, digit-1)
        if n < 9 * first * digit {
            conv := strings.Split(strconv.Itoa(first + n / digit), "")[n % digit]
            res, _ := strconv.Atoi(conv)
            return res
        }
        n -= 9 * first * digit
    }
    return 0
}

func pow(base, exp int) int {
    r := 1
    for exp != 0 {
        if exp & 1 == 1 {
            r = r * base
        }
        base = base * base
        exp = exp >> 1
    }
    return r
}



丶Axe
6个月前

Talk is cheap.

func numberOf1Between1AndN_Solution(n int) int {
    var count, tmp int
    for i := 1; i <= n; i *= 10 {
        a, b := n / i, n % i
        if a % 10 == 1 { tmp = 1 }
        count += (a + 8) / 10 * i + tmp * (b + 1)
    }
    return count
}



丶Axe
6个月前

Talk is cheap.

class Solution:
    def permutation(self, nums):
        perms = [[]]
        for n in nums:
            perms = [p[:i] + [n] + p[i:]
                     for p in perms
                     for i in range((p + [n]).index(n) + 1)]
        return perms
class Solution:
    def permutation(self, nums):
        import itertools
        return list(set(itertools.permutations(nums)))



丶Axe
6个月前

Talk is cheap.

func numberOfDice(n int) []int {
    dp := make([]int, 6*n)
    for i:= 0; i < 6; i++ {
        dp[i] = 1
    }
    for time := 2; time <= n; time++ {
        for i := 6*time-1; i >= 0; i-- {
            dp[i] = 0
            for j := 6; j > 0; j-- {
                if i < j { continue }
                dp[i] += dp[i-j]
            }
        }
    }
    return dp[n-1:]
}



丶Axe
6个月前

Talk is cheap.

class Solution(object):
    def numberOfDice(self, n):
        dp = [0] * (6 * n)
        dp[0:6] = [1] * 6
        for time in range(2,n+1):
            for i in range(6*time-1, -1, -1):
                dp[i]=0
                for j in range(6, 0, -1):
                    if i < j:
                        continue
                    dp[i] += dp[i - j]
        return dp[n-1:]



丶Axe
6个月前

递归过不了,栈太深。sum ? 或者 reduce

class Solution(object):
    def getSum(self, n):
        import functools
        return functools.reduce(lambda x, y: x+y, range(n+1))




丶Axe
7个月前

Talk is cheap.

func findNumberAppearingOnce(nums []int) int {
    var a, b int
    for _, v := range nums {
        a = (a ^ v) & ^b
        b = (b ^ v) & ^a
    }
    return a
}