Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4dd378e

Browse files
committed
FEAT Optimized recursive toBinaryString method
1 parent 3dceff6 commit 4dd378e

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

‎src/PracticeAlgorithms.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public static String toBinaryString(int x) {
77
StringBuilder binaryString = new StringBuilder();
88
binaryString.append(binaryFormString(x, binaryString.toString()));
99

10-
return binaryString.reverse().toString();
10+
return binaryString.toString();
1111
}
1212

1313
private static String binaryFormString(int x, String binString) {
1414
if (x == 1) return binString + "1";
1515

16-
return (x % 2) + binaryFormString(x / 2, binString);
16+
return binaryFormString(x / 2, binString) + (x % 2);
1717
}
1818

1919
public static void main(String[] args) {
@@ -22,6 +22,6 @@ public static void main(String[] args) {
2222

2323
System.out.println(isBetweenZeroAndOne(testX, testY));
2424

25-
System.out.println(toBinaryString(7));
25+
System.out.println(toBinaryString(8));
2626
}
2727
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /