LeetCode
yimi1995
这个作者很懒,什么都没留下…
展开
-
387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Note: You ma原创 2017-11-05 14:47:36 · 229 阅读 · 0 评论 -
经典排序算法(二)--插入排序、希尔排序(Java实现)
插入排序的思想:首先假设索引为0的元素]被正确排序,然后只需要对索引为1-nums.length-1的元素排序,每次将要插入的元素赋值给变量target,然后判断target与已经排好序的元素相比较,如果target小于排好序的元素,说明target要插入到这个元素的前面,那么将此元素向后移动一位,即nums[j] = nums[j-1],将nums[j-1]赋值给nums[j],这样一直比较直到原创 2017-07-09 16:01:11 · 203 阅读 · 0 评论 -
105. Construct Binary Tree from Preorder and Inorder Traversal
给出一棵树的先序和中序遍历(用数组形式给出),让我们构造这棵树。原创 2017-07-08 21:03:58 · 269 阅读 · 0 评论 -
64. Minimum Path Sum--找路径最小和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2017-07-20 16:09:00 · 295 阅读 · 0 评论 -
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原创 2017-07-14 23:07:31 · 857 阅读 · 0 评论 -
628. Maximum Product of Three Numbers--找最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product.Example 1:Input: [1,2,3]Output: 6Example 2:Input: [1,2,3,4]Output: 24Note:原创 2017-07-05 15:48:43 · 253 阅读 · 0 评论 -
624.Maximum Distance in Arrays --找数组中的最大差值
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance betw原创 2017-07-05 11:27:32 · 559 阅读 · 0 评论 -
605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.Gi原创 2017-06-13 17:07:56 · 758 阅读 · 0 评论 -
75. Sort Colors--数组排序
用0,1,2,分别代表红色,白色和蓝色,按照红白蓝的顺序排序,并且相同的颜色要相邻,所以这道题是对一个含有0,1,2三个元素的数组排序即可原创 2017-07-13 15:39:55 · 377 阅读 · 0 评论 -
442. Find All Duplicates in an Array--找到数组中出现两次的数字
因为题目中说明,这些数字1 ≤ a[i] ≤ n (n = size of array),并且一些数字出现2次,其他的出现1次,那么,新建一个数组,把nums中的数字nums[i]放到temp中索引为nums[i]的地方,如果temp[nums[i]]=nums[i],说明nums[i]之前已经出现过,那么就将它加入到res这个列表中,提交结果为60%左右原创 2017-06-23 21:39:19 · 327 阅读 · 0 评论 -
532. K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers原创 2017-06-12 11:29:37 · 255 阅读 · 0 评论 -
101. Symmetric Tree--判断是否为对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2017-06-20 16:26:29 · 331 阅读 · 0 评论 -
42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2017-06-08 15:20:00 · 208 阅读 · 0 评论 -
经典排序算法(一)--冒泡排序、快速排序java实现
冒泡排序(升序)的思想是:每次比较相邻两个元素,如果这两个元素没有按升序排序,即如果第一个元素大于第二个元素,则需要调换两个元素的顺序。经过一轮比较之后,最大的元素排在末尾,这时候只需要再对第0-nums.length-2这些元素进行两两比较即可,一共需要比较nums.length轮,下面的代码用i控制需要比较的轮数,用j表示每一轮需要比较的元素的索引。public static List b原创 2017-07-08 22:19:33 · 259 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array--在旋转数组中找最小值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no原创 2017-07-21 19:53:40 · 285 阅读 · 0 评论 -
371. Sum of Two Integers
因为计算两个数的和又不允许使用+或-操作,可以在二进制下实现这一操作:先用异或计算两个数的二进制相加结果sum,但是不考虑进位情况,然后用与计算得到产生进位的地方,再向左移一位模拟进位carry,(因为两个二进制位包括四种情况,1 1,1 0, 0 0, 0 1,第一种情况相与为1,且正好是产生进位的地方)。然后递归调用函数,直到carry为0,返回sum即可。原创 2017-11-12 19:02:02 · 205 阅读 · 0 评论 -
395. Longest Substring with At Least K Repeating Characters
题目的意思是找最长的子字符串T,在T中每个字母出现的次数都不少于k,返回结果是T的长度。思路:一,用HashMap找到每个字母出现的次数;二,如果每个字母出现的次数都不小于k,那么直接返回s的长度即可,否则,如果一个字母出现次数小于k,那T一定不包含这个字符,那么就可以以出现次数小于k的字母作为分隔符;三,将分割后的子字符串重复步骤一,二。原创 2017-10-28 20:06:43 · 448 阅读 · 0 评论 -
520. Detect Capital
题目大意:给定一个单词,检查大写字母的使用是否正确。我们定义满足以下三种形式即为正确使用大写字母:1.所有的字母都是大写,例如“USA”2.所有的字母都是小写,例如“leetcode”3.只有首字母是大写(如果这个单词不止有一个字母的话),例如“Google”注意:输入的单词非空。如果输入的单词满足上述三个规则,那么返回True,否则返回False。原创 2017-09-18 14:00:18 · 448 阅读 · 0 评论 -
345. Reverse Vowels of a String
交换一个字符串中的元音字母,例如"hello"转换为“holle”,如果有多个元音字母,如“leetcode”,第一个元音字母和最后一个交换,第二个和倒数第二个交换原创 2017-09-25 22:01:48 · 220 阅读 · 0 评论 -
Java中的split函数
在刷到leetcode第657题的时候,看到一种解法是用分割后的字符串数组的长度去判断Robot是否能回到原点,代码如下: moves= " " + moves + " ";return moves.split("L").length==moves.split("R").length && moves.split("U").length == moves.split("D").length原创 2017-08-30 10:25:39 · 666 阅读 · 0 评论 -
14. Longest Common Prefix最长公共字符串前缀
题目:Write a function to find the longest common prefix string amongst an array of strings.大意:写一个函数实现以下功能:在字符串数组中找到最长的公共字符串前缀例如:如果String [ ] strs = {"ant","an", "and", "anut"}那么最长公共字符串前缀“an”解决方法原创 2017-09-06 19:26:22 · 544 阅读 · 2 评论 -
56. Merge Intervals
给定一些间隔的集合,合并所有有重叠的间隔。从题目中给的例子来看,可以发现,如果下一个间隔的start小于当前间隔的end的时候,两个间隔可以合并。题目中给的间隔 是按照间隔的start有序排列的,如果给定的间隔无序并且间隔比较多的话,例如[1,3],[8,10],[2,6],[4,5]......这时需要对间隔按照start进行排序,然后对每个间隔,比较它的end和下一个间隔的start的大小,如果end<下一个间隔的start则将这个新的间隔加入到要返回的结果中,如果不小于,说明两个间隔没有重叠,那么原创 2017-08-15 10:18:56 · 194 阅读 · 0 评论 -
289. Game of Life
根据维基百科的文章:“生命的游戏,也被称为生命,是1970年由英国数学家约翰·霍顿·康威(John Horton Conway)设计的一种细胞自动机。”给定一个有m*n个细胞的单元格,每个细胞有一个初始的状态活着(1)或者死亡(0)。每个细胞与其他8个细胞(水平,垂直,对角线)使用下面四条规则进行交互(取自上述的维基百科文章):1.任意活着的细胞,它周围的细胞活着的少于两个,那么它会死亡,由于人口太少造成的;2.任意活着的细胞,它周围活着的细胞是2-3个的话,它会活到下一代;原创 2017-08-19 16:27:44 · 242 阅读 · 0 评论 -
31. Next Permutation
题目翻译:实现下一个排列,把数组重新排列成字典序中下一个更大的数字排列,如果找不到这样的排列,那么必须把它重新排列成可能的最低顺序(以升序排列),必须在原处替换,不能占用额外内存。下面是一些例子,输入在左边栏,相应的输出在右边栏。原创 2017-08-12 14:28:28 · 657 阅读 · 0 评论 -
15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain d原创 2017-07-27 11:51:37 · 203 阅读 · 0 评论 -
48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?用一个n*n的矩阵代表一个图片,将图片顺时针旋转90度。题目的意思很明显,相当于,如果一个矩阵是1 2 3原创 2017-07-25 11:43:14 · 195 阅读 · 0 评论 -
55. 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 i原创 2017-07-23 15:18:39 · 234 阅读 · 0 评论 -
34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the targ原创 2017-07-22 19:32:58 · 194 阅读 · 0 评论 -
621. Task Scheduler--任务调度
CPU在进行任务调度是,如果设置间隔为n,则相同的任务之间的间隔是n,如果所有的任务不够间隔n次出现,就用idle表示,即让CPU空转。用大写字母从A-Z表示不同的任务,求出完成所有的任务调度的间隔数。原创 2017-06-28 20:33:27 · 1076 阅读 · 0 评论 -
611. Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.Example 1原创 2017-06-19 17:30:08 · 686 阅读 · 0 评论 -
40. Combination Sum II---找出和为target的子数组
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combinati原创 2017-06-19 15:28:02 · 1368 阅读 · 0 评论 -
495. Teemo Attacking
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning tim原创 2017-05-12 15:19:24 · 170 阅读 · 0 评论 -
581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.You need to fin原创 2017-05-15 15:36:25 · 1895 阅读 · 0 评论 -
561. Array Partition I
561. Array Partition IGiven an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi)原创 2017-05-05 15:17:21 · 256 阅读 · 0 评论 -
441. Arranging Coins
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.n is原创 2017-05-24 19:48:58 · 230 阅读 · 0 评论 -
495. Teemo Attacking
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning tim原创 2017-05-12 15:34:35 · 218 阅读 · 0 评论 -
13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.题目就这么简洁,没有然后了..所以,还需要知道由罗马数字转换成阿拉伯数字的转换方法:1.罗马数字的组成主要是7个字母的组合:‘I’代表1,‘V’代表原创 2017-05-23 14:46:42 · 248 阅读 · 0 评论 -
152. Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest原创 2017-05-11 18:55:30 · 215 阅读 · 0 评论 -
566. Reshape the Matrix
先得到nums的行数和列数,判断要reshape的矩阵的r和c与nums的所有元素个数是否相等,若不相等,则返回原来的nums,否则,将nums中的元素按照r行c列分别赋给matrix,代码如下: int a = nums.length; int b = nums[0].length; if(r*c != a*b) return nums;原创 2017-05-10 19:18:32 · 452 阅读 · 0 评论 -
525. Contiguous Array
525. Contiguous ArrayGiven a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.Example 1:Input: [0,1]Output: 2Explanation: [0, 1] is the longe原创 2017-03-25 15:54:07 · 355 阅读 · 0 评论