public class Solution {
    public string ConvertToBase7(int num) {
        var prefix = num >= 0 ? "" : "-";
            var list = new List<string>();
            StringBuilder sb = new StringBuilder();
            num = Math.Abs(num);
            do
            {
                var n = num % 7;
                sb.Append(n.ToString());
                num = num / 7;
            } while (num != 0);

            var result = sb.ToString() + prefix;
            var list2 = result.Reverse();
            sb.Clear();
            foreach (var c in list2)
            {
                sb.Append(c);
            }
            return sb.ToString();
    }
}

https://leetcode.com/problems/base-7/#/description

posted on 2017-04-19 11:21  Sempron2800+  阅读(135)  评论(0编辑  收藏  举报