Skip to content

Commit 4fb9642

Browse files
committed
367 Valid Perfect Square and code style
1 parent 91c4661 commit 4fb9642

8 files changed

+73
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
| 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
109109
| 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n) |
110110
| 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/366_Find_Leaves_of_Binary_Tree.py) | 1. Set or hash to check leaft, O(n^2) and O(n)<br>2. Recursively check level and return them, O(n) and O(n)|
111+
| 367 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/367_Valid_Perfect_Square.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/367_Valid_Perfect_Square.java) | [Integer square root](https://en.wikipedia.org/wiki/Integer_square_root#Using_only_integer_division)<br>1. 1+3+…+(2n-1) = n^2<br>2. Binary search<br>3. Newton's method |
111112
| 368 | [Largest Divisible Subset](https://leetcode.com/problems/largest-divisible-subset/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/368_Largest_Divisible_Subset.py) | Sort and generate x subset with previous results, O(n^2) and O(n^2) |
112113
| 369 | [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/369_Plus_One_Linked_List.py) | 1. Stack or list that store the list, O(n) and O(n)<br>2. Two points, the first to the tail, the second to the latest place that is not 9, O(n) and O(1) |
113114
| 370 | [Range Addition](https://leetcode.com/problems/range-addition/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/370_Range_Addition.py) | Interval problem with cumulative sums, O(n + k) and O(n) |

java/367_Valid_Perfect_Square.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
/* public boolean isPerfectSquare(int num) {
3+
int i = 1;
4+
while (num > 0) {
5+
num -= i;
6+
i += 2;
7+
}
8+
return num == 0;
9+
} */
10+
11+
public boolean isPerfectSquare(int num) {
12+
int low = 1;
13+
int high = num;
14+
while (low <= high) {
15+
long mid = (low + high) >>> 1;
16+
if (mid * mid == num) {
17+
return true;
18+
} else if (mid * mid < num) {
19+
low = (int) mid + 1;
20+
} else {
21+
high = (int) mid - 1;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
/* public boolean isPerfectSquare(int num) {
28+
long x = num;
29+
while (x * x > num) {
30+
x = (x + num / x) >> 1;
31+
}
32+
return x * x == num;
33+
} */
34+
}

python/001_Two_Sum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ class Solution(object):
4747

4848
def twoSum(self, nums, target):
4949
# two point
50-
nums_index = [(v, index)for index, v in enumerate(nums)]
50+
nums_index = [(v, index) for index, v in enumerate(nums)]
5151
nums_index.sort()
5252
begin, end = 0, len(nums) - 1
5353
while begin < end:
5454
curr = nums_index[begin][0] + nums_index[end][0]
5555
if curr == target:
5656
return [nums_index[begin][1], nums_index[end][1]]
5757
elif curr < target:
58-
begin +=1
58+
begin += 1
5959
else:
6060
end -= 1
6161

6262

6363
if __name__ == '__main__':
6464
# begin
6565
s = Solution()
66-
print s.twoSum([3,2,4], 6)
66+
print s.twoSum([3, 2, 4], 6)

python/002_Add_Two_Numbers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ def __init__(self, x):
44
self.val = x
55
self.next = None
66

7+
78
class Solution(object):
89
# def addTwoNumbers(self, l1, l2):
910
# """
@@ -54,6 +55,3 @@ def addTwoNumbers(self, l1, l2):
5455
if carry > 0:
5556
curr.next = ListNode(carry)
5657
return head.next
57-
58-
59-

python/003_Longest_Substring_Without_Repeating_Characters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ def lengthOfLongestSubstring(self, s):
6666
# duplicate character in current i,j. So we need to update i.
6767
if charMap[ord(s[j])] >= i:
6868
i = charMap[ord(s[j])] + 1
69-
charMap[ord(s[j])] = j
69+
charMap[ord(s[j])] = j
7070
max_len = max(max_len, j - i + 1)
7171
return max_len

python/035_Search_Insert_Position.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,3 @@ def searchInsert(self, nums, target):
3333
if nums[l] < target:
3434
return l + 1
3535
return l
36-
37-

python/367_Valid_Perfect_Square.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
# def isPerfectSquare(self, num):
3+
# """
4+
# :type num: int
5+
# :rtype: bool
6+
# """
7+
# i = 1
8+
# while num > 0:
9+
# num -= i
10+
# i += 2
11+
# return num == 0
12+
13+
def isPerfectSquare(self, num):
14+
low, high = 1, num
15+
while low <= high:
16+
mid = (low + high) / 2
17+
mid_square = mid * mid
18+
if mid_square == num:
19+
return True
20+
elif mid_square < num:
21+
low = mid + 1
22+
else:
23+
high = mid - 1
24+
return False
25+
26+
# def isPerfectSquare(self, num):
27+
# x = num
28+
# while x * x > num:
29+
# x = (x + num / x) / 2
30+
# return x * x == num

python/372_Super_Pow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ def superPow(self, a, b):
1313
if b is None or len(b) == 0:
1414
return 1
1515
last_digit = b.pop()
16-
return self.powmod(self.superPow(a, b), 10) * self.powmod(a, last_digit) % self.base
17-
16+
return self.powmod(self.superPow(a, b), 10) * \
17+
self.powmod(a, last_digit) % self.base
1818

1919
def powmod(self, a, k):
2020
a %= self.base
2121
result = 1
2222
for i in range(k):
2323
result = (result * a) % self.base
24-
return result
24+
return result

0 commit comments

Comments
 (0)