Java实现 LeetCode 166 分数到小数
166. 分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。
如果小数部分为循环小数,则将循环的部分括在括号内。
示例 1:
输入: numerator = 1, denominator = 2
输出: “0.5”
示例 2:
输入: numerator = 2, denominator = 1
输出: “2”
示例 3:
输入: numerator = 2, denominator = 3
输出: “0.(6)”
class Solution {
public String fractionToDecimal(int numerator, int denominator) {
if (numerator == 0 || denominator == 0) return "0";
int sign = 1;
if (numerator > 0 && denominator < 0) sign = -1;
long big = (long) numerator / (long) denominator;
long small = numerator % denominator;
StringBuilder result = new StringBuilder(String.valueOf(big));
if (sign == -1) result.insert(0, "-");
if (small != 0) {
result.append(".");
StringBuilder smallStr = new StringBuilder();
Map<String, Integer> smallIndexs = new HashMap<String, Integer>();
while (small != 0) {
small *= 10;
big = small / denominator;
small = small % denominator;
String str = small + "_" + big;
if (smallIndexs.containsKey(str)) {
smallStr.append(")");
smallStr.insert(smallIndexs.get(str), "(");
break;
} else {
smallIndexs.put(str, smallStr.length());
smallStr.append(Math.abs(big));
}
}
result.append(smallStr);
}
return result.toString();
}
}
Java实现 LeetCode 166 分数到小数的更多相关文章
- Leetcode 166.分数到小数
分数到小数 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: num ...
- leetcode 166分数到小数
手动排除特殊情况: 对于一般情况,使用位运算和加减法来计算除法,使用sign记录结果符号:(这部分为leetcode 29题的答案) 使用hashmap来记录循环体出现的开始位置(如果有的话),使用f ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java实现 LeetCode 592 分数加减运算(纯体力活)
592. 分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- vue路由元之进入路由需要用户登录权限功能
为什么需要路由元呢??? 博猪最近开发刚刚好遇到一个情况,就是有个路由页面里面包含了客户的信息,客户想进这个路由页面的话, 就可以通过请求数据获取该信息,但是如果客户没有登录的话,是不能进到该页面的, ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- [hdu5033]单调队列
题意:x轴上有n棵树,询问你站在某个点的视角.从左至右,单调队列(类似凸包)维护下.我强迫症地写了个模板QAQ #include <iostream> #include <cstdi ...
- An SWT error has occurred
对话框标题:Problem Occurred 对话框内容:Unhandled event loop exception No more handles 对话框按钮:第一个超链接:Show Error ...
- AndroidStudio3.6升级后的坑-apk打包
前段时间尝试了最新版的AndroidStudio3.6,整体来说gradle调试和自带的虚拟机相比较历史版本有了更香的体验. 刚好有个新项目,就直接使用最新版了,这次新版的升级除了保持原有的界面风格, ...
- Python单元测试框架:unittest(二)
一.直接使用TestCase 注意所有测试方法都需要以test开头.代码如下: import unittest class Test1(unittest.TestCase): @classmethod ...
- vue打包 报错问题记录
1. assetsPublicPath: '/' 修改为 assetsPublicPath: './' 2. untils.js里面增加 publicPath: '../../' 重新运行 ...
- 对CSS3中的transform:Matrix()矩阵的一些理解
只要有CSS基础的人肯定都知道,我们可以通过transform中的translate,scale,rotate,skew这些方法来控制元素的平移,缩放,旋转,斜切,其实这些方法呢都是为了便于开发者使用 ...
- [转]如何用Vmware 10虚拟化物理机
在重装系统的过程中总会遇到这种情况,平时用的东西一下子就找不到了,比如办公记录,考勤记录等等.虽然在重装系统之前你一般都会备份一些重要文件,但是往往都会漏备份一些之前忽略文件,还有一些自己使用电脑的“ ...
- 转载 配置vue项目
Vue作为前端三大框架之一截至到目前在github上以收获44,873颗星,足以说明其以悄然成为主流.16年10月Vue发布了2.x版本,经过了一段时间的摸索和看官方的教程和api,才了解到2.0版本 ...