LeetCode
文章平均质量分 68
Wally@Web3.0
Make Web3.0 native app development easier
展开
-
LeetCode Single Number
class Solution {public: int singleNumber(int A[], int n) { sort(A,A+n); for(int i=0;i<n;){ if(A[i]==A[i+1]){ i+=2; }else{ r原创 2014-08-01 22:12:08 · 490 阅读 · 0 评论 -
[LeetCode]Next Permutation
题意: 找出当前数组排列的下一个排列,按升序,如果没有下一个排列就输出最小的排列思路:倒序寻找升序序列直到某个元素不满足逆升序为止,然后交换该元素与其后面比他大的最小元素,最后将后续元素按升序排序即可代码: public void nextPermutation(int[] num) { int j = num.length - 1; int i原创 2015-04-18 09:02:35 · 834 阅读 · 0 评论 -
[LeetCode] Find Minimumin Rotated Sorted Array II
题意:在旋转过的有序数组中找到最小数,数组中可能有重复元素思路:二分,判断是否有相等的元素,主要是二分的时候的一些细节,比如说是有序的还是rotated代码: public int findMin(List nums) {//solution1 O(log(N)) int min = Integer.MAX_VALUE; int l = 0, r原创 2015-04-18 08:54:44 · 944 阅读 · 0 评论 -
[LeetCode]Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integer原创 2015-04-06 10:56:52 · 908 阅读 · 0 评论 -
[LeetCode]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原创 2015-04-06 12:44:11 · 963 阅读 · 0 评论 -
[LeetCode]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?思路1:直接开辟一个数组,然后按照对应关系复制即可,空间和时间复杂度o(N*N)代码1:原创 2015-04-06 13:58:22 · 636 阅读 · 0 评论 -
[LeetCode]Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.in place,主要是感觉比较麻烦,其实就是利用第一行和第一列保存该行列是否需要变0,再用两个变量表示第一行和第一列是否需要变0代码:public class Solution { publi原创 2015-04-06 15:39:19 · 776 阅读 · 0 评论 -
[LeetCode]Plus One
简单题不解释, 维护一个进位即可public class Solution { public int[] plusOne(int[] digits) { int c = 1; for(int i = digits.length - 1; i >=0; i --){ if(c == 0)break; digits[i]原创 2015-04-06 16:00:06 · 821 阅读 · 0 评论 -
[LeetCode]Unique Paths II
该题和之前的题目之间的区别就是某些单元是不可达的,这可以加上判断语句即可算法1:public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { int pathsNum = 0,m = obstacleGrid.length,n=obstacleGrid[0].l原创 2015-04-06 16:43:36 · 403 阅读 · 0 评论 -
[LeetCode]Combination Sum
题意:求一个数组中的组合为某个target的所有子数组组合,要求不重复,思路:先将数组排序,然后按深度遍历的思想对i - > len -1的元素进行遍历代码如下:public class Solution { public List> combinationSum(int[] candidates, int target) { List> results = n原创 2015-04-07 21:47:47 · 722 阅读 · 0 评论 -
[LeetCode]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]原创 2015-04-20 09:14:31 · 693 阅读 · 0 评论 -
[LeetCode]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). Fin原创 2015-04-21 21:20:19 · 616 阅读 · 0 评论 -
[LeetCode]Binary Tree Inorder Traversal
题意:题意比较简单就是二叉树的中序遍历思路:1. 递归是最简单的了 2. 非递归的实现:用一个stack做存储结构一直查找leftchild 知道没有下一个leftchild,访问该元素如果该元素有right child 加入stack从stack中取出一个元素 重复上述行为(这里需要加入set集合记录访问过得left的节点 否则会出现重复访问的问题)代码:原创 2015-05-16 15:41:44 · 709 阅读 · 0 评论 -
[LeetCode]Copy List with Random Pointer
题意:这个题目也是个蛮有意思的题目,就是对一个有随机指针的链表进行深拷贝,思路:简单地来说就是递归拷贝,然后呢防止重复拷贝,所以凡是拷贝过得内存地址都得记录下来代码: Map m = new HashMap(); //保存已经copy的部分 public RandomListNode copyRandomList(RandomListNode head)原创 2015-05-16 15:35:45 · 590 阅读 · 0 评论 -
[LeetCode]Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and w原创 2015-05-16 15:50:45 · 735 阅读 · 0 评论 -
[LeetCode]Anagrams
题意:这个题意理解比较重要,题目的意思就是指找到一个字符串数组中由相同字符构成的字符串组思路:基本思路是这样的,建立一个string - > list 的hash表, key是字符串的字符按序排列的字符串,然后遍历list的size即可代码: public List anagrams(String[] strs) { List rs = new LinkedLis原创 2015-05-16 15:31:55 · 689 阅读 · 0 评论 -
[LeetCode]Longest Consecutive Sequence
题意:在一堆无序元素中找到最长的连续串的长度,要求时间复杂度O(N)思路:首先将元素放到set集合中,然后再判断,每次判断是否包含某元素的复杂度为O(1)代码: public int longestConsecutive(int[] num) { int currLen = 0, longestLen = Integer.MIN_VALUE; Se原创 2015-04-18 08:58:07 · 932 阅读 · 0 评论 -
[LeetCode]First Missing Positive
题意:在乱序数组中找到第一个没出现的正整数思路1: 直接暴力解决,复杂度O(N*N)代码1: public int firstMissingPositive1(int[] A) {// big O(N*N) if(A.length == 0)return 1; int i = 1; while(i <= A.length){原创 2015-04-05 21:28:48 · 520 阅读 · 0 评论 -
[LeetCode] Spiral Matrix
这个题目的意思比较简单,就是螺旋式的输出数组的数据,[[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].我自己的解法比较直接,就是从最外层向内层逐步递归这种方式不需要额外的存储空间,但是边界控制比较繁琐,代码如下: public List spiral原创 2015-03-25 10:08:21 · 795 阅读 · 0 评论 -
[LeetCode]Insert Interval
题意分析:想一堆区间中插入一个新的区间,返回之后的分布情况我的思路是先找到新区间的start 然后在找到他的结束点代码如下,复杂度O(N): public List insert(List intervals, Interval newInterval) { if (newInterval == null) return intervals;原创 2015-03-25 18:34:18 · 670 阅读 · 0 评论 -
[LeetCode]Two Sum
题意:求出某个数组中的两个数值的和等于一个固定的target的该两个数值的下标,按从小到大的顺序.解题思路:1: 直接暴力遍历 复杂度O(n*n) 超时2:先排序,再遍历 增加了空间复杂度,时间复杂度为O(N+N*log(N)+N)3:有人建议用HashMap但是如果有重复数据就应该是不成立的,思路二的解法如下: public int[] twoSum(int[] num原创 2015-03-31 20:07:54 · 690 阅读 · 0 评论 -
[LeetCode]Merge Sorted Array
题意;将A,B连个有序数组合并到A中,A空间充足思路1: 基本思路很简单,开辟一个额外数组,空间复杂度和时间复杂度都为O(N)代码1:public class Solution { public void merge(int A[], int m, int B[], int n) { if(n == 0)return; if(m == 0){原创 2015-04-03 20:54:05 · 512 阅读 · 0 评论 -
[LeetCode]Word Search
题意:在二维字符表中查找目标字符串,不能重复访问单元格,主要是找到入口+深度搜索然后标记访问过得单元即可public class Solution { public boolean exist(char[][] board, String word) { if (word == null || word.length() == 0 || board == nu原创 2015-03-24 10:49:27 · 819 阅读 · 0 评论 -
[LeetCode]Rotate Array
题意:给定一个数组,求该数组向右平移k个位置之后的数组,例如:[1,2,3,4,5,6,7] k = 3 => [5,6,7,1,2,3,4,5,6]思路1:最简单的想法必然是暴力啊,一个个的平移,复杂度O(K*N),代码简洁,也能够通过代码1: public void rotate3(int [] nums, int k){//一次移动一个 if(k ==0 )原创 2015-04-03 18:38:23 · 572 阅读 · 0 评论 -
[LeetCode]3Sum
题意: 找出一个数组内和为0的三元组,元组不能重复思路1:.直接暴力DFS,这样会超时,不过先排序加上剪枝的花据说能够AC思路2:先排序O(N*log(N)),然后遍历a 从0 到 nums.length - 2, 结下来的问题就简化为2Sum的问题了 复杂度O(N*N)代码: public List> threeSum(int[] num) { List>原创 2015-04-01 18:06:01 · 728 阅读 · 0 评论 -
[LeetCode] Search Insert Position
题意: 给出一个target找出他在有序数组中的位置思路1: 直接遍历 复杂度O(N)代码1: public int searchInsert1(int[] A, int target) {//直接遍历 算法O(N) int i = 0; if(target < A[0]) return 0; if(target > A[A.len原创 2015-04-04 10:56:57 · 697 阅读 · 0 评论 -
[LeetCode]Remove Duplicates from Sorted Array2
题意:从有序数组中删除重复数据,但是与题目一有一点区别:可以允许重复一次思路:首先判断是否重复,有的话指针后移,然后按照题1中的思路进行,复杂度O(N)代码: public int removeDuplicates(int[] A) { if(A == null || A.length == 0)return 0; int len = A.leng原创 2015-04-18 09:05:47 · 816 阅读 · 0 评论 -
[LeetCode] Search in Rotated Sorted Array
题意:在旋转的有序数组中搜索某个元素,无重复数据思路:二分搜索,注意有旋转的区间的上下界的判断代码: public int search(int[] A, int target) { return bsearch(A, target, 0, A.length - 1); } /** * 二分搜索,无重复元素,判断是否为rotated原创 2015-04-18 09:09:26 · 828 阅读 · 0 评论 -
[LeetCode]Pascal's Triangle 1 & 2
这两题都比较简单,第一题输出杨辉三角,第二题输出特定的某一行,第二题要求空间复杂度为O(k)代码如下:Pascal's Triangle: public List> generate(int numRows) {//direct simulate List> rs = new LinkedList>(); if(numRows == 0)retur原创 2015-04-15 13:57:51 · 674 阅读 · 0 评论 -
[LeetCode]Unique Paths
题意: https://leetcode.com/problems/unique-paths/A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at a原创 2015-04-05 16:27:48 · 554 阅读 · 0 评论 -
[LeetCode]Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks,原创 2015-04-05 17:09:52 · 696 阅读 · 0 评论 -
[LeetCode]Majority Element
题意: 找出一个数组中的主要元素,主要元素为出现次数大于[n/2]的元, 当然最笨的方法就是暴力,没式不知道时间行不行思路1: 遍历数组,统计每个元素出现的次数,用HashMap统计每个元素出现的次数 时间复杂度O(N),空间复杂度 O(N)代码1:public class Solution { public int majorityElement(int[] num)原创 2015-04-05 15:15:38 · 680 阅读 · 0 评论 -
[LeetCode]Find Minimum in Rotated Sorted Array
题意:从一个平移过的数组中找到最小的元素思路1:最简单的思路莫过于排序然后取第一个元素,复杂度O(N*log(N))代码1:public class Solution { public int findMin(int[] num) { Arrays.sort(num); return num[0]; }}思路2: 遍历,比较两段原创 2015-04-05 15:51:14 · 571 阅读 · 0 评论 -
[LeetCode]Repeated DNA Sequences Total
题意:题目意思很简单就是有一个由 A C G T 组成的字符串,要求找出字符窜中出现次数不止1次的字串思路1: 遍历字符串,用hashmap存储字串,判断即可代码1: public List findRepeatedDnaSequences(String s) { List rs = new LinkedList(); Map map = new原创 2015-05-16 15:28:29 · 641 阅读 · 0 评论