
LeetCode
文章平均质量分 64
toward_south
自律给人自由
展开
-
计算几何-扫描线算法
计算几何中,扫描线算法(Sweep Line Algorithm)或平面扫描算法(Plane Sweep Algorithm)是一种算法模式,虚拟扫描线或扫描面来解决欧几里德空间中的各种问题,一般被用来解决图形面积,周长等问题,是计算几何中的关键技术之一。这种算法背后的想法是想象一条线(通常是一条垂直线)在平面上扫过或移动,在某些点停止。几何操作仅限于几何对象,无论何时停止,它们都与扫描线相交或紧邻扫描线,并且一旦线穿过所有对象,就可以获得完整的解。原创 2024-05-19 17:51:57 · 954 阅读 · 0 评论 -
LeetCode - Reverse Nodes in k-Group
/** * 题目 * * 将给出的链表中的节点每k个一组翻转,返回翻转后的链表 * 如果链表中的节点数不是k的倍数,将最后剩下的节点保持原样 * 你不能更改节点中的值,只能更改节点本身。 * 只允许使用常数级的空间 * 例如: * 给定的链表是1->2->3->4->5 * * 对于 k = 2, 你应该返回 2->1->4->3-&...原创 2019-11-06 10:21:50 · 189 阅读 · 0 评论 -
LeetCode - Remove Duplicates from Sorted List
/** * 题目 * * 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 */public class RemoveDuplicatesfromSortedList { /** * 常规迭代解法 * @param head * @return */ public ListNode deleteDuplicate...原创 2019-11-06 10:17:39 · 185 阅读 · 0 评论 -
LeetCode - Two Sum
/** * 题目 * * 给定一个整数数组 nums和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。 * * 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 */public class TwoSum { public static int[] twoSum(int[] nums, int...原创 2019-11-05 16:07:22 · 123 阅读 · 0 评论 -
LeetCode - Rotate List
/** * 旋转链表 * * 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。 */public class RotateList { public ListNode rotateRight(ListNode head, int k) { if(head == null || head.next == null){ ...原创 2019-11-05 14:01:14 · 146 阅读 · 0 评论 -
LeetCode - set-matrix-zeroes
题目:Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m...原创 2019-04-29 21:21:00 · 139 阅读 · 0 评论 -
LeetCode - climbing-stairs
题目:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?题意:你正在爬楼梯。到达山顶需要n步。每...原创 2019-05-03 18:17:13 · 181 阅读 · 0 评论 -
LeetCode - sqrtx
题目:Implementint sqrt(int x).Compute and return the square root ofx.题意:求他的平方根解题思路:第一种,调用Math.sqrt()API第二种,类似于折半查找,通过比较 mid *mid 是否等于 x ,和折半思想差不多(mid 需要设为long类型,防止数据泄露)第三种,使用牛顿法来解决,...原创 2019-05-04 11:03:39 · 143 阅读 · 0 评论 -
LeetCode - plus-one
题目:Given a number represented as an array of digits, plus one to the number.题意:翻译过来的题意比较难理解,我意译下,就是给一个数组[1,2,3,4],然后在这数组上加1 就变成了[1,2,3,5] 。这里9 到10是需要向前进位的,如[1,2,3,9] 变为[1,2,4,0] 。大概是这个意思...原创 2019-05-04 11:38:47 · 167 阅读 · 0 评论 -
LeetCode - valid-number
题目:Validate if a given string is numeric.Some examples:"0"=>true" 0.1 "=>true"abc"=>false"1 a"=>false"2e10"=>trueNote:It is intended for the problem statement to be ambiguou...原创 2019-05-04 12:29:00 · 157 阅读 · 0 评论 -
LeetCode -largest-rectangle-in-histogram
题目:Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of...原创 2019-04-19 16:57:59 · 144 阅读 · 0 评论 -
LeetCode - maximal-rectangle
题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.题意:给定一个包含0和1的二维二进制矩阵,找出包含所有1的最大矩形并返回其面积。解题思路:当时我是这么想的直接双重循环,找到等于1的数...原创 2019-04-20 10:40:03 · 197 阅读 · 0 评论 -
LeetCode - simplify-path
题目:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consi...原创 2019-05-03 17:37:40 · 150 阅读 · 0 评论 -
LeetCode - partition-list
题目:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each...原创 2019-04-19 10:28:55 · 125 阅读 · 0 评论 -
LeetCode - remove-duplicates-from-sorted-array
题目:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place w...原创 2019-04-23 20:35:13 · 190 阅读 · 0 评论 -
LeetCode - edit-distance
题目:Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a wo...原创 2019-05-02 17:48:20 · 121 阅读 · 0 评论 -
LeetCode - search-a-2d-matrix
题目:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right. The first integer of eac...原创 2019-04-27 21:36:42 · 143 阅读 · 0 评论 -
LeetCode - scramble-string
题目:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ g...原创 2019-04-18 11:11:56 · 166 阅读 · 0 评论 -
LeetCode - merge-sorted-array
题目:Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in...原创 2019-04-17 21:10:04 · 155 阅读 · 0 评论 -
LeetCode - decode-ways
题目:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the...原创 2019-04-17 20:37:34 · 126 阅读 · 0 评论 -
LeetCode - search-in-rotated-sorted-array
题目:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return it...原创 2019-04-22 12:19:03 · 148 阅读 · 0 评论 -
LeetCode - remove-duplicates-from-sorted-list
题目:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3....原创 2019-04-20 11:16:23 · 192 阅读 · 0 评论 -
LeetCode-remove-duplicates-from-sorted-array-ii
题目:Follow up for "Remove Duplicates":What if duplicates are allowed at mosttwice?For example,Given sorted array A =[1,1,1,2,2,3],Your function should return length =5, and A is now[1,1,2,2,3]...原创 2019-04-24 21:50:16 · 164 阅读 · 0 评论 -
LeetCode - longest-palindromic-substring
题目找出给出的字符串S中最长的回文子串。假设S的最大长度为1000,并且只存在唯一解。解题思路利用左右两个指针,从中间往左右走,有点像那个滑动窗口这个概念。这里需要注意两个例子,一个是两个字符相同的情况aa和3个字符2个相同的情况abb需要用index来进行判断,不能用right指针来判断,否则无论如何都会略过一个test,复杂度为0(n*n),空间上为O(1)代码...原创 2019-10-09 22:01:53 · 126 阅读 · 0 评论 -
LeetCode - merge-intervals
题目:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].题意:给定一组区间,合并所有重叠的区间。解题思路:这题和LeetCode - in...原创 2019-05-13 12:27:37 · 185 阅读 · 0 评论 -
LeetCode - insert-interval
题目:Given a set ofnon-overlappingintervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times....原创 2019-05-13 11:24:52 · 223 阅读 · 0 评论 -
LeetCode - length-of-last-word
题目:Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is d...原创 2019-05-13 09:35:38 · 192 阅读 · 0 评论 -
LeetCode - spiral-matrix-ii
题目:Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ],...原创 2019-05-12 10:01:01 · 135 阅读 · 0 评论 -
LeetCode - permutation-sequence
题目:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123" "132" "213" "231"...原创 2019-05-11 17:41:58 · 117 阅读 · 0 评论 -
LeetCode - minimum-path-sum
题目:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right...原创 2019-05-07 12:18:21 · 137 阅读 · 0 评论 -
LeetCode - jump-game
题目:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine ...原创 2019-05-14 11:01:21 · 198 阅读 · 0 评论 -
LeetCode - rotate-list
题目:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.题意:...原创 2019-05-09 10:56:47 · 146 阅读 · 0 评论 -
LeetCode - merge-two-sorted-lists
题目: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.题意:合并两个已排序的链表,并将其作为一个新列表返回。新列表应该通过将前两个列表的节点拼接在一...原创 2019-05-06 12:36:22 · 148 阅读 · 0 评论 -
LeetCode - combinations
题目:Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4]...原创 2019-04-25 12:13:47 · 234 阅读 · 0 评论 -
LeetCode -unique-paths-ii
题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid....原创 2019-05-08 12:08:52 · 181 阅读 · 0 评论 -
LeetCode - unique-paths
题目:A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach t...原创 2019-05-08 11:31:15 · 199 阅读 · 0 评论 -
LeetCode - word-search
题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertica...原创 2019-04-25 10:37:02 · 142 阅读 · 0 评论 -
LeetCode - add-binary
题目:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".题意:给定两个二进制字符串,返回它们的和(也是一个二进制字符串)。解题思路:从末尾开始比较,也就是从低位开始,一直相加,这里需要维护一个进位这...原创 2019-05-05 12:03:43 · 126 阅读 · 0 评论 -
LeetCode - search-in-rotated-sorted-array-ii
题目:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the...原创 2019-04-22 12:02:30 · 145 阅读 · 0 评论 -
LeetCode - gray-code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gr...原创 2019-04-14 11:49:39 · 137 阅读 · 0 评论