关注
https://www.nowcoder.com/discuss/71505?type=2&order=0&pos=22&page=1 题目来源 头条 一面编程题 翻转二叉树 Leetcode 226. Invert Binary Tree class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root:
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root 最大连续子串和 Leetcode 53. Maximum Subarray class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []: return 0
dp = ret= nums[0]
for i in range(1,len(nums)):
dp = max(dp+nums[i],nums[i])
ret = max(ret,dp)
return ret 二面编程题 给一棵边权树树找到最大路径 可以先考虑不带权值的 Leetcode 543. Diameter of Binary Tree 最大路径实际上可以转化为求叶子节点之间的最长距离 class Solution(object):
def diameterOfBinaryTree(self, root):
self.diameter = 0
self.depth(root)
return self.diameter
def depth(self,root):
if not root: return 0
left = self.depth(root.left)
right = self.depth(root.right)
self.diameter = max(self.diameter,left+right)
return max(left,right)+1
其实带权值和不带权值的区别在于不带权值的树其实权值为1 需要修改的点在 self.diameter = max(self.diameter,left+right+root.lweight+root.rweight)
return max(left+root.lweight,right+root.rweight) 三面编程题 给一个字符串和单词列表,判断字符串能不能由这些单词组成 Leetcode 139 Word Break 思路是用数组dp,dp[i] 表示 字符串 s[:i+1] 能否由单词列表中的单词组成 那么可以得到 dp[i] = dp[j] and (s[j:i+1] in wordlist) for j in j in range(i) class Solution(object):
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
"""
worddict = {}
for word in wordDict:
worddict[word] = True
dp = [True]+[False for i in s]
for i in range(len(s)):
for j in range(i+1):
if s[j:i+1] in worddict and dp[j]==True:
dp[i+1] = True
break
return dp[-1]
股票买卖问题可以参见 https://blog.csdn.net/tinkle181129/article/details/79506010 Leetcode 121. Best Time to Buy and Sell Stock 贪心思想 class Solution(object): def maxProfit(self, prices): if prices == []: return 0 minNum,ret = prices[0],0 for p in prices:
minNum = min(minNum,p)
ret = max(ret,p-minNum) return ret
查看原帖
点赞 1
相关推荐
Louis_含泪拒绝...:整理的好好,拿这个去面试感觉稳过了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 你小心翼翼的闯过多大的祸? #
4011次浏览 68人参与
# 找不到实习会影响秋招吗 #
1399880次浏览 13635人参与
# 实习没事做是福还是祸? #
4362次浏览 68人参与
# 重来一次,你会对开始求职的自己说 #
941次浏览 19人参与
# 2025年终总结 #
134587次浏览 2294人参与
# 考研人,我有话说 #
156616次浏览 1211人参与
# 哪些公司笔/面试难度大? #
7080次浏览 32人参与
# 实习简历求拷打 #
24225次浏览 249人参与
# 你觉得现在还能进互联网吗? #
29964次浏览 201人参与
# 携程工作体验 #
18958次浏览 66人参与
# 大厂VS公务员你怎么选 #
69146次浏览 638人参与
# 扒一扒那些奇葩实习经历 #
140186次浏览 1149人参与
# 找不到好工作选择GAP真的丢人吗 #
93726次浏览 1007人参与
# 那些我实习了才知道的事 #
253135次浏览 1785人参与
# 非技术投递记录 #
672960次浏览 6821人参与
# 机械求职避坑tips #
81094次浏览 531人参与
# 投格力的你,拿到offer了吗? #
154987次浏览 829人参与
# 第一份工作能做外包吗? #
94076次浏览 599人参与
# 作业帮求职进展汇总 #
85506次浏览 559人参与
# 秋招遇到的奇葩面试题 #
101272次浏览 416人参与
