- 博客(31)
- 资源 (21)
- 收藏
- 关注
原创 [LeetCode]304. Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).The above rectangle (with the red bo
2017-04-26 00:21:09 200
原创 [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) -> -1sumRan
2017-04-26 00:19:34 158
原创 [LeetCode]300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], ther
2017-04-24 22:02:42 147
原创 [LeetCode]292. Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the
2017-04-23 22:47:15 178
原创 [LeetCode]290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
2017-04-23 17:27:22 180
原创 [LeetCode]287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
2017-04-23 10:56:48 135
原创 [LeetCode]279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n =
2017-04-23 00:32:06 158
原创 [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-04-22 21:51:25 228
原创 [LeetCode]275. H-Index II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?思路:这个就是给你从小到大排序过的数组了,可以直接用上一个算法解决了。。。让h先等于n(能取到的最大数),然后用nums[n-h]与h相比较,因为
2017-04-22 00:04:47 150
原创 [LeedCode]274. H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikipedia: "A
2017-04-21 23:57:41 156
原创 [LeetCode]268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm sho
2017-04-21 23:32:32 143
原创 [LeetCode]264. Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first
2017-04-20 23:44:06 224
原创 [LeetCode]263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc
2017-04-20 23:22:20 151
原创 [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-04-20 22:38:59 138
原创 [LeetCode]242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.思路:很简单,使用两个数组存
2017-04-20 22:23:27 153
原创 [LeetCode]240. Search a 2D Matrix II
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 in ascending from left to right.Integers in
2017-04-20 00:26:31 181
原创 [LeetCode]239. Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window
2017-04-20 00:09:45 151
原创 [LeetCode]238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O
2017-04-19 23:42:20 119
原创 [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-04-19 00:40:13 149
原创 [LeetCode]236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node
2017-04-19 00:29:04 210
原创 [LeetCode]235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw
2017-04-18 21:59:22 135
原创 [LeetCode]234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.思路:快慢指针,慢指针一次跳一个,快指针一次跳两个,这样快指针跳完慢指针就是中心点这样还是要用到n/2的栈空间,使用反转链表法可以实现1空间/** * Definition for singly-linked list. * public clas
2017-04-18 21:45:27 132
原创 [LeetCode]232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(
2017-04-16 23:21:07 170
原创 [LeetCode]229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.public class Solution { public List majorit
2017-04-16 22:47:02 168
原创 [LeetCode]230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ? k ? BST's total elements.思路:从最小的开始进行从1编号,直到等
2017-04-16 22:29:59 193
原创 [LeetCode]228. Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].思路:很简单的一道题,遍历一遍找到连续的存下就行了public c
2017-04-16 22:07:23 197
原创 [LeetCode]225. Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet
2017-04-16 13:21:58 134
原创 [LeetCode]226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1思路:这个很简单,判断root是否为空,不为空则将左右子节点交换,然后递归进行左右子节点/** * Definition for a bin
2017-04-16 10:07:44 144
原创 [LeetCode]224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em
2017-04-16 09:45:19 338
原创 [LeetCode]223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the tota
2017-04-15 00:09:52 176
原创 [LeetCode]222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely fille
2017-04-14 23:31:28 251
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人