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 d1c82de

Browse files
committed
Add solution 67.
1 parent 96ab883 commit d1c82de

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $a
5+
* @param String $b
6+
* @return String
7+
*/
8+
function addBinary($a, $b) {
9+
$sb = "";
10+
$i = strlen($a) - 1;
11+
$j = strlen($b) - 1;
12+
$carry = 0;
13+
14+
while($i >= 0 || $j >= 0 || $carry > 0)
15+
{
16+
$sum = $carry;
17+
if($i >= 0) $sum += ord($a[$i--]) - ord('0');
18+
if($j >= 0) $sum += ord($b[$j--]) - ord('0');
19+
$sb .= chr($sum % 2 + ord('0'));
20+
$carry = intval($sum / 2);
21+
}
22+
return strrev($sb);
23+
}
24+
}

0 commit comments

Comments
(0)

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