File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package leetcode .easy ;
2+
3+ /**
4+ * https://leetcode.com/problems/add-binary/submissions/
5+ */
6+ public class add_binary {
7+ public static void main (String [] args ) {
8+ String a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101" ;
9+ String b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011" ;
10+ System .out .println (addBinary (a , b ));
11+ }
12+
13+ static public String addBinary (String a , String b ) {
14+ StringBuilder sb = new StringBuilder ();
15+ int i = a .length () - 1 , j = b .length () - 1 , carry = 0 ;
16+ int sum = 0 ;
17+ while (i >= 0 || j >= 0 ) {
18+ sum = carry ;
19+ if (i >= 0 ) sum += a .charAt (i --) + '0' ;
20+ if (j >= 0 ) sum += b .charAt (j --) + '0' ;
21+ sb .append (sum % 2 );
22+ carry = sum / 2 ;
23+ }
24+ if (carry != 0 ) sb .append (carry );
25+ return sb .reverse ().toString ();
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package leetcode .easy ;
2+
3+ public class sqrtx {
4+ public static void main (String [] args ) {
5+ System .out .println (mySqrt (16 ));
6+ }
7+
8+ static public int mySqrt (int x ) {
9+ long r = x ;
10+ while (r * r > x )
11+ r = (r + x / r ) / 2 ;
12+ return (int ) r ;
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments