- 博客(239)
- 收藏
- 关注
原创 [leetcode] 226.Invert Binary Tree
题目: Invert a binary tree.4/ \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 题意: 翻转二叉树。 思路: 这道题采用递归的思想,将左右子树分别反转,然后将左右子树对调。 代码如下:/** * Definitio
2015-07-23 17:04:49 260
原创 [leetcode] 210.Course Schedule II
题目: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a
2015-07-22 22:36:03 244
原创 [leetcode] 209.Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array [2,3,1
2015-07-22 22:01:25 365
原创 [leetcode] 207.Course Schedule
题目: There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a
2015-07-20 10:45:28 362
原创 [leetcode] 206.Reverse Linked List
题目: Reverse a singly linked list. 题意: 反转单链表。 思路: 可以使用循环的方式,或者栈的方式,或者递归。 栈的方式是把每个元素依次入栈,出来的时候就是从尾到头的顺序。 递归的本质就是栈,此处不表。 代码如下: 注释部分是循环和栈的两个实现。/** * Definition for singly-linked list. * struct L
2015-07-20 00:23:09 247
原创 [leetcode] 188.Best Time to Buy and Sell Stock IV
题目: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete at most k transactions.Note: You may not en
2015-07-19 23:24:34 307
原创 [leetcode] 205.Isomorphic Strings
题目: Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another
2015-07-19 21:47:14 329
原创 [leetcode] 204.Count Primes
题目: Description:Count the number of prime numbers less than a non-negative number, n.题意: 找出小于n的所有素数。 思路: 采用筛选法。首先知道如果一个数不是素数,那么它一定可以表示成多个素数相乘,而且很明显这些素数肯定是比这个合数小。 每次第一个没删去的元素就是素数,接下来找该素数的所有倍数(倍数大于等
2015-07-19 01:20:28 326
原创 [leetcode] 203.Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 题意: 删除链表中元素值等于某个值的所有节点。 代码如下:/**
2015-07-18 19:28:54 254
原创 [leetcode] 200.Number of Islands
题目: Given a 2d grid map of ‘1’s (land) and ‘0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may as
2015-07-18 18:43:03 305
原创 [leetcode] 201.Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the range [5, 7], you should return 4. 题意: 给定一个区间,找出该区间所有元
2015-07-18 18:11:56 1161
原创 [leetcode] 202.Happy Number
题目: Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares
2015-07-18 17:53:59 314
原创 [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 wind
2015-07-18 15:52:55 317
原创 [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”].题意: 排序数组,不包含重复元素。返回每一个区间。区间就是元素连续相连的。 思路: 比较简单就是从前
2015-07-18 10:57:35 186
原创 [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 h
2015-07-17 23:55:32 283
原创 [leetcode] 189.Rotate Array
题目: Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note: Try to come up as many solutions as you can,
2015-07-17 15:57:58 343
原创 [leetcode] 179.Largest Number
题目: Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be very l
2015-07-16 17:48:56 249
原创 [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(n).
2015-07-16 14:43:31 345
原创 [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() and
2015-07-16 14:08:03 274
原创 [leetcode] 172.Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity. 题意: 给定一个整数n,返回n!的末尾的0的个数。 思路: 首先能够出现0是末尾是5的数乘上2的倍数或者末尾是0的数。而且是2的倍数的
2015-07-16 00:32:20 229
原创 [leetcode] 171.Excel Sheet Column Number
题目: Given a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 题意: 把26进制的数转化为十进制。 代码比较简单,如下:class
2015-07-15 23:58:30 154
原创 [leetcode] 168.Excel Sheet Column Title
题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB题意: 给定一个正数,返回它在excel中对应的列号。如上面所示,这是一个2
2015-07-15 23:45:19 193
原创 [leetcode] 164.Maximum Gap
题目: Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements.You
2015-07-15 23:36:20 598
原创 [leetcode] 165.Compare Version Numbers
题目: Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and
2015-07-15 14:47:46 348
原创 [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
2015-07-15 14:24:58 327
原创 [leetcode] 162.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, in that
2015-07-15 00:02:44 227
原创 [leetcode] 160.Intersection of Two Linked Lists
题目: 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: a1 → a2 ↘
2015-07-14 21:44:24 286
原创 [leetcode] 155.Min Stack
题目: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get
2015-07-14 17:37:33 296
原创 [leetcode] 154.Find Minimum in Rotated Sorted Array II
题目: Follow up for “Find Minimum in Rotated Sorted Array”: What if duplicates are allowed?Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unkno
2015-07-14 17:27:27 325
原创 [leetcode] 153.Find Minimum in Rotated Sorted Array
题目: Suppose a sorted array 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 duplicate exists in the array.
2015-07-14 15:43:30 357
原创 [leetcode] 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
2015-07-14 10:38:23 381
原创 [leetcode] 150.Evaluate Reverse Polish Notation
题目: Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: [“2”, “1”, “+”, “3”
2015-07-13 21:14:54 269
原创 [leetcode] 146.LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.get(key) - Get the value (will always be positive) of the key if
2015-07-13 16:39:42 423
原创 [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 nodes
2015-07-13 11:05:47 233
原创 [leetcode] 148.Sort List
题目: Sort a linked list in O(n log n) time using constant space complexity. 题意: 使用常量空间对链表进行时间复杂度是O(n log n)的排序。 思路: 首先回忆下时间复杂度是O(nlogn)的排序算法,有归并排序,堆排序,二分插入法等。 我打算使用归并排序来完成。 首先将链表分成两个部分,每个部分是[n/2]
2015-07-12 17:48:48 263
原创 [leetcode] 147.Insertion Sort List
题目: Sort a linked list using insertion sort. 题意: 使用插入排序的方法对一个链表进行排序。 思路: 首先你得知道什么叫做插入排序。插入排序就是将未排序的元素插入到已经排好序的那部分元素中去。 对于链表,主要考虑插入的位置是不是在最前面,注意空指针的判断。 比较简单。 代码如下:/** * Definition for singly-li
2015-07-12 17:38:45 248
原创 [leetcode] 143.Reorder List
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4
2015-07-12 16:01:25 379
原创 [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 betwee
2015-07-12 15:37:14 374
原创 [leetcode] 135.Candy
题目: There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one ca
2015-07-10 17:58:14 616
原创 [leetcode] 131.Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = “aab”, Return[ [“aa”,”b”],
2015-07-10 17:29:13 298
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人