LeetCode
文章平均质量分 62
Ellie_
这个作者很懒,什么都没留下…
展开
-
[LeetCode]389. Find the Difference
依旧是利用a^b^b==a的bit manipulation的特性。另一个方法则利用了将字母作为数组下标的方法。(从本质上来讲,在计算机里面,可以认为char就是int了,就是说可以把char当作int来用,如,作为数组下标。)Given two strings s and t which consist of only lowercase letters.String原创 2017-08-06 21:13:05 · 219 阅读 · 0 评论 -
[LeetCode]237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value原创 2017-08-11 11:08:03 · 232 阅读 · 0 评论 -
[LeetCode]141. Linked List Cycle
Two pointers + Linked list题目。很容易想到的方法是用hashSet来进行存储。而要用到O(1)的空间的话,则需要两个指针,一快一慢,若有circle,终会相遇。The fast runner will eventually meet the slow runner. Why? Consider this case (we name it case A) - The f原创 2017-08-11 10:53:43 · 266 阅读 · 0 评论 -
[LeetCode]349. Intersection of Two Arrays
Binary Search很简单的一道题,但是犯了好多错误。要求:再从头写一遍,要求一次bug-free AC.https://leetcode.com/problems/intersection-of-two-arrays/description/Given two arrays, write a function to compute their interse原创 2017-08-03 16:50:28 · 336 阅读 · 0 评论 -
[LeetCode]257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]/**原创 2017-07-26 10:54:15 · 203 阅读 · 0 评论 -
[LeetCode]268. Missing Number
Point: a^b^b=a.two xor operations with the same number will eliminate the number and reveal the original number. 两个相同的数异或结果为0。一个数和两个相同的数异或的结果为它自身。Given an array containing n distinct numbers原创 2017-08-03 14:33:00 · 222 阅读 · 0 评论 -
[LeetCode]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-07-26 09:43:27 · 291 阅读 · 0 评论 -
[LeetCode]100. Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.方法一:用队原创 2017-07-26 09:15:23 · 202 阅读 · 0 评论 -
[LeetCode]173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2017-08-10 09:57:01 · 219 阅读 · 0 评论 -
[LeetCode]442. Find All Duplicates in an Array
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements that appear twice in this array.Could you do it without ex原创 2017-04-25 20:05:25 · 202 阅读 · 0 评论 -
[LeetCode]406. Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of t转载 2017-07-25 19:11:27 · 203 阅读 · 0 评论 -
[LeetCode]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: 6原创 2017-08-02 20:21:53 · 325 阅读 · 0 评论 -
[LeetCode]513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Example 2: Input: 1 / \ 2 3 / /原创 2017-07-25 09:46:43 · 190 阅读 · 0 评论 -
[LeetCode]374. Guess Number Higher or Lower<Binary Search>
注意亮点。1、low=mid+1; high=mid;2、mid=(high-low)/2+low; 不可以用mid=(low+high)/2,因为加了以后数据有可能会溢出。We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to gu原创 2017-08-03 16:03:53 · 262 阅读 · 0 评论 -
[LeetCode]160. Intersection of Two Linked Lists
重点在于两个list的长度。如何可以巧妙地使得两个不同长度的list的指针遍历到同一个位置上去。Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A:原创 2017-08-11 15:13:31 · 285 阅读 · 0 评论 -
[LeetCode]645. Set Mismatch
其实还是那种利用【数组元素->数组下标】来【转换正负性】的问题。The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in t原创 2017-08-06 20:58:59 · 837 阅读 · 0 评论 -
[LeetCode]204. Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.public class Solution { public int countPrimes(int n) { int res=0; boolean[] prime = new bo原创 2017-08-06 19:53:37 · 224 阅读 · 0 评论 -
[LeetCode]206. Reverse Linked List
注意递归的解法。原创 2017-08-14 10:49:50 · 251 阅读 · 0 评论 -
[LeetCode]Sliding Window Algorithm相关题目总结【重要】
滑动窗口算法的一些具体应用。subString的问题。https://leetcode.com/problems/find-all-anagrams-in-a-string/discuss/原创 2017-08-06 17:10:04 · 3503 阅读 · 1 评论 -
[LeetCode]53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] ha原创 2017-07-29 08:26:37 · 177 阅读 · 0 评论 -
[LeetCode]303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange转载 2017-07-28 20:49:32 · 243 阅读 · 0 评论 -
[LeetCode]547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, the原创 2017-07-27 20:51:43 · 437 阅读 · 0 评论 -
[LeetCode]110. Balanced Binary Tree
Given 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 of every node never diffe原创 2017-07-27 17:43:10 · 254 阅读 · 0 评论 -
[LeetCode]111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for a binary原创 2017-07-27 17:05:10 · 239 阅读 · 0 评论 -
[LeetCode]108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST./** * Definition for a binary tree node. * public class TreeNode { * int val; * T原创 2017-07-27 16:51:36 · 182 阅读 · 0 评论 -
[LeetCode]112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2017-07-27 15:33:30 · 194 阅读 · 0 评论 -
[LeetCode]594. Longest Harmonious Subsequence<HashMap>
We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.Now, given an integer array, you need to find the length of its longest h原创 2017-08-04 16:37:22 · 261 阅读 · 0 评论 -
[LeetCode]637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanat原创 2017-07-24 18:42:59 · 339 阅读 · 0 评论 -
[LeetCode]344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".题目不难,但是超了time limitStringBuffer类和String一样,也用来代表字符串,只是由于Str转载 2017-07-24 17:08:25 · 223 阅读 · 0 评论 -
[LeetCode]575. Distribute Candies
Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute the原创 2017-07-24 16:48:49 · 190 阅读 · 0 评论 -
[LeetCode]538. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.Exam原创 2017-08-07 16:29:34 · 235 阅读 · 0 评论 -
[LeetCode]537. Complex Number Multiplication<Java>
Given two strings representing two complex numbers.You need to return a string representing their multiplication. Note i2 = -1 according to the definition.Example 1:Input: "1+1i", "1+1i"Out原创 2017-07-22 23:22:07 · 623 阅读 · 0 评论 -
[LeetCode]283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2017-07-31 17:00:00 · 231 阅读 · 0 评论 -
[LeetCode]646. Maximum Length of Pair Chain<DP>
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.Now, we define a pair (c, d) can follow another pair (a, b) if and only if b . Chain转载 2017-07-31 16:04:53 · 550 阅读 · 0 评论 -
[LeetCode]448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Coul原创 2017-07-31 14:58:17 · 216 阅读 · 0 评论 -
[LeetCode]1. Two Sum
Given 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 the same原创 2017-07-31 10:28:24 · 219 阅读 · 0 评论 -
[LeetCode]617. Merge Two Binary Trees<Java>
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree原创 2017-07-22 09:55:24 · 925 阅读 · 0 评论 -
[LeetCode]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 positive原创 2017-07-29 20:35:49 · 198 阅读 · 0 评论 -
[LeetCode]121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock),原创 2017-07-29 20:18:38 · 183 阅读 · 0 评论 -
[LeetCode]198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2017-07-29 19:43:09 · 250 阅读 · 0 评论