leetcode
_menglan_Zi
水水一枚
展开
-
candy
candytitle原题目链接几个小孩站一排,每个小孩有个等级值,现在给小孩发糖,发的时候要遵守2个规则:(1)每个小孩至少一颗糖(2)两个相邻的小孩中,等级大的小孩一定比等级小的小孩糖多,求发糖的数目的最小值solution最直接的想法就是,开始的时候给每个人一颗candy. 如果我比你高,我就比你多1,最低的孩子为1,从左往右扫瞄一次,波谷为1,然后依次加原创 2014-10-30 18:44:55 · 796 阅读 · 0 评论 -
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.思路对搜索二叉树(BST)进行中序遍历, 得到便是有序的序列.既原创 2015-10-06 10:55:23 · 523 阅读 · 0 评论 -
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 an原创 2015-10-06 09:32:25 · 475 阅读 · 0 评论 -
Count and Say
原题链接题目The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2原创 2015-10-06 09:57:15 · 456 阅读 · 0 评论 -
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-11-05 23:39:58 · 549 阅读 · 0 评论 -
Different Ways to Add Parentheses
题目链接原题Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *.思路左右子串分别计算原创 2015-11-05 23:52:24 · 549 阅读 · 0 评论 -
Binary Tree Right Side View
原题Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-10-06 18:32:22 · 486 阅读 · 0 评论 -
Range Sum Query - Immutable
题目链接原题Given an integer array nums, find the sum of theelements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0原创 2015-11-12 12:03:43 · 633 阅读 · 0 评论 -
Range Sum Query 2D - Immutable
题目链接原题Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by (row1, col1), (row2, col2).Example:Given matrix = [ [3, 0, 1, 4, 2], [5, 6, 3, 2, 1], [1, 2, 0, 1, 5原创 2015-11-12 12:00:11 · 763 阅读 · 0 评论 -
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.Exa原创 2015-10-29 13:58:41 · 461 阅读 · 0 评论 -
H-Index和H-Index II
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:原创 2015-11-14 09:47:54 · 1721 阅读 · 0 评论 -
Merge Two Sorted Lists
题目原题Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.思路类似于有序数组的归并排序.这里需要注意的是如何设置head, 和如何交换l1, l2.code/**原创 2015-10-29 14:10:22 · 533 阅读 · 0 评论 -
Single Number III
原题原题Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:Given nums =原创 2015-10-28 20:51:40 · 430 阅读 · 0 评论 -
Merge Intervals
题目原题Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].思路先排序后mergecode/** * Definition for an interval. * stru原创 2015-10-29 14:05:27 · 401 阅读 · 0 评论 -
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原创 2015-10-30 00:31:47 · 378 阅读 · 0 评论 -
House Robber II
题目原题After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged i原创 2015-10-30 00:41:08 · 469 阅读 · 0 评论 -
Remove Linked List Elements
原题链接题目Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5思路先处理链表为空/只有一个元素的情况, 其余的情况直接遍原创 2015-10-05 12:17:40 · 444 阅读 · 0 评论 -
Number of Islands - leetcode
题目: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 assu原创 2015-10-03 14:06:44 · 586 阅读 · 0 评论 -
two sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2014-10-29 17:41:38 · 625 阅读 · 0 评论 -
Max Points on a Line
Max Points on a LineleetcodeMax Points on a Line题目链接题目大意:给定一些点(二维)的集合, 找出位于同一条直线的点的个数的最大值Code reference/** * Definition for a point. * struct Point { * int x; * i原创 2014-11-29 19:24:28 · 703 阅读 · 0 评论 -
Reverse Integer
Reverse IntegerleetcodeTitleCodeTitle题目链接Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Codeclass Solution {publ原创 2014-11-29 12:03:27 · 633 阅读 · 0 评论 -
Pascal's Triangle
Pascal's Triangleleetcode原题链接题意Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1]原创 2015-01-29 10:48:27 · 569 阅读 · 0 评论 -
Pascal's Triangle II
Pascal's Triangle IIleetcode原题链接题意Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1].Note:Could you opt原创 2015-01-29 11:18:07 · 585 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal IIleetcode dfs bfs原题链接题意Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level b原创 2015-01-29 11:36:03 · 548 阅读 · 0 评论 -
Add Binary
Add Binaryleetcode binary原题链接题意Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100".思路:处理好carry的情况原创 2015-01-28 22:10:17 · 623 阅读 · 0 评论 -
Largest Number
Largest Numberleetcode sort原题链接 参考 - Cindy_niu题意Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5,原创 2015-01-29 10:18:36 · 652 阅读 · 0 评论 -
Majority Element
Majority Elementleetcode bit-opearaion map sort majority原题链接题意Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊原创 2015-01-30 18:50:16 · 679 阅读 · 0 评论 -
Binary Tree Level Order Traversal
Binary Tree Level Order Traversalleetcode dfs bfsBinary Tree Level Order Traversal题目链接reference题目大意:Given a binary tree, return the bottom-up level order traversal of i原创 2015-01-20 17:54:56 · 767 阅读 · 0 评论 -
Add Two Numbers
Add Two Numbersleetcode mergeAdd Two Numbers题目链接题目大意:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of原创 2015-01-20 20:26:32 · 889 阅读 · 0 评论 -
Find Median from Data Stream
题目链接原题Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.Examples: [2,3,4] , the media原创 2015-11-04 09:49:33 · 417 阅读 · 0 评论 -
Maximal Square
题目Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return原创 2015-10-04 10:19:37 · 497 阅读 · 0 评论 -
Reverse Linked List
[题目](Reverse Linked List)原题Reverse a singly linked list.思路递归或者非递归.需要注意的是反转后最后节点的next指向(NULL).code/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;原创 2015-10-29 23:03:59 · 403 阅读 · 0 评论