LeetCode T14 T26 T27 T28 T35
class Solution:
def longestCommonPrefix(self, strs):
n = len(strs)
if n==0:
return ""
if n==1:
return strs[0]
n_s = len(strs[0])
count = 0
flag = True
ch = ""
if n_s == 0:
return ""
ch = strs[0][0]
while(flag):
for i in strs:
n_in = len(i)
if n_in==0:
return ""
if n_in -1 >= count:
if i[count] != ch:
flag = False
break
else:
return strs[0][:count]
if flag:
count += 1
if n_s -1 >= count:
ch = strs[0][count]
else:
break
if count != 0:
return strs[0][:count]
else:
return ""
class Solution:
def removeDuplicates(self, nums):
n = len(nums)
if n==0:
return 0
if n == 1:
return 1
temp = nums[0]
count = 1
while(count < len(nums)):
if nums[count] == temp:
nums.pop(count)
else:
temp = nums[count]
count += 1
return len(nums)
class Solution:
def removeElement(self, nums,val):
n = len(nums)
if n==0 or (n == 1 and nums[0] == val):
return 0
if n == 1:
return 1
count = 0
while(count < len(nums)):
if nums[count] == val:
nums.pop(count)
else:
count += 1
return len(nums)
class Solution:
def strStr(self, haystack, needle):
if haystack == needle:
return 0
n_h = len(haystack)
n_n =len(needle)
if n_h<n_n or (n_h == n_n and haystack != needle):
return -1
for i in range(n_h - n_n + 1):
if haystack[i:i+n_n] == needle:
return i
return -1
class Solution:
def searchInsert(self, nums, target):
n = len(nums)
if n == 0:
return 0
if n == 1:
if nums[0] == target:
return 0
if nums[0] > target:
return 0
if nums[0] < target:
return 1
if nums[0] > target:
return 0
for i in range(n-1):
if nums[i] == target:
return i
else:
if nums[i+1]==target:
return i + 1
else:
if nums[i+1]<target:
continue
else:
return i+1
return n
相關文章
- 【T35班班長來稿】高階影片課的學習心得
- Thinkpad T14升級Windows11ver22h2失敗問題解決小記ThinkPadWindows
- 【LeetCode】如何學習LeetCode?LeetCode
- LeetCodeLeetCode
- LeetCode in actionLeetCode
- Leetcode AnagramsLeetCode
- Leetcode SortListLeetCode
- Leetcode ReorderListLeetCode
- leetcode SubsetsLeetCode
- Leetcode PermutationsLeetCode
- [LeetCode刷題筆記] 關於LeetCode的前言LeetCode筆記
- 我的 LeetCodeLeetCode
- Leetcode學習LeetCode
- LeetCode 53 JSLeetCodeJS
- LeetCode 50 JSLeetCodeJS
- LeetCode 克隆圖LeetCode
- Leetcode Perfect SquaresLeetCode
- Leetcode Sort ColorsLeetCode
- Leetcode Sort ArrayLeetCode
- 【LeetCode】Jewels and StonesLeetCode
- 嘗試 LeetcodeLeetCode
- LeetCode 消除遊戲LeetCode遊戲
- Leetcode03LeetCode
- Leetcode LRU CacheLeetCode
- Leetcode Path SumLeetCode
- LeetCode Min StackLeetCode
- Leetcode Word SearchLeetCode
- Leetcode Number of islandsLeetCode
- Leetcode-394LeetCode
- Leetcode 921 JavascriptLeetCodeJavaScript
- golang刷leetcodeGolangLeetCode
- LeetCode:快速排序LeetCode排序
- [LeetCode] Group AnagramLeetCode
- leetcode690LeetCode
- LeetCode 283 jsLeetCodeJS
- Leetcode #463 JavascriptLeetCodeJavaScript
- Leetcode # 495 JavascriptLeetCodeJavaScript
- Leetcode 513 javascriptLeetCodeJavaScript