赞
踩
原创微信公众号:【小鸿星空科技】
版权声明:原创不易,本文禁止抄袭、转载,侵权必究!
主观题:
数据库:
Linux:
计算机网络
数据结构:
测试开发:
操作系统和其他问题:
算法题:
1.输出一个字符串中不重复最长连续子串的长度(比如:输入参数为字符串s = ‘abca’,输出为整型数字3)
#滑动窗口+哈希表
class Solution:
def maxLenSub(self, astring: str) -> int:
Dict, left, max_len = {}, 0, 0
for i, s in enumerate(astring):
if s in Dict:
left = max(left, Dict[s] + 1)
Dict[s] = i
max_len = max(max_len, i - left + 1)
return max_len
#双指针
class Solution:
def mergeSortedArr2(self, arr1: List[int], arr2: List[int]) -> List[int]:
temp_list, i, j = [], 0, 0
while arr1 and arr2:
if arr1[i] < arr2[j]:
temp_list.append(arr1.pop(i))
else:
temp_list.append(arr2.pop(j))
return temp_list + arr1 + arr2
class ListNode: def __init__(self,val=0, next=None): self.val = val self.next = next class Solution: def mergeSortedL2(self, L1: ListNode, L2: ListNode) -> ListNode: if L1 is None: reurn L2 elif L2 is None: return L1 elif L1.val < L2.val: L1.next = self.mergeSortedL2(L1.next, L2) return L1 else: L2.next = self.mergeSortedL2(L1, L2.next) return L2
class Solution:
def maxSumSub(self, arr: List[int]) -> int:
max_sum = arr[0]
pre_sum = 0
for i in arr:
if pre_sum < 0:
pre_sum = i
else:
pre_sum += i
if pre_sum > max_sum:
max_sum = pre_sum
return max_sum
def reverseInt(num): # 整数反转 INT_MIN, INT_MAX = -2**31, 2**31 - 1 total, negative_num = 0, False if num < 0: num = abs(num) negative_num = True while num != 0: if total < INT_MIN // 10 + 1 or total > INT_MAX // 10: return 0 mod = num % 10 num = num // 10 total = total*10 + mod if negative_num is True: total = int('-' + str(total)) return total
class Solution: def quickSort(self, arr: List[int]) -> List[int]: if len(arr) < 2: return arr else: pivot = arr[0] less = [i for i in arr[1:] if i <= pivot] greater = [i for i in arr[1:] if i > pivot] return self.quickSort(less) + [pivot] + self.quickSort(greater) def maopaoSort(self, arr: List[int]) -> List[int]: # while+for循环 times = len(arr) - 1 while times > 0: for i in range(times): if arr[i] > arr[i+1]: arr[i], arr[i+1] = arr[i+1], arr[i] times -= 1 return arr # 两个for循环 # for i in range(1, len(arr)): # for j in range(len(arr) - i): # if arr[j] > arr[j+1]: # arr[j], arr[j+1] = arr[j+1], arr[j] # return arr
class Solution:
def countNodes(self, root):
return 0 is root is None else self.countNodes(root.left) + self.countNodes(root.right) + 1
class Solution(object): """ 螺旋矩阵 """ def spiralOrder(self, matrix): """ :params matrix: 二维数组 """ if matrix is None or len(matrix) == 0: return matrix else: m, n = len(matrix), len(matrix[0]) return self.get_spiralOrder(matrix, 0, m - 1, 0, n - 1) def get_spiralOrder(self, matrix, r_start, r_end, c_start, c_end): if r_start > r_end or c_start > c_end: return [] elif r_start == r_end: return matrix[r_start][c_start:c_end + 1] elif c_start == c_end: return [matrix[i][c_end] for i in range(r_start, r_end + 1)] else: curr = matrix[r_start][c_start:c_end + 1] + [matrix[i][c_end] for i in range(r_start + 1, r_end)] + \ matrix[r_end][c_start:c_end + 1][::-1] + [matrix[i][c_start] for i in reversed(range(r_start + 1, r_end))] res = curr + self.get_spiralOrder(matrix, r_start + 1, r_end - 1, c_start + 1, c_end - 1) return res
class Solution:
def reverse(self, head: ListNode):
pre = None
curr = head
while curr is not None:
curr.next = pre
pre = curr
curr = curr.next
return pre
class Node: def __init__(self, val=None, children=None): self.val = val self.children = children # N叉树的前序遍历 def preOrder(self, root: Node): ans = [] def dfs(node: Node): if node is None: return node ans.append(node.val) for ch in node.children: dfs(ch) dfs(root) return ans # N叉树的后序遍历 def postOrder(self, root: Node): ans = [] def dfs(node: Node): if node is Node: return node for ch in node.children: dfs(ch) ans.append(node.val) dfs(root) return ans
class Solution: def longestValidParentheses(self, s: str) -> int: if not s: return 0 res = [] stack = [] for i in range(len(s)): if stack and s[i] == ")": res.append(stack.pop()) res.append(i) if s[i] == "(": stack.append(i) res.sort() #print(res) i = 0 ans = 0 n = len(res) while i < n: j = i while j < n - 1 and res[j + 1] == res[j] + 1: j += 1 ans = max(ans, j - i + 1) i = j + 1 return ans
class Solution:
def stringCount1(self, astring: str) -> dict:
return {i:astring.count(i) for i in astring}
def stringCount2(self, astring: str) -> dict:
Dict = {}
for s in astring:
Dict[s] = Dict[s] + 1 if s in Dict else 1
return Dict
class Solution: def swapOfArr(self, arr): def Biggest(arr): biggest = arr[0] biggest_index = 0 for i in range(len(arr)): if arr[i] > biggest: biggest = arr[i] biggest_index = i return biggest_index def Smallest(arr): smallest = arr[0] smallest_index = 0 for i in range(len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index biggest_index = Biggest(arr) smallest_index = Smallest(arr) arr[biggest_index], arr[smallest_index] = arr[smallest_index], arr[biggest_index] return arr
class Queue:
# 两个栈实现一个队列
def __init__(self):
self.A, self.B = [], []
def appendTail(self, value: int) -> None:
self.A.append(value)
def deleteHead(self) -> int:
if self.B: return self.B.pop()
if not self.A: return -1
while self.A:
self.B.append(self.A.pop())
return self.B.pop()
class Solution:
# 最长递增子数组长度
def lengthOfLIS(self, nums: list) -> int:
if not nums:
return 0
dp = []
for i in range(len(nums)):
dp.append(1)
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
class Solution:
def reverseStr1(self, s: str, k: int) -> str:
str_arr = [i for i in s]
for i in range(0, len(str_arr), 2*k):
str_arr[i:i+k] = str_arr[i:i+k][::-1]
return ''.join(str_arr)
def reverseStr2(self, s: str, k: int) -> str:
i, newStr = 0, ''
while i < len(s):
newStr += s[i:i+k][::-1] + s[i+k:i+2*k]
i += 2*k
return newStr
主观题:
数据库:
计算机网络:
数据结构:
操作系统和其他问题:
算法题:
作者:小鸿的摸鱼日常,Goal:让编程更有趣!
原创微信公众号:『小鸿星空科技』,专注于校招(内推/笔经/面经)、算法、爬虫,网站,游戏开发,数据分析、自然语言处理,AI等,期待你的关注,让我们一起成长、一起Coding!
版权说明:本文禁止抄袭、转载 ,侵权必究!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。