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 29e4e31

Browse files
add python of 724
1 parent f65f33d commit 29e4e31

File tree

8 files changed

+139
-103
lines changed

8 files changed

+139
-103
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
6868
|498|[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_498.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
6969
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_557.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Conclusion
7070
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_561.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
71-
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_724.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png) | Introduction to Array
71+
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_724.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_724.py)| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png) | Introduction to Array
7272
|747|[Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_747.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to Array
7373

7474

‎codes/java/leetcodes/src/main/java/com/hit/basinfo/sort_algorithm/HeapSort.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* author:Charies Gavin
77
* date:2019年11月11日,19:30
88
* https:github.com/guobinhit
9-
* description: Merge Sort
9+
* description: Heap Sort
1010
*/
1111
public class HeapSort {
1212

‎codes/python/leetcodes/src/main/python/com/hit/basmath/binary_search/_278.py‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,21 @@
1919
"""
2020

2121

22-
class Solution:
23-
def firstBadVersion(self, n):
24-
"""
25-
:type n: int
26-
:rtype: int
27-
"""
28-
left = 1
29-
right = n
30-
while left < right:
31-
mid = left + (right - left) // 2
32-
""" wirte as Solution.isBadVersion way is in order to defend error tips """
33-
if Solution.isBadVersion(mid):
34-
right = mid
35-
else:
36-
left = mid + 1
37-
return left
38-
39-
""" Below function is not real isBadVersion """
40-
41-
def isBadVersion(self, num):
42-
return True
22+
def firstBadVersion(n):
23+
left = 1
24+
right = n
25+
while left < right:
26+
mid = left + (right - left) // 2
27+
""" wirte as Solution.isBadVersion way is in order to defend error tips """
28+
if isBadVersion(mid):
29+
right = mid
30+
else:
31+
left = mid + 1
32+
return left
33+
34+
35+
""" Below function is not real isBadVersion """
36+
37+
38+
def isBadVersion(num):
39+
return True

‎codes/python/leetcodes/src/main/python/com/hit/basmath/binary_search/_33.py‎

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,32 @@
1515
1616
Input: nums = [4,5,6,7,0,1,2], target = 0
1717
Output: 4
18+
1819
Example 2:
1920
2021
Input: nums = [4,5,6,7,0,1,2], target = 3
2122
Output: -1
2223
"""
2324

2425

25-
class Solution:
26-
def search(self, nums, target):
27-
"""
28-
:type nums: List[int]
29-
:type target: int
30-
:rtype: int
31-
"""
32-
left = 0
33-
right = len(nums) - 1
34-
while left <= right:
35-
mid = (left + right) // 2
36-
if nums[mid] == target:
37-
return mid
38-
39-
if nums[left] <= nums[mid]:
40-
if target < nums[mid] and target >= nums[left]:
41-
right = mid - 1
42-
else:
43-
left = mid + 1
44-
45-
if nums[mid] <= nums[right]:
46-
if target > nums[mid] and target <= nums[right]:
47-
left = mid + 1
48-
else:
49-
right = mid - 1
50-
51-
return -1
26+
def search(nums, target):
27+
left = 0
28+
right = len(nums) - 1
29+
while left <= right:
30+
mid = (left + right) // 2
31+
if nums[mid] == target:
32+
return mid
33+
34+
if nums[left] <= nums[mid]:
35+
if nums[mid] > target >= nums[left]:
36+
right = mid - 1
37+
else:
38+
left = mid + 1
39+
40+
if nums[mid] <= nums[right]:
41+
if nums[mid] < target <= nums[right]:
42+
left = mid + 1
43+
else:
44+
right = mid - 1
45+
46+
return -1

‎codes/python/leetcodes/src/main/python/com/hit/basmath/binary_search/_374.py‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,33 @@
1212
-1 : My number is lower
1313
1 : My number is higher
1414
0 : Congrats! You got it!
15+
1516
Example :
1617
1718
Input: n = 10, pick = 6
1819
Output: 6
1920
"""
2021

2122

22-
class Solution(object):
23-
def guessNumber(self, n):
24-
"""
25-
:type n: int
26-
:rtype: int
27-
"""
28-
left = 0
29-
right = n
30-
while left <= right:
31-
mid = (left + right) // 2
32-
""" wirte as Solution.guess way is in order to defend error tips """
33-
cmp = Solution.guess(mid)
34-
if cmp == 0:
35-
return mid
36-
elif cmp == 1:
37-
left = mid + 1
38-
else:
39-
right = mid - 1
40-
return -1
41-
42-
""" Below function is not real guess """
43-
44-
def guess(self, num):
45-
return 1
23+
def guessNumber(n):
24+
left = 0
25+
right = n
26+
while left <= right:
27+
mid = (left + right) // 2
28+
""" wirte as Solution.guess way is in order to defend error tips """
29+
cmp = guess(mid)
30+
31+
if cmp == 0:
32+
return mid
33+
elif cmp == 1:
34+
left = mid + 1
35+
else:
36+
right = mid - 1
37+
return -1
38+
39+
40+
""" Below function is not real guess """
41+
42+
43+
def guess(num):
44+
return 1

‎codes/python/leetcodes/src/main/python/com/hit/basmath/binary_search/_69.py‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,27 @@
1111
1212
Input: 4
1313
Output: 2
14+
1415
Example 2:
1516
1617
Input: 8
1718
Output: 2
18-
Explanation: The square root of 8 is 2.82842..., and since
19-
the decimal part is truncated, 2 is returned.
19+
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
2020
"""
2121

2222

23-
class Solution(object):
24-
def mySqrt(self, x):
25-
"""
26-
:type x: int
27-
:rtype: int
28-
"""
29-
if x == 0:
30-
return 0
31-
32-
left = 1
33-
right = x
34-
while True:
35-
mid = left + (right - left) // 2
36-
if mid > x / mid:
37-
right = mid - 1
23+
def mySqrt(x):
24+
if x == 0:
25+
return 0
26+
27+
left = 1
28+
right = x
29+
while True:
30+
mid = left + (right - left) // 2
31+
if mid > x / mid:
32+
right = mid - 1
33+
else:
34+
if mid + 1 > x / (mid + 1):
35+
return mid
3836
else:
39-
if mid + 1 > x / (mid + 1):
40-
return mid
41-
else:
42-
left = mid + 1
37+
left = mid + 1

‎codes/python/leetcodes/src/main/python/com/hit/basmath/binary_search/_704.py‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
44
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
55
6-
76
Example 1:
87
98
Input: nums = [-1,0,3,5,9,12], target = 9
@@ -16,7 +15,6 @@
1615
Output: -1
1716
Explanation: 2 does not exist in nums so return -1
1817
19-
2018
Note:
2119
2220
You may assume that all elements in nums are unique.
@@ -25,12 +23,7 @@
2523
"""
2624

2725

28-
def search(self, nums, target):
29-
"""
30-
:type nums: List[int]
31-
:type target: int
32-
:rtype: int
33-
"""
26+
def search(nums, target):
3427
left = 0
3528
right = len(nums) - 1
3629
while left <= right:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
724. Find Pivot Index
3+
4+
Given an array of integers nums, write a method that returns the "pivot" index of this array.
5+
6+
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
7+
8+
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
9+
10+
Example 1:
11+
12+
Input:
13+
nums = [1, 7, 3, 6, 5, 6]
14+
Output: 3
15+
Explanation:
16+
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
17+
Also, 3 is the first index where this occurs.
18+
19+
Example 2:
20+
21+
Input:
22+
nums = [1, 2, 3]
23+
Output: -1
24+
Explanation:
25+
There is no index that satisfies the conditions in the problem statement.
26+
27+
Note:
28+
29+
The length of nums will be in the range [0, 10000].
30+
Each element nums[i] will be an integer in the range [-1000, 1000].
31+
"""
32+
33+
34+
class Solution(object):
35+
def pivotIndex(self, nums):
36+
"""
37+
:type nums: List[int]
38+
:rtype: int
39+
"""
40+
if len(nums) == 0:
41+
return -1
42+
43+
i = 0
44+
total = 0
45+
while i < len(nums):
46+
total += nums[i]
47+
i += 1
48+
49+
j = 0
50+
tempSum = 0
51+
while j < len(nums):
52+
if tempSum * 2 == total - nums[j]:
53+
return j
54+
tempSum += nums[j]
55+
j += 1
56+
57+
return -1

0 commit comments

Comments
(0)

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