1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { fun numberOfMatches(n: Int): Int { var remains = n var cnt = 0 while (remains > 1) { if (remains % 2 == 0) { cnt += remains / 2 remains /= 2 } else { cnt += (remains - 1) / 2 remains = (remains - 1) / 2 + 1 } } return cnt } }
|