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 44d0f00

Browse files
authored
Merge pull request #9 from 149ps/LC-problems
Lc problems
2 parents deb93df + 165b66e commit 44d0f00

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1.
3+
4+
Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer.
5+
6+
7+
8+
Example 1:
9+
10+
Input: nums1 = [1,2,3], nums2 = [2,4]
11+
Output: 2
12+
Explanation: The smallest element common to both arrays is 2, so we return 2.
13+
Example 2:
14+
15+
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5]
16+
Output: 2
17+
Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
18+
19+
20+
Constraints:
21+
22+
1 <= nums1.length, nums2.length <= 105
23+
1 <= nums1[i], nums2[j] <= 109
24+
Both nums1 and nums2 are sorted in non-decreasing order.
25+
"""
26+
class Solution:
27+
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
28+
def binary_search(target,nums):
29+
left,right = 0,len(nums)-1
30+
while left <= right:
31+
mid = left + (right- left)//2
32+
if nums[mid] == target: return True
33+
elif nums[mid] > target:
34+
right = mid - 1
35+
else:
36+
left = mid + 1
37+
return False
38+
if len(nums1) > len(nums2):
39+
for num in nums2:
40+
if binary_search(num,nums1):
41+
return num
42+
return -1
43+
else:
44+
for num in nums1:
45+
if binary_search(num,nums2):
46+
return num
47+
return -1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
3+
i, j = 0, 0
4+
while i < len(nums1) and j < len(nums2):
5+
if nums1[i] < nums2[j]:
6+
i += 1
7+
elif nums1[i] > nums2[j]:
8+
j += 1
9+
else:
10+
return nums1[i]
11+
return -1
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def dfs(self, left, right,s):
3+
final = 0
4+
while left >= 0 and right < len(s) and s[left] == s[right]:
5+
left -= 1
6+
right += 1
7+
final += 1
8+
return final
9+
10+
def countSubstrings(self, s: str) -> int:
11+
result = 0
12+
for i in range(len(s)):
13+
result += self.dfs(i,i,s)
14+
result += self.dfs(i,i+1,s)
15+
return result

0 commit comments

Comments
(0)

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