leetcode166
public class Solution {
public String fractionToDecimal(int numerator, int denominator) {
HashMap<Long,Integer> maps = new HashMap<>();//store divid number
List<Long> number = new ArrayList<>();
int index = 0;
int beginIndex = -1;
StringBuilder builder = new StringBuilder();
if(denominator==0)
return "";
long num = numerator;
long denum = denominator;
if((num<0 && denum>0) || (num>0 && denum<0))
builder.append('-');
num = Math.abs(num);
denum = Math.abs(denum);
long val = num/denum;
builder.append(String.valueOf(val));
num = (num%denum)*10;
while(num!=0){
if(maps.containsKey(num)){//开始重复
beginIndex = maps.get(num);
break;
}else{
maps.put(num, index++);
val = num/denum;
num = (num%denum)*10;
number.add(val);
}
}
for(int i = 0;i<index;i++){
if(i==0)
builder.append('.');
if(i==beginIndex){
builder.append('(');
}
builder.append(number.get(i));
}
if(beginIndex!=-1)
builder.append(')');
return builder.toString();
}
}
leetcode166的更多相关文章
- [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- leetcode166 Fraction to Recurring Decimal
思路: 模拟. 实现: class Solution { public: string fractionToDecimal(int numerator, int denominator) { long ...
- leetcode-166周赛-5282-转化为全0矩阵的最小反转次数
题目描述: 方法一:暴力BFS class Solution: def minFlips(self, mat) -> int: R, C = len(mat), len(mat[0]) def ...
- leetcode-166周赛-5280-用户分组
题目描述: 自己的提交: class Solution: def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]: ...
- Leetcode166. Fraction to Recurring Decimal分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...
随机推荐
- [题解] CodeM美团点评编程竞赛资格赛题
最近看到牛课网美团一个编程竞赛,想着做做看,结果一写就是两天..真是写不动了啊.话不多说,下面开始我的题解. 题目大致还是比较考察思维和代码能力(因为自己代码能力较弱,才会觉得比较考察代码能力吧= = ...
- python 获取excel文件的所有sheet名字
当一个excel文件的sheet比较多时候, 这时候需要获取所有的sheet的名字. xl = pd.ExcelFile('foo.xls') xl.sheet_names # see all she ...
- MySQL Transaction--快照读和当前读
在MySQL读取数据时可以按照是否使用一致性非锁定读来分为快照读和当前读:1.快照读:MySQL使用MVCC (Multiversion Concurrency Control)机制来保证被读取到数据 ...
- oracle 与sql serve 获取随机行数的数据
Oracle 随机获取N条数据 当我们获取数据时,可能会有这样的需求,即每次从表中获取数据时,是随机获取一定的记录,而不是每次都获取一样的数据,这时我们可以采取Oracle内部一些函数,来达到这 ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- 学hadoop需要什么基础
最近一段时间一直在接触关于hadoop方面的内容,从刚接触时的一片空白,到现在也能够说清楚一些问题.这中间到底经历过什么只怕也就是只有经过的人才会体会到吧.前几天看到有个人问“学hadoop需要什么基 ...
- DCNN相关算法资料
https://blog.csdn.net/hjimce/article/details/49955149 讲的很详细
- 解决EF一对一或多对一的删除
people 类中有 zhengshu类 且是一对一,现在要删除people类中的zhengshu 网上看了N多办法,什么更新外键什么滴. 其实方法简单极了 using (KJExamEntity c ...
- Linux strace命令 一
简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...
- mui plus.uploader.createUpload 上传文件服务端获取文件名中文乱码问题
客户端上传文件需要做一次url编码:encodeURIComponent(fileName) 服务端:URL解码 var fileName = HttpUtility.UrlDecode(hfc.Fi ...