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 ...
随机推荐
- UI控件之UIView与动画
UIView:用来展示用户的界面,响应用户的操作(继承自UIResponder) UIView的作用:绘图.动画.处理事件 UIView可以包含和管理子视图,并且决定子视图的位置大小 获取所有的子视图 ...
- Java底层代码实现单文件读取和写入(解决中文乱码问题)
需求: 将"E:/data/车站一次/阿坝藏族羌族自治州.csv"文件中的内容读取,写入到"E:/data//车站一次.csv". 代码: public cla ...
- 获取电脑连接WiFi的信息
在cmd中执行如下命令,即可查看到所有连接过的WiFi信息 for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show ...
- 【TopCoder】SRM152 DIV2总结
为什么平常刷的时候感觉还不错,比赛的时候只能做出来一道题=.= 250分题:大水题,根据题目规则把一个字符串翻译成数字,直接代码:GitHub 我是通过遍历一个个数出来的,看到大神的解法是把字符用‘- ...
- CSS3带小图标垂直下拉菜单
在线演示 本地下载
- 非root权限的linux用法添加工作路径
修改~目录的bashrc文件: 1.cd 到~目录. 2.ls -a ,bashrc文件是隐藏的. 3.vim .bashrc;export PATH=$PAHT:要添加的工作路径. 4.source ...
- linux虚拟机ping通主机
右键虚拟机,选择网络适配器,设置为桥接模式.然后关闭主机防火墙,ping就行了(一直ping是没有参数的)
- JSP DAO(Model)
示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...
- Kubernetes pod网络解析
在Kubernetes中,会为每一个pod分配一个IP地址,pod内的所有容器都共享这个pod的network namespace,彼此之间使用localhost通信. 那么pod内所有容器间的网络是 ...
- MSSqlserver的锁模式介绍
一 SQL Server 锁类型的说明 在SQL Server数据库中加锁时,除了可以对不同的资源加锁,还可以使用不同程度的加锁方式,即有多种模式,SQL Server中锁模式包括: 1.共享锁(S) ...