Leetcode
文章平均质量分 52
whuissyxa
这个作者很懒,什么都没留下…
展开
-
Cointainer 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 linei is at (i, ai) and(i,原创 2017-09-16 15:51:38 · 235 阅读 · 0 评论 -
Pow(x,n)
原题:Implement pow(x, n).应该是不调用Math.Pow()方法实现Math.Pow()。思考过程:让我想起以前做的用减法实现除法的方法。这次以乘法实现乘方为了不超时,乘数应该是以指数形式增长的。一开始想到了一个办法自觉得应该没问题,结果还是超时了:public double myPowTLE(double x, int n) { do原创 2017-10-16 14:14:46 · 140 阅读 · 0 评论 -
Group Anagrams
原题:Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note:原创 2017-10-16 13:04:24 · 190 阅读 · 0 评论 -
698. Partition to K Equal Sum Subsets
原题:Discuss Pick OneGiven an array of integers nums and a positive integer k, find whether it's possible to divide this array intok non-empty subsets whose sums are all equal.Example 1:原创 2017-11-01 15:23:30 · 707 阅读 · 0 评论 -
307. Range Sum Query - Mutable
原题:Given an integer array nums, find the sum of the elements between indicesi and j (i ≤ j), inclusive.The update(i, val) function modifies nums by updating the element at indexi to val.Ex原创 2017-11-01 09:24:09 · 136 阅读 · 0 评论 -
代理模式
个人笔记,请不要被误导。代理模式:为另一个对象提供一个替身或占位符以控制对这个对象的访问。(被代理的对象可以是远程的对象、创建开销大的对象或需要安全控制的对象,下面的三个例子远程代理、虚拟代理和保护代理分别代表这三类)。实体和代理者实现同一个接口,用户用的时候不用关心究竟是谁,实现用户和实体的解耦。一般代理者都有实体的引用,这样才能调用它。这和适配器模式很相似区别在于前者代理者和实体实现同原创 2017-10-31 20:44:04 · 286 阅读 · 0 评论 -
382. Linked List Random Node
原题:即给定不知长度的链表,调用getRandom()函数会返回一个随机节点的值。解题思路&思考过程:一开始想到的是硬解,后来了解到有一个蓄水池抽样算法:我们设定一个大小为k的水池存放k个元素,第k + n个元素有(k / (k + n))的概率放到水池,把水池中一个随机元素换出。一个元素如果想最终留在蓄水池里,必须它被添加到蓄水池,而且它后面的元素没有把它换出去。算得它的概率原创 2017-11-09 11:17:41 · 145 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
原题:点击打开链接层次遍历二叉树,但是要求按锯齿状输出(类似于蛇形)。思考过程:一开始想用双向队列。Deque deque = new ArrayDeque();后来发现用栈就可以实现。栈和队列顺序相反,前者先进后出,后者先进先出。有点绕,但是最终还是解决了。解题思路:每层创建一个栈用于储存下一层(是按下一层输出顺序的倒序储存)。这里关键在于:如果本行是原创 2017-10-23 20:00:11 · 116 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal
原题:点击打开链接即实现给定二叉树的层次遍历。思考过程&解题思路:用队列解决这个问题不困难。第一次用java的队列Queue queue = new LinearList结果代码:List> ret = new ArrayList<>(); public List> levelOrder(TreeNode root) { if (ro原创 2017-10-23 19:52:27 · 125 阅读 · 0 评论 -
173. Binary Search Tree Iterator
原题:点击打开链接完成一个二叉搜索树的迭代器。里面的next()方法会返回下一个最小的值。hasNext()返回是否还有下一个。要求它们的时间复杂度平均O(1),空间复杂的O(h)。h是树的高度。思考过程:一开始想的是用中序遍历二叉搜索树得到结果就是所有值从小到大排列。把它们存到队列里,每次调用next()就出队列,很方便。没有去实现,因为觉得这样空间复杂度就超了。只好用不原创 2017-10-23 15:35:23 · 101 阅读 · 0 评论 -
Rotate Image
原题:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D m原创 2017-10-13 19:06:22 · 121 阅读 · 0 评论 -
Permutations
原题:Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [原创 2017-10-13 17:19:16 · 145 阅读 · 0 评论 -
220. Contains Duplicate III
原题:Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference betweennums[i] and nums[j] is at most t and theabsolute di原创 2017-10-30 20:54:08 · 139 阅读 · 0 评论 -
130. Surrounded Regions
原题:Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X转载 2017-10-30 20:46:33 · 129 阅读 · 0 评论 -
208. Implement Trie (Prefix Tree)
原题:Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.构建一个字典树(可以看成自动机),实现插入(增加相应节点)、搜索(判断字典树是否存在输入字符串)、是否原创 2017-10-30 20:21:12 · 138 阅读 · 0 评论 -
98. Validate Binary Search Tree
原题:点击打开链接即判断给定树是不是二叉搜索树(每个节点左子树所有节点值都小于该节点的值,右子树都大于该节点的值)。思考过程&解题思路:一开始只理解左节点小于该节点,右节点大于该节点,结果出了问题。后来想到每次递归时都要传值,传当前节点左节点不能小于的值和右节点不能大于的值。详情见代码注释。结果代码:public boolean isValidBST(Tre原创 2017-10-22 11:07:20 · 105 阅读 · 0 评论 -
Jump Game II
原题: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.Your g原创 2017-10-12 16:18:31 · 119 阅读 · 0 评论 -
Wildcard Matching
原题:Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching shoul原创 2017-10-12 14:48:50 · 122 阅读 · 0 评论 -
78. Subsets
原题:点击打开链接即返回给定数组的所有子集。思考过程&解题思路:一开始想到用递归的办法(其实是深度优先遍历DFS);后来看到更高明的办法:位操作。其实一个集合子集数量就是2的(集合元素个数)次方。因为对于任何一个元素,在某个子集中要么有,要么没有。所以子集个数就是这个情况数。用二进制数字表示当前子集是否有数组某个元素。二进制数字中第n位为1表示当前子集有数组的第n个元素。原创 2017-10-25 19:34:12 · 143 阅读 · 0 评论 -
55. Jump Game
原题:点击打开链接给定非负数组成的数组,数组元素代表该点可以向后移动几个单位。判断是否可以移动到数组末尾。思考过程&解题思路:这道题不像Jump GameII那样返回走的步数,简单很多。我是用一个reach表示当前可以走到最远的距离。如果reach可以到数组末尾,返回true;如果遍历数组遍历到reach恰巧reach位置的元素为0,返回false。每次遍历完数组更新reach原创 2017-10-26 17:25:58 · 135 阅读 · 0 评论 -
70. Climbing Stairs
原题:You are climbing a stair case. It takes n steps 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?Note: Given n will be a原创 2017-12-06 15:22:53 · 105 阅读 · 0 评论 -
73. 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.click to show follow up.Follow up:Did you use extra space?A straight forward solution using原创 2017-12-23 16:41:02 · 142 阅读 · 0 评论 -
72. Edit Distance
原题:Given two words word1 and word2, find the minimum number of steps required to convertword1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a原创 2017-12-23 16:26:41 · 108 阅读 · 0 评论 -
60. Permutation Sequence
String ret = ""; int k; public String getPermutation(int n, int k) { this.k = k - 1;//第k个字符串表示变化了k - 1次 int[] alphabet = new int[n];//存储还有哪些数字可选 for (int i = 1;i <= n;i原创 2017-11-27 11:46:38 · 130 阅读 · 0 评论 -
58. Length of Last Word
原题:即返回给定字符串最后一个单词的长度。单词之间用空格' ' 隔开。注意实验表明最后一个单词后面可以有空格。解题思路:先跳过尾部的空格,然后从尾部遍历,直到发现空格。AC代码:public int lengthOfLastWord(String s) { int len = s.length(); if (len == 0) r原创 2017-11-27 10:14:09 · 117 阅读 · 0 评论 -
68. Text Justification
原题:Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified. You should pack your words in a greedy approach; t原创 2017-12-06 12:06:05 · 116 阅读 · 0 评论 -
71. 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:原创 2017-12-14 18:37:58 · 140 阅读 · 0 评论 -
54. Spiral Matrix
原题:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9原创 2017-11-26 20:37:30 · 121 阅读 · 0 评论 -
57. Insert Interval
原题:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.原创 2017-11-25 22:30:27 · 128 阅读 · 0 评论 -
65. 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 ambi原创 2017-12-01 22:23:36 · 142 阅读 · 0 评论 -
560. Subarray Sum Equals K
原题:Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals tok.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:原创 2017-11-16 15:18:03 · 123 阅读 · 0 评论 -
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 as 1 and 0 respectively in the原创 2017-11-29 21:59:17 · 119 阅读 · 0 评论 -
62. Unique Paths
原题:Discuss Pick OneA 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 any point in time. The原创 2017-11-29 21:28:25 · 121 阅读 · 0 评论 -
61. Rotate List
原题:即把给定链表尾部k个元素挪到链表开始。思考过程 & 解题思路:先遍历整个链表并存到数组便于索引。所需要做的也就是把尾部和头部连起来,再把第len - k -1个元素后面断开。同时要考虑k == 0和k == len等情况。AC代码:public ListNode rotateRight(ListNode head, int k) { Arr原创 2017-11-29 20:56:53 · 142 阅读 · 0 评论 -
329. Longest Increasing Path in a Matrix
原题:Discuss Pick OneGiven an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT mo原创 2017-11-20 19:17:12 · 116 阅读 · 0 评论 -
207. Course Schedule
public boolean canFinish(int numCourses, int[][] prerequisites) { int countOfEdge = prerequisites.length;//边的个数 List> graph = new ArrayList<>();//存储一个点的前驱 ArrayList> inverseGra原创 2017-11-06 11:51:25 · 107 阅读 · 0 评论 -
621. Task Scheduler
原题:Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task co原创 2017-11-05 20:29:00 · 119 阅读 · 0 评论 -
319. Bulb Switcher
原题:即一共有n个灯泡关着,一共对它们操作n次,第i次操作会把第i的整数倍的灯泡改变状态。要求返回最后开着的灯泡。思考过程 & 解题思路:一开始看灯泡只有两种状态,想到的是位操作(虽然不太熟)。后来想到,所有位置不是平方数的灯泡都会被关掉。比如位置为6的灯泡。6 = 1 * 6 = 2 * 3。i = 1,2,3,6都会使他改变状态,改变四次相当于没变,还是关闭的。而对于9,9 =原创 2017-11-07 10:40:10 · 116 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
原题:点击打开链接即输出中序遍历所给二叉树结果,建议不用递归的方法。思考过程:先复习了一下,用递归方法解决,代码很简洁:List ret = new ArrayList<>(); public List inorderTraversalRecursive(TreeNode root) { if (root != null){原创 2017-10-21 12:07:50 · 103 阅读 · 0 评论 -
375. Guess Number Higher or Lower II
原题:We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I'll tell you whether the number I p原创 2017-11-06 20:11:13 · 132 阅读 · 0 评论