504. Base 7
Given an integer, return its base 7 string representation.Example 1:Input: 100
Output: "202"Example 2:Input: -7
Output: "-10"Note: The input will be in range of [-1e7, 1e7].
解题:题意很容易理解,将十进制整型数转化为七进制的数,并以字符串的形式返回。先看我写的第一种方法,比较繁琐,也利用了StringBuffer和堆栈,代码如下:
1 class Solution { 2 public String convertToBase7(int num) { 3 Stack<Integer>stack = new Stack<Integer>(); 4 boolean isNagative = false; 5 if(num < 0){ 6 isNagative = true; 7 num = -num; 8 } 9 while(num != 0){ 10 stack.push(num % 7); 11 num /= 7; 12 } 13 StringBuffer result = new StringBuffer(""); 14 15 while(!stack.isEmpty()) result.append(String.valueOf(stack.pop())); 16 if(result.length() == 0) 17 return String.valueOf(0); 18 else if(isNagative) 19 return '-'+result.toString(); 20 else return result.toString(); 21 } 22 }
稍微改进一下,去掉栈和StringBuffer,直接使用String及其性质,速度会快很多,代码如下:
1 class Solution { 2 public String convertToBase7(int num) { 3 boolean isNagative = false; 4 String result = ""; 5 if(num < 0){ 6 isNagative = true; 7 num = -num; 8 } 9 while(num != 0){ 10 result = String.valueOf(num % 7) + result; 11 num /= 7; 12 } 13 if(result.length() == 0) 14 return String.valueOf(0); 15 else if(isNagative) 16 return '-'+result; 17 else return result; 18 } 19 }
当然也可以这样,虽然有点投机取巧:
1 public String convertToBase7(int num) { 2 return Integer.toString(num, 7); 3 }
转载于:https://www.cnblogs.com/phdeblog/p/9168181.html
504. Base 7相关推荐
- LeetCode 504. Base 7
504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...
- LeetCode 504. Base 7 (C++)
题目: Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "2 ...
- LeetCode 简单算法题
使用Nodejs 抓取的LeetCode 简单算法题 一步一步来,先攻破所有简单的题目,有些题目不适合使用JS解决,请自行斟酌 Letcode 简单题汇总 104. Maximum Depth of ...
- LeetCode力扣刷题——巧解数学问题
数论 一.引言 对于 LeetCode 上数量不少的数学题,我们尽量将其按照类型划分讲解.然而很多数学题的解法并不通用,我们也很难在几道题里把所有的套路讲清楚,因此我们只选择了几道经典 ...
- Leetcode题解-算法-数学
文章目录 1.进制转换 1.1 小于 n 的所有素数 1.2 7进制 1.3 16进制 1.4 26进制 2.阶乘 2.1 阶乘的尾部有多少0 3.字符串加法减法 3.1 二进制加法 3.2 字符串加 ...
- Leetcode算法题-解法转载
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...
- Leetcode题解(超赞!!!)
我是技术搬运工,好东西当然要和大家分享啦.原文地址 算法思想 二分查找 二分查找思想简单,但是在实现时有一些需要注意的细节: 在计算 mid 时不能使用 mid = (l + h) / 2 这种方式, ...
- Leetcode 部分题解
算法思想 双指针 排序 快速选择 堆排序 桶排序 荷兰国旗问题 贪心思想 二分查找 分治 搜索 BFS DFS Backtracking 动态规划 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...
最新文章
- python使用手册-python(自用手册)
- 一篇不错的讲DHCP的文章
- 当中国数据中心建设提速,能耗问题引关注
- 恢复SAP IDES中SPFLI SFLIGHT SBOOK表数据
- 微服务扩展新途径:Messaging
- cxgrid中纵横单元格合并_逆向查询合并单元格中的数据,这两种方法你值得拥有...
- 深度学习算法 第四期
- 矩阵分解在推荐系统中的应用:NMF和经典SVD实战
- 管理感悟:套路就是文档化
- AJAX在IE下的调试
- 计算机考苏州公务员考试,苏州公务员考试难度
- 【操作系统 · 调度】多处理器 实时调度
- 算法系列——遗传算法matlab编程详解
- NOIP提高组【JZOJ4809】挖金矿
- python爬虫爬取豆瓣电影为啥内容有缺失-Python爬虫之抓取豆瓣影评数据
- 方框加对勾怎么输入_Word怎么输入对号和方框对勾
- 支付宝即时到账之给二维码设置超时失效时间
- Problem I. Hall of Fame (2014 Syria ICPC)
- 2017年问题汇总-待整理
- curl模拟发送post请求参数通过json格式传输时需要对引号进行转义