Re: [閒聊] 每日LeetCode

看板 Marginalman
作者 JIWP (神楽めあ的錢包)
時間 2024-02-02 12:51:55
留言 2則留言 (2推 0噓 0→)

: https://leetcode.com/problems/sequential-digits/description : 1291. Sequential Digits : 一個Sequential Digits是一個數字滿足所有位數都比前面的位數恰好多出一例如: : 123 456,給你兩個數字low 和 high,求介於 low~high的所有Sequential Digits : 並排序之。 : 思路: : 1.用dfs窮舉所有的可能,不斷的把尾數+1並append到原本的數字直到超出high, : 因為測資範圍為 10~10^9 所以可以從12開始窮舉,然後排除掉尾數0的case。 思路: 先求出low和high分別是幾位數 建立一個123456789的string 接著用兩個迴圈去跑 外層是位數,內層是起點 從123456789這個string去取值 假設5位數,起點是2 那就是取34567 接著在把34567 atoi轉成int func sequentialDigits(low int, high int) []int { l := len(strconv.Itoa(low)) h := len(strconv.Itoa(high)) ans := []int{} s := "123456789" for i := l; i <= h; i++ { for j := 0; j <= 9-i; j++ { num, _ := strconv.Atoi(s[j : j+i]) if high >= num && num >= low { ans = append(ans, num) } } } return ans } --
※ 批踢踢實業坊(ptt.cc), 來自: 42.72.17.201 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1706849517.A.5A6.html

SecondRun: 大師 02/02 12:52

RinNoKareshi: 大師 02/02 12:54

您可能感興趣