[LeetCode 166] Fraction to Recurring Decimal

关于Arduino中的常用函数 一、I/O函数 二、时间函数 三、数学函数 abs 返回其参数的无符号值 【abs(12)返回12,abs(-12)也返回12】 constrain 限制一个数字以阻止它超出范围。第一个参数是被限制的数字,第二个参数是范围区间的开始,第三个参数是允许的数字范围区间的结尾。 【constrain(8,1,10)返回8,constrain(11,1,10)返回10,constrain(0,1,10)返回1】 map 将一个范围内的数字映射到另一个范围。第一个参数是要映射到数字,第二个和第三个参数是from范围( 阅读详情

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

If the fractional part is repeating, enclose the repeating part in parentheses.

For example,

  • Given numerator = 1, denominator = 2, return "0.5".
  • Given numerator = 2, denominator = 1, return "2".
  • Given numerator = 2, denominator = 3, return "0.(6)".
solution:

1. need to consider positive or negative

2. if exist decimal

3. how to record repeating decimals


public String fractionToDecimal(int numerator, int denominator) {
        if(numerator == 0) return "0";
        StringBuilder sb = new StringBuilder();
        if((numerator<0 && denominator>0) || (numerator>0 && denominator<0)){
            sb.append("-");
        }
        long num = (long) numerator;
        long den = (long) denominator;
        num = Math.abs(num);
        den = Math.abs(den);
        String round = String.valueOf(num/den);
        sb.append(round);
        long rem = (num%den) * 10;
        if(rem == 0) return sb.toString();
        //if has decimal
        Map<Long, Integer> fractionMap = new HashMap<Long, Integer>();
        sb.append(".");
        while(rem != 0) {
            //contains reminder
            if(fractionMap.containsKey(rem)) {
                String part1 = sb.substring(0, fractionMap.get(rem));
                String part2 = sb.substring(fractionMap.get(rem), sb.length());
                sb.delete(0, sb.length());
                sb.append(part1);
                sb.append("(");
                sb.append(part2);
                sb.append(")");
                return sb.toString();
            }
            fractionMap.put(rem, sb.length());
            sb.append(String.valueOf(rem/den));
            rem = (rem%den) *10;
        }
        return sb.toString();
     }



ThingsBoard 场景联动需求 当初有个兄弟请我帮他实现一个需求,需求如上,很简单,实现完成后我怕他操作不来,又写了这份文档给他,今天把这个文档的场景发出来,大家也可以学习一下思路我把需要模拟设备的指令都总结在下面,连接参数截图讲解如下:温度传感器风扇光照传感器灯。 阅读详情

相关推荐

S32K144之芯片锁死解锁——关闭debug端口

S32K144之芯片锁死及解锁 经常捣鼓单片机就会发现,时常会因为某些操作不当导致芯片锁死情况的发生,通过Jlink我们可以读到芯片的ID,但是无法进行Debug,这很是让人苦恼。 基于S32K144的芯片锁死,一般有如下几种可能: 1、时钟配置异常,过高的配置时钟频率以及不配置时钟,都有可能造成芯片保护锁死,我就曾经因为不注意把时钟配置代码注释掉,导致芯片锁死。 2、我是微软雅黑烧写断电,如果在...

weixin_41935674的博客 1万+

LeetCode 166. Fraction to Recurring Decimal

LeetCode 166. Fraction to Recurring Decimal 问题来源LeetCode 166. Fraction to Recurring Decimal 问题描述 Given two integers representing the numerator and denominator of a fraction, return the fraction in

yanqinghe123的专栏 305

奔图M6700-M6800-M7100-M7200 系列打印机拆解教程?

奔图M6700-M6800-M7100-M7200 系列打印机拆解教程?

m0_58098052的博客 1647

【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/fr...

负雪明烛 3603

【STM32】433MHz OOK信号软解码实战:从EV1527协议到嵌入式实现

本文详细介绍了基于STM32微控制器实现433MHz OOK信号软解码的完整实战过程。文章从EV1527协议解析入手,重点阐述了利用STM32的GPIO和定时器进行信号采样、脉宽还原、同步头识别以及数据位判决的核心算法,并提供了对抗无线干扰的容错优化策略,帮助开发者深入理解并掌握低成本无线通信的嵌入式实现方案。

weixin_29299807的博客 799

倍福Beckhoff运动控制功能块实战解析:从MC_ReadActualPosition到MC_GearInVelo

本文深入解析倍福(Beckhoff)TwinCAT运动控制功能块,从基础的MC_ReadActualPosition、MC_Power等轴状态读取与使能操作,到MC_MoveAbsolute、MC_MoveRelative等单轴定位指令,并重点剖析了用于动态速度同步的核心功能块MC_GearInVelo在飞锯等场景的应用。文章结合实战经验,详细介绍了齿轮耦合、电子凸轮(MC_CamIn)等高级同步功能的参数配置、调试技巧及常见问题,旨在帮助工程师快速掌握倍福运动控制工具箱,实现复杂的同步工艺。

transformer2023的博客 995

[leetcode] 166. Fraction to Recurring Decimal

Description Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parent...

Learning from the mistakes 290

别再死记硬背参数了!手把手教你用Fluent欧拉模型搞定流化床模拟(附颗粒粘度/体积分数设置详解)

本文深入解析Fluent欧拉模型在流化床模拟中的应用,重点讲解颗粒粘度和体积分数等关键参数的设置原理与科学方法。通过对比不同模型(如Syamlal-O'Brien、Gidaspow)的适用场景,帮助工程师摆脱参数死记硬背,实现精准模拟。内容涵盖相间作用机制、求解策略优化及结果验证,为化工、能源领域的流化床模拟提供实用指南。

weixin_42656416的博客 434

leetcode 166: Fraction to Recurring Decimal

Fraction to Recurring Decimal Total Accepted: 3168 Total Submissions: 27432 Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.

西施豆腐渣 2898

【unity】解决unity编译器安装中文汉化包失败

查看是否有Localization目录如果没有就进行创建,然后点击打开Localization把下载的zh-hans.po文件复制剪切进去即可。如果有的同学中文包安装失败,我们找到相应的编译器版本,点击在资源管理器中显示按钮,我们找到简体中文,复制downloadUrl后面的值到浏览器打开进行下载,点击Edtior language选择框,选择简体中文,等待加载。我们点击当前目录的上一级,进入编译器目录。返回在文件管理器中点击Editor目录。编译器启动后点击Edit[编辑]按钮。复制或者剪切下载的文件。

weixin_44959182的博客 8037

4.26华为OD机试真题 新系统 - 端口流量统计 (Java/Py/C/C++/Js/Go)

本文整理华为 OD 机试新系统机考真题库,涵盖高频考点精析与完整题目内容,提供 C 语言、Java、C++、Python、Go、JavaScript 多语言源码实现,搭配详细解题思路讲解,并支持在线 OJ 刷题练习,助力高效备考华为 OD 机试新系统。

算法大师! 2342

LeetCode(166) Fraction to Recurring Decimal

题目如下: Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parenthe

BUILD 3031

车企自动泊车技术全解|纯视觉/多传感器融合双路线、端到端大模型革新 助力机械车位/无划线车位/极窄车位精准泊车落地

随着智能驾驶技术全面下沉,自动泊车已从高端车型专属配置,普及至十万级家用车型,成为车载核心刚需功能。但市面主流车企的泊车能力差距极大,核心差异源于传感器硬件架构、算法迭代逻辑、场景训练体系的全方位分化。传统规则式泊车算法仅适配标准划线车位,面对地库暗光、标线磨损、机械双层车位、极窄靠墙车位、无划线空地泊车等真实长尾场景极易失效。

getusushu的博客 127

【leetcode】166. Fraction to Recurring Decimal

一、题目描述 Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in par

小拳头的博客 516

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode(166) Fraction to Recurring Decimal

题目Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For ex

指尖飞舞 1940

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

166. Fraction to Recurring Decimal Leetcode Python

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

hyperbolechi的专栏 1409

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Leetcode 166 Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

无名山丘,崛起成峰 700

LeetCode 166 Fraction to Recurring Decimal

题目描述Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.For

Yano_nankai的博客 539

LeetCode 166. Fraction to Recurring Decimal

题目来源:https://leetcode.com/problems/fraction-to-recurring-decimal/ 问题描述 166.Fraction to Recurring Decimal Medium Given two integers representing the numerator and denominator of a fraction, return...

da_kao_la的博客 233

leetcode[166] Fraction to Recurring Decimal

For example, Given numerator = 1, denominator = 2, return "0.5". Given numerator = 2, denominator = 1, return "2". Given numerator = 2, denominator = 3, return "0.(6)". 按照上例子实现结果。主要就是如何处理...

121

Leetcode 166. Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. Exa...

salmonwilliam的博客 387

leetcode——166—— Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For

happyxuma1991的博客 445

[Leetcode 166, Medium] Fraction to Recurring Decimal

[Leetcode 166, Medium] Fraction to Recurring Decimal

算法的实践 565

[leetcode] 166. Fraction to Recurring Decimal 解题报告

题目链接: https://leetcode.com/problems/fraction-to-recurring-decimal/ Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fra

小榕流光的专栏 1486

[leetcode 166] Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

xinxinxv的专栏 466

leetcode[166]Fraction to Recurring Decimal

---恢复内容开始--- Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in par...

weixin_30823001的博客 80
上一篇: [LeetCode 215] Kth Largest Element in an Array
下一篇: [LeetCode 216] Combination Sum III
Mavs
博客等级 码龄17年 166粉丝 296原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值