Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion in O(1) runtime? 這一題說實在比較偏向數學公式題... 本來我還老老實實的作加總後再比較 結果他這個東西是Digital root的數學觀念 資料在Wiki https://en.wikipedia.org/wiki/Digital_root 帶入此公式 dr(n) = 1 + ((n - 1) mod 9) 就可以求解...
只是說找到Digital root的應用真的太偏了,最多就是建立觀念而已
沒有留言 :
張貼留言