GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-1/python/067_Add_Binary.py at master · spinefire/leetcode-1

Expand file tree

/

067_Add_Binary.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
60 lines (59 loc) · 1.73 KB
/

067_Add_Binary.py

Copy path

File metadata and controls

60 lines (59 loc) · 1.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution(object):
# def addBinary(self, a, b):
# """
# :type a: str
# :type b: str
# :rtype: str
# """
# res = ''
# lsa, lsb = len(a), len(b)
# pos = -1
# plus = 0
# while (lsa + pos) >= 0 and (lsb + pos) >= 0:
# curr = int(a[pos]) + int(b[pos]) + plus
# if curr >= 2:
# plus = 1
# curr %= 2
# else:
# plus = 0
# res = str(curr) + res
# pos -= 1
# if lsa + pos >= 0:
# while (lsa + pos) >= 0:
# curr = int(a[pos]) + plus
# if curr >= 2:
# plus = 1
# curr %= 2
# else:
# plus = 0
# res = str(curr) + res
# pos -= 1
# if lsb + pos >= 0:
# while (lsb + pos) >= 0:
# curr = int(b[pos]) + plus
# if curr >= 2:
# plus = 1
# curr %= 2
# else:
# plus = 0
# res = str(curr) + res
# pos -= 1
# if plus == 1:
# res = '1' + res
# return res
def addBinary(self, a, b):
res = ''
lsa, lsb = len(a), len(b)
pos, plus, curr = -1, 0, 0
# plus a[pos], b[pos] and curr % 2
while (lsa + pos) >= 0 or (lsb + pos) >= 0:
if (lsa + pos) >= 0:
curr += int(a[pos])
if (lsb + pos) >= 0:
curr += int(b[pos])
res = str(curr % 2) + res
curr /= 2
pos -= 1
if curr == 1:
res = '1' + res
return res
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.