![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 61
xz1308579340
这个作者很懒,什么都没留下…
展开
-
leetcode(455). Majority Element 贪心法分饼干
Assign CookiesAssume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content wi.原创 2021-09-29 20:30:13 · 165 阅读 · 0 评论 -
leetcode(169). Majority Element
寻找数组中占比超过一半的数,要求空间复杂度0(1)题目Given an array nums of size n, return the majority element.The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.Example 1:Input: nums =原创 2021-09-22 23:10:00 · 100 阅读 · 0 评论 -
leetcode(50):最大回文子串
题目定义回文,就是一个字符串正着念和反着念都一样,比如“abcba”,比如“上海自来水来自还上”,比如“明天到操场….”。对于这道算法题研究的英文字符串来说,有奇数回文和偶数回文两种情况。奇数回文是指回文字符串是奇数个,比如abcba;偶数回文是指回文字符串是偶数个,比如abba。在设计算法的时候应该把这两种情况都考虑在内。Given a string s, find the longes...原创 2019-04-08 22:00:16 · 134 阅读 · 0 评论 -
leetcode(11):Container With Most Water
题目Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two...原创 2019-04-08 22:43:54 · 150 阅读 · 0 评论 -
leetcode(16): 3Sum Closest
题目Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input wou...原创 2019-04-09 00:35:23 · 126 阅读 · 0 评论 -
leetcode(101): Symmetric Tree
文章目录题目解题思路:python代码一python代码2题目Easy!题目描述:给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。1/ 2 2/ \ / 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:1/ 2 2\ 3 3说明:如果你可以运用递...原创 2019-04-20 21:54:13 · 147 阅读 · 0 评论 -
leetcode(17): Letter Combinations of a Phone Number
题目Letter Combinations of a Phone NumberMediumGiven a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to l...原创 2019-04-09 23:52:34 · 131 阅读 · 0 评论 -
leetcode(19):Remove Nth Node From End of List
题目Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, t...原创 2019-04-10 00:20:46 · 122 阅读 · 0 评论 -
leetcode(121): Best Time to Buy and Sell Stock
题目原题:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。示例 1:输入: [7,1,5,3,6,4]输出: 5解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。...原创 2019-04-13 22:36:31 · 159 阅读 · 0 评论 -
leetcode(287):Find the Duplicate Number
文章目录1.题目2.1二分法思路2.2指针法1.题目给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。示例 1:输入: [1,3,4,2,2]输出: 212示例 2:输入: [3,1,3,4,2]输出: 312说明:不能更改原数组(假设数组是只读的)...原创 2019-04-20 15:19:06 · 122 阅读 · 0 评论 -
二分法
二分法看起来很简单,但是要注意很多边界问题,一不留神,代码就会报错1.二分查找,找到了返回位置,没找到返回-1def binary_search(nums, target) : low = 0 high = length = len(nums)-1 while 0<=low <= high<= length: #防止溢出 ...原创 2019-07-12 23:30:50 · 259 阅读 · 0 评论 -
207. Course Schedule
在网上看到一个很惊艳的解法,里面有很多小checkclass Solution(object): def canFinish(self, n, pres): from collections import deque ind = [[] for _ in xrange(n)] # indegree #关系举证 oud = [0] *...原创 2019-07-17 23:54:12 · 100 阅读 · 0 评论 -
200岛屿的个数
用深度优先搜索做def numIslands(self, grid): if not grid: return 0 count = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == '1': ...原创 2019-07-18 00:05:08 · 104 阅读 · 0 评论 -
leetcode 105 106
文章目录前序中序 重构二叉树 105python 我的代码别人的代码后序中序 重构二叉树 106前序中序 重构二叉树 105python 我的代码# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# ...原创 2019-08-19 10:42:24 · 163 阅读 · 0 评论 -
329. Longest Increasing Path in a Matrix
题目描述题目大意题目大意求二维矩阵中最长的递增路径。思路和417. Pacific Atlantic Water Flow非常类似,直接DFS求解。一般来说DFS需要有固定的起点,但是对于这个题,二维矩阵中的每个位置都算作起点。把每个位置都当做起点,然后去做个dfs,看最长路径是多少。然后再找出全局的最长路径。使用动态规划dp保存已经访问过的位置,这样能节省了很多搜索的过程,然后有个c...原创 2019-08-22 21:16:17 · 213 阅读 · 0 评论 -
leetcode(102): 二叉树按层遍历 Binary Tree Level Order Traversal
题目二叉树的层次遍历(广度优先搜索)20/ 12 22/ / 3 21 25/ 33也就是说这样一棵树遍历的结果为:第一种结果:[[33],[3, 21, 25],[12, 22],[20]]第二种结果:[[20],[12, 22],[3, 21, 25],[33]]第二种结果是从根按照树的高度(准确的说是按照节点的深度)遍历,得到...原创 2019-04-20 22:40:42 · 142 阅读 · 0 评论 -
leetcode(162):寻找峰值 Find Peak Element
题目峰值元素是指其值大于左右相邻值的元素。给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。你可以假设 nums[-1] = nums[n] = -∞。示例:示例1:输入: nums = [1,2,3,1]输出: 2解释: 3 是峰值元素,你的函数应该返回...原创 2019-04-22 21:47:29 · 254 阅读 · 0 评论 -
leetcode(50): Pow(x, n)
题目Implement pow(x, n).Example 1:Input: 2.00000, 10Output: 1024.00000Example 2:Input: 2.10000, 3Output: 9.26100double myPow(double x, int n) {}根据题目再加上给出的函数的返回参数和参数 我的理解是是要我们实现一个myPow函数,这个函数有...原创 2019-04-08 15:23:15 · 233 阅读 · 0 评论 -
leetcode(21): Merge Two Sorted Lists
题目翻译:合并2个已经排序的链表,并且返回一个新的链表。这个新的链表应该由前面提到的2个链表的节点所组成。Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.E...原创 2019-04-07 22:31:36 · 236 阅读 · 0 评论 -
leetcode(20):Valid Parentheses
题目给定一个字符串,只包含’(‘, ‘)’, ‘{‘, ‘}’, ‘[’ 和’]’这些字符,检查它是否是“有效”的。括号必须以正确的顺序关闭,例如”()” 和”()[]{}”都是有效的,”(]” 和”([)]”是无效的。分析:本题考查的是栈结构,具有后进先出的特性。有效包含2个方面,第一个是如果是关闭的括号,前一位一定要刚好有一个开启的括号;第二个是最终结果,需要把所有开启的括号都抵消完...原创 2019-04-07 22:23:21 · 182 阅读 · 0 评论 -
8-String to Integer(atoi) C++
8-String to Integer(atoi) C++Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and a...原创 2019-02-20 22:27:12 · 244 阅读 · 0 评论 -
leetcode(98):判断是否为搜索二叉树
题目Validate Binary Search TreeGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys ...原创 2019-03-24 21:53:33 · 362 阅读 · 0 评论 -
leetcode:x新型二叉树的后序节点
原创 2019-03-24 22:25:48 · 124 阅读 · 0 评论 -
leetcode(200):Number of Islands
链接:https://leetcode.com/problems/number-of-islands/题目Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting ad...原创 2019-03-30 21:48:21 · 141 阅读 · 0 评论 -
leetcode(695): Max Area of Island
链接https://leetcode.com/problems/max-area-of-island/题目Given a non-empty 2D array grid of 0’s and 1’s, an island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertic...原创 2019-03-30 22:01:24 · 165 阅读 · 0 评论 -
leetcode(62): Unique Paths
题目一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。问总共有多少条不同的路径?例如,上图是一个7 x 3 的网格。有多少可能的路径?说明:m 和 n 的值均不超过 100。示例 1:输入: m = 3, n = 2输出: 3解释:从左上角开...原创 2019-04-04 16:16:44 · 259 阅读 · 0 评论 -
leetcode(88):;两个数组排序合并
题目Merge Sorted ArrayEasyGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:The number of elements initialized in nums1 and nums2 are m and n respec...原创 2019-03-27 00:01:11 · 148 阅读 · 0 评论 -
leetcode(102):二叉树按层遍历
题目Binary Tree Level Order TraversalMediumGiven a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example:Given binary tree [3,9,...原创 2019-03-24 14:05:47 · 129 阅读 · 0 评论 -
leetcode110:判断是否为平衡二叉树
题目Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees o...原创 2019-03-24 15:33:07 · 147 阅读 · 0 评论 -
leetcode(75):荷兰国旗问题
题目Sort ColorsMediumGiven an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.H...原创 2019-03-27 18:49:05 · 194 阅读 · 0 评论 -
leetcode(74):Search a 2D Matrix
题目Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each ro...原创 2019-03-27 19:20:47 · 115 阅读 · 0 评论 -
leetcode(581):Shortest Unsorted Continuous Subarray
题目最短需要排序的子序列Shortest Unsorted Continuous SubarrayEasyGiven an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array...原创 2019-03-27 20:12:40 · 127 阅读 · 0 评论 -
leetcode(164):Maximum Gap
题目Maximum GapHardGiven an unsorted array, find the maximum difference between the successive elements in its sorted form.Return 0 if the array contains less than 2 elements.Example 1:Input: [3...原创 2019-03-27 23:27:10 · 115 阅读 · 0 评论 -
leetcode(53):Maximum Subarray
题目Maximum SubarrayEasyGiven an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5...原创 2019-03-27 23:51:10 · 185 阅读 · 0 评论 -
leetcode(1): Two Sum
题目Two SumEasyGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may...原创 2019-04-07 16:24:39 · 85 阅读 · 0 评论 -
leetcode(15):Three sum
https://www.cnblogs.com/wangkundentisy/p/9079622.html原创 2019-04-07 22:18:14 · 116 阅读 · 0 评论 -
1. Two Sum
1. Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use ...原创 2019-02-20 22:03:38 · 109 阅读 · 0 评论