2020-05-01から1ヶ月間の記事一覧

GoでのNodeの扱い方

TreeNodeの次をとる方法。 /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func getDecimalValue(head *ListNode) int { curr, result := head, 0 for curr != nil { result = result<<1 result += …

Goで数値を文字列にする方法

strconvで変換する。 import ( "fmt" "strconv" ) func subtractProductAndSum(n int) int { sum, prod := 0, 1 for _, x := range fmt.Sprintf("%d", n) { xi, _ := strconv.Atoi(string(x)) sum += xi prod *= xi } return prod - sum }