LeetCode Base 7
原题链接在这里:https://leetcode.com/problems/base-7/#/description
题目:
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].
题解:
每次num%7放在前面. num再除以7. 最后加上正负号.
注意corner case: num = 0.
Time Complexity: O(num/7). Space: O(1).
AC Java:
public class Solution {
public String convertToBase7(int num) {
if(num == 0){
return "0";
}
boolean isNeg = false;
if(num < 0){
isNeg = true;
}
num = Math.abs(num);
StringBuilder sb = new StringBuilder();
while(num != 0){
sb.insert(0, num%7);
num /= 7;
}
return isNeg ? "-"+sb.toString() : sb.toString();
}
}
LeetCode Base 7的更多相关文章
- [LeetCode] Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- [LeetCode] Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [LeetCode] 483. Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [LeetCode] 504. Base 7 基数七
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
- 【LeetCode】504. Base 7 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 内建库 BigInteger类 逐位计算 倍数相加 ...
- 45. leetcode 504. Base 7
504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: ...
- LeetCode算法题-Base 7(Java实现)
这是悦乐书的第247次更新,第260篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第114题(顺位题号是504).给定一个整数,返回其基数为7的字符串表示.例如: 输入: ...
- #Leetcode# 1009. Complement of Base 10 Integer
https://leetcode.com/problems/complement-of-base-10-integer/ Every non-negative integer N has a bina ...
- [LeetCode&Python] Problem 504. Base 7
Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202&q ...
随机推荐
- 计算机网络概述---OSI参考模型
应用层:所有能产生网络流量的程序,例如:qq等,txt记事本没有产生流量,所以不属于应用层: 表示层:在传输之前对应用层的数据进行加工或处理,例如:加密.压缩.传视频时二进制,传文档时ASCII码 等 ...
- Java基础面试集合
1.面向对象的特征有哪些方面? 抽象 封装 继承 多态,多态性是指允许不同子类型的对象对同一消息作出不同的响应.简单的说就是用同样的对象引用调用同样的方法但是做了不同的事情.多态性分为编译时的多态性和 ...
- java基础之常量与变量
概要:通过这段时间的工作,发现自己的基础还是很薄弱的,so,you know 常量 一种特殊的变量,程序运行过程中不能改变的值 语法格式:final 数据类型 常量名称 = 常量值 例子:fina i ...
- IDEA中集成JRebel插件
下载下面2个插件 jr-ide-intellij-6.4.3_13-16.zip --- 官网的jar(地址:https://plugins.jetbrains.com/plugin/4441-jre ...
- 【P2014】选课(树状DP)
蒟蒻的第二道树形DP,话说看了这个题的正常做法之后一脸蒙,森林转二叉树??什么诡异的操作,蒟蒻完全没明白那个原理是啥...可能是当初没好好学吧..不管了,索性直接DP. 不难看出,这个题的DP方程和刚 ...
- CocoaPods安装使用
$ gem sources --remove https://rubygems.org/ //等有反应之后再敲入以下命令 $ gem sources -a http://ruby.taobao.org ...
- window 安装 thrift
1.下载thrift:http://thrift.apache.org/ 2.然后将该执行文件,保存到磁盘的文件夹下(你自己喜欢的,随便一个个目录下) 我的是(目录名少了个t,无妨): 3.把该执行文 ...
- mysql基础(5)-关联(mysql+pandas)
表关联类型 内连接: 仅显示满足条件的行 From T1,T2 where T1.ID=T2.ID From T1 inner join T2 ON T1.ID=T2.ID 左连接: 显示左表T1中的 ...
- 将本地jar包制作成maven依赖
首先,需要配置maven环境,如果没配置,传送门:http://www.cnblogs.com/hyyq/p/6557683.html 然后,需要一个ben地的jar包,这里以阿里云支付宝sdk为例, ...
- enter回车---焦点切换
$(function(){ $('.enter').bind('keydown',function(e){ var inputs = $('.enter_cash'); var key = e.whi ...