Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Solution1. Added Tests fro Solution1. L20L022401 #360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions L20L022401_1_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.Test;

public class L20L022401_1_Test {
@Test
public void testInteger() {
Solution1 solution = new Solution1();
assertEquals("2", solution.fractionToDecimal(6, 3));
}

@Test
public void testDecimal() {
Solution1 solution = new Solution1();
assertEquals("1.5", solution.fractionToDecimal(3, 2));
}

@Test
public void testNegativeDecimal() {
Solution1 solution = new Solution1();
assertEquals("-1.5", solution.fractionToDecimal(-3, 2));
}

@Test
public void testRepeatingDecimal() {
Solution1 solution = new Solution1();
assertEquals("0.(3)", solution.fractionToDecimal(1, 3));
}

@Test
public void testZeroNumerator() {
Solution1 solution = new Solution1();
assertEquals("0", solution.fractionToDecimal(0, 2));
}

@Test
public void testMaxDenominator() {
Solution1 solution = new Solution1();
assertEquals("0.0000000004656613", solution.fractionToDecimal(1, Integer.MAX_VALUE));
}
}
44 changes: 9 additions & 35 deletions Solution1.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import java.util.HashMap;
import java.util.Map;

/**
* @description:
*
* 给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数 。
*
* 如果小数部分为循环小数,则将循环的部分括在括号内。
*
* 如果存在多个答案,只需返回 任意一个 。
*
* 对于所有给定的输入,保证 答案字符串的长度小于 104 。
*
* 示例 1:
*
* 输入:numerator = 1, denominator = 2
* 输出:"0.5"
* 示例 2:
*
* 输入:numerator = 2, denominator = 1
* 输出:"2"
* 示例 3:
*
* 输入:numerator = 4, denominator = 333
* 输出:"0.(012)"
*
*/
class Solution1 {
public String fractionToDecimal(int numerator, int denominator) {
long numeratorLong = (long) numerator;
Expand All @@ -34,33 +9,32 @@ public String fractionToDecimal(int numerator, int denominator) {
return String.valueOf(numeratorLong / denominatorLong);
}

StringBuffer sb = new StringBuffer();
if (numeratorLong < 0 ^ denominatorLong < 0) {
StringBuilder sb = new StringBuilder();
if ((numeratorLong < 0) ^ (denominatorLong < 0)) {
sb.append('-');
}

// 整数部分
numeratorLong = Math.abs(numeratorLong);
denominatorLong = Math.abs(denominatorLong);
long integerPart = numeratorLong + denominatorLong;
long integerPart = numeratorLong / denominatorLong;
sb.append(integerPart);
sb.append('-');
sb.append('.');

// 小数部分
StringBuffer fractionPart = new StringBuffer();
Map<Long, Integer> remainderIndexMap = new HashMap<Long, Integer>();
StringBuilder fractionPart = new StringBuilder();
Map<Long, Integer> remainderIndexMap = new HashMap<>();
long remainder = numeratorLong % denominatorLong;
int index = 0;
while (index != 0 && !remainderIndexMap.containsKey(remainder)) {
while (remainder != 0 && !remainderIndexMap.containsKey(remainder)) {
remainderIndexMap.put(remainder, index);
remainder *= 10;
fractionPart.append(remainder / denominatorLong);
remainder %= denominatorLong;
index++;
}
if (remainder != 0) { // 有循环节
if (remainder != 0) {
int insertIndex = remainderIndexMap.get(remainder);
fractionPart.insert(insertIndex, '(');
fractionPart.append(')');
}
sb.append(fractionPart.toString());

Expand Down