赞
踩
问题描述:给定一个整数数组 nums
和一个目标值 target
,找出数组中和为目标值的两个数的下标。
解题思路:使用哈希表记录每个值及其索引,遍历数组时查找目标值与当前值的差是否在哈希表中。
Python代码示例:
def twoSum(nums, target):
hashmap = {}
for i, num in enumerate(nums):
complement = target - num
if complement in hashmap:
return [hashmap[complement], i]
hashmap[num] = i
return []
问题描述:给定一个数组,它的第 i
个元素是一支给定股票第 i
天的价格,设计一个算法来找到最大的可能利润,你可以尽可能地完成最多两次交易(一次买入和一次卖出)。
解题思路:动态规划,定义四个变量分别记录第一次买入、第一次卖出、第二次买入和第二次卖出的最大利润。
Python代码示例:
def maxProfit(prices):
buy1, buy2, profit1, profit2 = float('inf'), float('inf'), 0, 0
for price in prices:
buy1 = min(buy1, price)
profit1 = max(profit1, price - buy1)
buy2 = min(buy2, price - profit1)
profit2 = max(profit2, price - buy2)
return profit2
问题描述:给定一个字符串 s
,找出其中没有重复字符的最长子串的长度。
解题思路:滑动窗口,用两个指针分别表示窗口的左右边界,维护一个哈希集合记录窗口内的字符,窗口右移时检查字符是否重复,重复则左指针右移。
Python代码示例:
def lengthOfLongestSubstring(s):
char_set = set()
left, right, max_len = 0, 0, 0
while right < len(s):
if s[right] not in char_set:
char_set.add(s[right])
max_len = max(max_len, right - left + 1)
right += 1
else:
char_set.remove(s[left])
left += 1
return max_len
问题描述:给定一个由 ‘1’(陆地)和 ‘0’(水)组成的二维网格地图,计算岛屿的数量。
解题思路:深度优先搜索,遍历地图,遇到 ‘1’ 则进行 DFS 并标记为 ‘0’ 避免重复计数。
Python代码示例:
def numIslands(grid): def dfs(i, j): if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1': return grid[i][j] = '0' dfs(i + 1, j) dfs(i - 1, j) dfs(i, j + 1) dfs(i, j - 1) count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == '1': dfs(i, j) count += 1 return count
问题描述:编写一个函数来找出字符串数组中的最长公共前缀。
解题思路:水平扫描,比较每个字符串的相同位置的字符,直到遇到不匹配或遍历完所有字符串的最短长度。
Python代码示例:
def longestCommonPrefix(strs):
if not strs:
return ""
prefix, count = strs[0], len(strs)
for i in range(1, count):
prefix = prefix[:min(len(prefix), len(strs[i]))]
while prefix and strs[i].find(prefix) != 0:
prefix = prefix[:-1]
return prefix
问题描述:给定一个整数 n
,生成所有合法的括号组合,如 n=3
,输出 ["((()))","(()())","(())()","()(())","()()()"]
。
解题思路:回溯法,递归生成所有可能的组合,同时保证左括号数量不超过右括号数量。
Python代码示例:
def generateParenthesis(n): def backtrack(openN, closeN, path, result): if openN == closeN == n: result.append("".join(path)) return if openN < n: path.append("(") backtrack(openN + 1, closeN, path, result) path.pop() if closeN < openN: path.append(")") backtrack(openN, closeN + 1, path, result) path.pop() result = [] backtrack(0, 0, [], result) return result
问题描述:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
解题思路:迭代法,创建哑节点作为新链表的起点,比较两个链表的节点值,将较小值的节点添加到新链表,并移动对应链表的指针。
Python代码示例:
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def mergeTwoLists(l1, l2): dummy = ListNode() tail = dummy while l1 and l2: if l1.val < l2.val: tail.next, l1 = l1, l1.next else: tail.next, l2 = l2, l2.next tail = tail.next tail.next = l1 or l2 return dummy.next
问题描述:给定一个大小为 n
的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋
的元素。
解题思路:摩尔投票法,使用一个候选人变量和计数器,遍历数组,不断更新候选人和计数器。
Python代码示例:
def majorityElement(nums):
candidate, count = 0, 0
for num in nums:
if count == 0:
candidate = num
count += (1 if num == candidate else -1)
return candidate
问题描述:给定一个字符串 s
,找到它的最长回文子串。
解题思路:动态规划,用二维数组 dp
记录字符串中各个子串是否为回文,基于已知状态推导未知状态。
Python代码示例:
def longestPalindrome(s): if not s: return "" n = len(s) dp = [[False]*n for _ in range(n)] max_len = 1 start = 0 for i in range(n): dp[i][i] = True for length in range(2, n+1): for i in range(n-length+1): j = i + length - 1 if s[i] == s[j] and (length == 2 or dp[i+1][j-1]): dp[i][j] = True if length > max_len: max_len = length start = i return s[start:start+max_len]
问题描述:给定一个整数数组 nums
,返回该数组所有可能的子集。
解题思路:回溯法,递归构建子集,对于每个元素都有两种选择:包含或不包含。
Python代码示例:
def subsets(nums):
def backtrack(start, path):
result.append(path[:])
for i in range(start, len(nums)):
path.append(nums[i])
backtrack(i+1, path)
path.pop()
result = []
nums.sort()
backtrack(0, [])
return result
问题描述:给定一个由 ‘1’(陆地)和 ‘0’(水)组成的二维网格地图,计算岛屿的数量。岛屿是由水平或垂直方向相邻的陆地连接而成的,你可以假设网格的四个边缘都被水包围。
解题思路:深度优先搜索(DFS)或广度优先搜索(BFS),遍历网格,遇到陆地就进行深度遍历标记为已访问,计数加一。
Python代码示例 (DFS):
def dfs(grid, i, j): if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != '1': return grid[i][j] = '0' dfs(grid, i + 1, j) dfs(grid, i - 1, j) dfs(grid, i, j + 1) dfs(grid, i, j - 1) def numIslands(grid): count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == '1': dfs(grid, i, j) count += 1 return count
问题描述:给定一个整数数组 nums
,找到一个具有最大和的连续子数组,并返回其最大和。
解题思路:动态规划,用一个变量记录当前最大和以及全局最大和,遍历数组时不断更新这两个值。
Python代码示例:
def maxSubArray(nums):
current_max = global_max = nums[0]
for num in nums[1:]:
current_max = max(num, current_max + num)
global_max = max(global_max, current_max)
return global_max
问题描述:给定一个字符串 s
,找出无重复字符的最长子串长度。
解题思路:滑动窗口,使用两个指针表示窗口的左右边界,同时用集合记录当前窗口内的字符,当遇到重复字符时,左指针向右移动一位,移除重复字符,直到没有重复为止,过程中记录最长无重复子串长度。
Python代码示例:
def lengthOfLongestSubstring(s):
char_set = set()
left, right, max_length = 0, 0, 0
while right < len(s):
if s[right] not in char_set:
char_set.add(s[right])
max_length = max(max_length, right - left + 1)
right += 1
else:
char_set.remove(s[left])
left += 1
return max_length
问题描述:给定一个二叉树的根节点,返回该二叉树的中序遍历结果。
解题思路:递归或迭代,递归方法较为直观,直接按照“左根右”的顺序访问节点。
Python代码示例 (递归):
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorderTraversal(root):
result = []
def dfs(node):
if node:
dfs(node.left)
result.append(node.val)
dfs(node.right)
dfs(root)
return result
问题描述:实现一个最小堆类,包括插入元素、删除最小元素、获取最小元素的操作。
解题思路:使用列表表示堆结构,插入元素后上浮调整,删除元素后下沉调整,保持堆的性质。
Python代码示例:
class MinHeap: def __init__(self): self.heap = [] def push(self, val): self.heap.append(val) self._swim(len(self.heap) - 1) def pop(self): if not self.heap: return None root = self.heap[0] self.heap[0] = self.heap[-1] del self.heap[-1] self._sink(0) return root def top(self): return self.heap[0] if self.heap else None def _swim(self, idx): parent = (idx - 1) // 2 while idx > 0 and self.heap[parent] > self.heap[idx]: self.heap[parent], self.heap[idx] = self.heap[idx], self.heap[parent] idx, parent = parent, (parent - 1) // 2 def _sink(self, idx): child = 2 * idx + 1 while child < len(self.heap): if child + 1 < len(self.heap) and self.heap[child] > self.heap[child + 1]: child += 1 if self.heap[child] >= self.heap[idx]: break self.heap[child], self.heap[idx] = self.heap[idx], self.heap[child] idx, child = child, 2 * child + 1
问题描述:合并两个升序排列的链表为一个单一的升序链表。
解题思路:采用虚拟头节点简化操作,双指针法遍历两个链表,比较节点值大小,依次连接到结果链表中。
Python代码示例:
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def mergeTwoLists(l1, l2): dummy = ListNode() tail = dummy while l1 and l2: if l1.val < l2.val: tail.next, l1 = l1, l1.next else: tail.next, l2 = l2, l2.next tail = tail.next tail.next = l1 or l2 return dummy.next
问题描述:给定一个包含 n 个整数的数组,找出所有和为0的不重复三元组。
解题思路:排序后使用双指针法,固定一个元素,剩下两个元素使用类似两数之和的方法查找。
Python代码示例:
def threeSum(nums): nums.sort() res = [] for i in range(len(nums) - 2): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: s = nums[i] + nums[left] + nums[right] if s < 0: left += 1 elif s > 0: right -= 1 else: res.append([nums[i], nums[left], nums[right]]) while left < right and nums[left] == nums[left + 1]: left += 1 while left < right and nums[right] == nums[right - 1]: right -= 1 left, right = left + 1, right - 1 return res
问题描述:编写一个函数来查找字符串数组中的最长公共前缀。
解题思路:横向扫描,逐个比较字符串数组中每个位置的字符,直到找到不匹配的字符为止。
Python代码示例:
def longestCommonPrefix(strs):
if not strs:
return ""
for i, char in enumerate(zip(*strs)):
if len(set(char)) > 1:
return strs[0][:i]
return min(strs, key=len)
问题描述:给定一个整数 n
,生成所有合法的括号组合。
解题思路:回溯法,递归生成所有可能的组合,同时保证左括号数量不大于右括号数量。
Python代码示例:
def generateParenthesis(n): def backtrack(openN, closeN, path, result): if openN == closeN == n: result.append("".join(path)) return if openN < n: path.append("(") backtrack(openN + 1, closeN, path, result) path.pop() if closeN < openN: path.append(")") backtrack(openN, closeN + 1, path, result) path.pop() result = [] backtrack(0, 0, [], result) return result
问题描述:给定一个二叉树,找到该树中两个指定节点的最近公共祖先。
解题思路:从根节点开始递归,如果当前节点是p或q,则返回当前节点;如果左右子树分别找到了p和q,则返回当前节点;如果只有一边找到了p或q,则继续在另一边找。
Python代码示例:
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def lowestCommonAncestor(root, p, q):
if not root or root == p or root == q:
return root
left = lowestCommonAncestor(root.left, p, q)
right = lowestCommonAncestor(root.right, p, q)
return root if left and right else left or right
黑客&网络安全如何学习
今天只要你给我的文章点赞,我私藏的网安学习资料一样免费共享给你们,来看看有哪些东西。
攻击和防守要学的东西也不少,具体要学的东西我都写在了上面的路线图,如果你能学完它们,你去就业和接私活完全没有问题。
网上虽然也有很多的学习资源,但基本上都残缺不全的,这是我自己录的网安视频教程,上面路线图的每一个知识点,我都有配套的视频讲解。
内容涵盖了网络安全法学习、网络安全运营等保测评、渗透测试基础、漏洞详解、计算机基础知识等,都是网络安全入门必知必会的学习内容。
(都打包成一块的了,不能一一展开,总共300多集)
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取
CSDN大礼包:《黑客&网络安全入门&进阶学习资源包》免费分享
技术文档也是我自己整理的,包括我参加大型网安行动、CTF和挖SRC漏洞的经验和技术要点,电子书也有200多本,由于内容的敏感性,我就不一一展示了。
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取
CSDN大礼包:《黑客&网络安全入门&进阶学习资源包》免费分享
“工欲善其事必先利其器”我为大家总结出了最受欢迎的几十款款黑客工具。涉及范围主要集中在 信息收集、Android黑客工具、自动化工具、网络钓鱼等,感兴趣的同学不容错过。
还有我视频里讲的案例源码和对应的工具包,需要的话也可以拿走。
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取
CSDN大礼包:《黑客&网络安全入门&进阶学习资源包》免费分享
最后就是我这几年整理的网安方面的面试题,如果你是要找网安方面的工作,它们绝对能帮你大忙。
这些题目都是大家在面试深信服、奇安信、腾讯或者其它大厂面试时经常遇到的,如果大家有好的题目或者好的见解欢迎分享。
参考解析:深信服官网、奇安信官网、Freebuf、csdn等
内容特点:条理清晰,含图像化表示更加易懂。
内容概要:包括 内网、操作系统、协议、渗透测试、安服、漏洞、注入、XSS、CSRF、SSRF、文件上传、文件下载、文件包含、XXE、逻辑漏洞、工具、SQLmap、NMAP、BP、MSF…
因篇幅有限,仅展示部分资料,需要点击下方链接即可前往获取
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。