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 ...
随机推荐
- 如何让html引用公共布局(多个html文件公用一个header.html和footer.html)
如何实现多个.html静态页,引用同一个header.html和footer.html文件? 直接上代码: 公共头部文件:header.html //不用写标准的html文档格式 <div> ...
- 【基准测试】BenchmarkDotNet介绍
BenchmarkDotNet 概述 BenchmarkDotNet helps you to transform methods into benchmarks, track their perfo ...
- vue项目开发中遇到的问题总结
(转自)https://www.cnblogs.com/zifayin/p/8312677.html 1.路由变化页面数据不刷新问题 这种情况一般出现在vue-router的history模式下,初次 ...
- MyBatis入门知识汇总
为什么要使用MyBatis? 我们都知道,在学习mybatis之前,要在Java中操作数据库,需要用到JDBC,但是在使用JDBC时会有许多缺陷. 比如: 1.使用时需要先进行数据库连接,不用后要立 ...
- python之Python Eclipse+PyDec下载和安装教程(超级详细)
Eclipse 是著名的跨平台 IDE 工具,最初 Eclipse 是 IBM 支持开发的免费 Java 开发工具,2001 年 11 月贡献给开源社区,目前它由非盈利软件供应商联盟 Eclipse ...
- 在 n 道题目中挑选一些使得所有人对题目的掌握情况不超过一半。
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...
- BZOJ1066 网络流
拆点,将一个柱子拆成入点和出点,入点出点之间的容量就是柱子的容量 1066: [SCOI2007]蜥蜴 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多 ...
- SpringBoot2.x快速入门指南(一)
SpringBoot2.x快速入门指南(一) 准备工作 IDE: IntelliJ IDEA 2020.3 Java环境 jdk1.8 在官网快速创建SpringBoot项目 下面开始进入正题: 进入 ...
- SpringBoot2.1电商通用(微信+支付宝)支付系统实战
『课程目录』: ├─第10章 全模块电商系统之商品模块 │ 10-1_商品列表-上.mp4 │ 10-2_商品列表-中.mp4 │ 10-3_商品列表-下.mp4 │ ...
- 第二篇-用Flutter手撸一个抖音国内版,看看有多炫
前言 继上一篇使用Flutter开发的抖音国际版 后再次撸一个国内版抖音,大部分功能已完成,主要是Flutter开发APP速度很爽, 先看下图 项目主要结构介绍 这次主要的改动在api.dart 及 ...