Skip to content

Commit 62fb77d

Browse files
authored
Merge pull request keon#84 from ankit167/Check_for_negative_numbers
Check if the input is negative
2 parents 816a018 + ca4e32f commit 62fb77d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

bit/count_ones.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
For example, the 32-bit integer ’11' has binary
77
representation 00000000000000000000000000001011,
88
so the function should return 3.
9+
10+
T(n)- O(log n)
911
"""
1012

1113

@@ -14,6 +16,8 @@ def count_ones(n):
1416
:type n: int
1517
:rtype: int
1618
"""
19+
if n < 0:
20+
return
1721
counter = 0
1822
while n:
1923
counter += n & 1

0 commit comments

Comments
 (0)