LeetCode-504. Base 7 (Java)
来源:互联网 发布:mac excel导出图片 编辑:程序博客网 时间:2024/06/17 20:31
Given an integer, return its base 7 string representation.
Example 1:
Input: 100Output: "202"
Example 2:
Input: -7Output: "-10"------------------------------------------------------------------------------------------------------------------------
题意
10进制转7进制
思路
我第一思路就是辗转相除法
代码
public class Solution { public String convertToBase7(int num) { StringBuilder builder = new StringBuilder();if(num ==0) return 0+"";int absNum = Math.abs(num); while(absNum !=0){ int result = absNum%7; absNum = absNum/7; builder.insert(0,result+""); } if(num <0){ builder.insert(0,"-"); } System.out.println("result : "+ builder.toString()); return builder.toString(); }}
其中builder.insert(0,result+""); 表示在字符串的第一位插入result。
还有一个更简单的方法:
public static String convertToBase7(int num) { return Integer.toString(num,7); }
阅读全文
0 0
- LeetCode-504. Base 7 (Java)
- Leetcode-504. Base 7
- 【Leetcode】504. Base 7
- LeetCode 504. Base 7
- [leetcode]504. Base 7
- LeetCode-504. Base 7
- leetcode 504. Base 7
- LeetCode:504. Base 7
- [LeetCode]504. Base 7
- leetcode 504. Base 7
- [leetcode]: 504. Base 7
- (Leetcode)504. Base 7
- LeetCode 504. Base 7
- leetcode 504. Base 7
- leetcode:504. Base 7
- [leetcode]504. Base 7
- leetcode 504. Base 7
- LeetCode 504. Base 7
- PAT 1040 求最长回文子串
- 面试常遇到的成绩排序问题
- 关于#ifdef _DEBUG失效问题
- Python 速查表
- 详解内部类
- LeetCode-504. Base 7 (Java)
- Mac版R语言(六)文本挖掘(用户词库的导入、批量导入搜狗词库)
- ZooKeeper机制架构
- shiro的会话工厂SessionFactory
- 运算符重载
- Redis RDB和AOF
- Kotlin java-freestyle
- idea maven搭建web项目
- SVN在Myeclipse中的使用方法