[leetCode] Fraction to Recurring Decimal
来源:互联网 发布:防止别人调用php接口 编辑:程序博客网 时间:2024/05/10 03:20
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)".
class Solution {public: string fractionToDecimal(int numerator, int denominator) { string ans = ""; long long nume = numerator; long long deno = denominator; if(denominator == 0)return ans; if(numerator == 0) return "0"; if(denominator < 0 ^ numerator < 0) { ans += '-'; nume = abs(nume); deno = abs(deno); } ans += to_string(nume / deno); long long rem = nume % deno * 10; if(rem == 0) return ans; ans += '.'; map<long long,int> m; long long res = 0; while(rem){ if(m.find(rem) != m.end()){ int begin = m[rem]; string s1 = ans.substr(0,begin); string s2 = ans.substr(begin); ans = s1 + '(' + s2 + ')'; break; } m[rem] = ans.length(); res = rem / deno; ans += to_string(res); rem = rem % deno * 10; } return ans; }};
0 0
- [leetcode] Fraction to Recurring Decimal
- LeetCode Fraction to Recurring Decimal
- Fraction to Recurring Decimal leetcode
- leetcode:Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [leetCode] Fraction to Recurring Decimal
- LeetCode:Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- leetcode:Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [leetcode]Fraction to Recurring Decimal
- LeetCode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- leetcode: Fraction to Recurring Decimal
- setContentView
- OpenCV——Skewing
- 空循环和无穷循环的区别
- 电子商务系统的设计与实现(十二):技术选型
- sql时间转换
- [leetCode] Fraction to Recurring Decimal
- Hadoop学习笔记(一):零Linux基础安装hadoop过程笔记
- VC6、VS2005环境搭建驱动开发环境注意事项
- 64位Eclipse运行时提示“Failed to load the JNI shared library \Java\jre6\bin\client\jvm.dll”的一个解决方案
- 条件编译
- Effective C++ 改善程序设计的55个具体做法(一)
- 系统瞬间关闭或重启
- 系统关机函数ExitWindowsEx详解
- 异常