随笔分类 - leetcode
摘要:这道题为简单题 题目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. Yo
阅读全文
摘要:这是一道我觉得有点不是很好的简单题 题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: You may assume that nums1 has
阅读全文
摘要:这道题为简单题 题目: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may ass
阅读全文
摘要:这道题为简单题 题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example, Given n
阅读全文
摘要:这道题为简单题 题目: Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums
阅读全文
摘要:这道题为简单题 题目: Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twic
阅读全文
摘要:这道题为简单题,但是做的过程中忽略了一些特殊情况,最后还是做出来了,怪说不得这道题的通过率才21%左右 题目: 思路: 我设置了两个变量,prev代表上一个元素,prev_prev代表上上个元素,代码的大致思路就是如果有两次(因为我把两个值设置为无穷小,所以第一次num就是自动加1,但是这一次不算)
阅读全文
摘要:这道题为简单题 题目: 思路: 设置三个变量,max_long 存储总的最大长度,l代表该值目前的最大长度,num主要用来比较列表的前后大小。如果i大于num那么l就一直加1.否则l就和max_long比较大小,l变为1,num更新为i值 代码:
阅读全文
摘要:这道题为简单题 题目: 思路: 1、这道题主要搞清楚两种情况就行了: (1)、如果‘A’出现两次 (2)、如果出现‘LLL’ 以上两种情况出现任意一种就返回False 代码: 我的代码: 简介代码:
阅读全文
摘要:这道题属于简单题 题目: 思路: 1、我的思路比较不好,弄出来的代码不够简洁。我采用栈的形式,遍历整个列表,遇到‘ ’或者长度达到了最大(长度这个条件是为了防止示例中没有出现空格,只有一个单词)就把a[]当中的元素全部加到b[]中并添加一个‘ ’,最后去掉b的最后一个元素,再返回‘’。join(b)
阅读全文
摘要:这道题为简单题,网上通过率接近70%,我就不怎么仔细讲了 题目: 思路: 题目太简单,我也没怎么多想,直接暴力解决,然后就导致时间运行太慢 = = 代码: 1、这是我的写的,很菜 2、大神的简洁代码:
阅读全文
摘要:这道题是简单题 题目: 思路: 1、我的思路:for循环1到n的自然数,nums列表删除i,如果捕获到异常,就在后面nums后面添加i,最后返回nums。但是这样做复杂度太高,程序运行会超时,所以需要改进 2、另外加入一个列表b,循环nums列表对应b[i] + 1,最后遍历b列表,返回b[i]等于
阅读全文
摘要:这道题为简单题: 题目: 思路: 最开始我以为就只是把该节点的左右孩子的节点值做绝对值就行了,结果是它的所有左右子树的值做绝对值,利用递归就行了 代码:
阅读全文
摘要:这道题为简单题 题目: 思路: 利用递归。把大问题化小,算每个节点的的左右子树的深度并得到该节点的最大长度self.sum,然后返回该节点左右子树中的最大深度加1 代码:
阅读全文
摘要:这道题为简单题 题目: 思路: 1、我的思路:首先利用广搜,保存每个节点值在列表中,然后两个for循环列表,使对应列表中的每个值变为题目要求的值,然后再将广搜的列表与节点值列表对应。 这种方法复杂,时间空间复杂度高,运行时还出现超时 2、大神的思路:利用递归。其实我最开始也是想用递归,但是没有找到转
阅读全文
摘要:这是一道简单题 题目: 思路: 本来最开始我想用BFS但是感觉那样有点复杂,后来就用了递归的方法,把他们转换为每个节点的左右节点都交换 代码:
阅读全文
摘要:这个题是简单题 题目: 思路: 利用广搜(BFS)。定义一个列表a(存放节点),定义变量b(存放最小值)并赋值根节点值,定义变量c(存放第二小值)并赋值-1,遍历每个节点,如果其节点值大于b,而且如果c 1那么c = 该节点值,否则如果该节点值小于c,那么c = 该节点值 代码:
阅读全文
摘要:这道题为简单题 题目: 思路: 利用广搜,对每个节点进行判断,是否它的左儿子存在,并且它左儿子的左右儿子都不存在,那么total就加上它左儿子的值 代码:
阅读全文
摘要:这道题为简单题 题目: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally
阅读全文
摘要:这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the
阅读全文

浙公网安备 33010602011771号