
leetcode
文章平均质量分 68
樱桃小猴子atZJU
这个作者很懒,什么都没留下…
展开
-
【leetcode】Interleaving String
题目:Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbba原创 2014-07-23 20:15:03 · 453 阅读 · 0 评论 -
【leetcode】ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line: "PAHNAPLSIIG原创 2014-08-11 21:53:39 · 769 阅读 · 0 评论 -
【leetcode】Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.解析:求字符串数组中所有数组的最长公共前缀,重点考察细节和边界条件,比如:[] :输入字符串数组为空,要判断if (strs .size() == 0)原创 2014-08-28 15:55:01 · 532 阅读 · 0 评论 -
【leetcode】括号符匹配问题(Parentheses):Valid Parentheses|Generate Parentheses|LongestValid Parentheses
leetcode上有三道关于括号符(‘(’,‘)’,‘[’,‘]’,‘{’,‘}’)匹配的问题,其中第三题是第一题的拓展,分别是:(1)Valid Parentheses,判断字符串是否是合法的括号匹配字符串(2)Generate Parentheses,根据所给的整数生成所有合法的括号匹配字符串(3)Longest Valid Parentheses,根据所给的括号字符串,求其中最长的括号匹配字符子串 下面分别讨论我对这三道题的思路的做法。原创 2014-08-28 17:09:59 · 3172 阅读 · 0 评论 -
【leetcode】Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.解析:将一个整形数字倒序输出,注意判断正负。这道题在华为面试的时候遇到过,很简单。原创 2014-08-14 21:01:19 · 650 阅读 · 0 评论 -
【leetcode】Remove Nth Node From End of List
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node fr原创 2014-08-28 16:58:19 · 633 阅读 · 0 评论 -
【leetcode】Letter Combinations of a Phone Number
题目:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:D原创 2014-08-28 16:17:08 · 395 阅读 · 0 评论 -
【leetcode】Merge k Sorted Lists (归并排序)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解析:合并k个有序链表,最后返回一个总的有序链表,分析并描述其复杂度。该题的实质为归并排序,平均时间复杂度为O(NlogN)。原创 2014-08-29 10:07:20 · 699 阅读 · 0 评论 -
【leetcode】Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only cons原创 2014-08-29 10:19:52 · 439 阅读 · 0 评论 -
【leetcode 链表相关】Reverse Nodes in k-Group
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should原创 2014-08-29 11:21:34 · 492 阅读 · 0 评论 -
【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
leetcode上有四道关于移除有序序列中重复数字的题目,其中两道为数组结构,两道为链表结构,分别为:(1)Remove Duplicates from sorted array I:移除一个有序数组中的重复数字,并且返回新数组的大小。(2)Remove Duplicates from sorted array II:移除一个有序数组中的重复数字,并且返回新数组的大小,和上道题目不同的是每原创 2014-08-29 16:32:22 · 783 阅读 · 0 评论 -
【leetcode】String to Integer (atoi)
题目:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.Notes: It is intended for this problem to be spe原创 2014-08-14 21:01:46 · 411 阅读 · 0 评论 -
【leetcode 排列组合问题】Next Permutation | Permutations | Permutations II | Permutation Sequence
leetcode上关于数字排列组合问题有四个,分别是:(1)Next Permutation :已知一个数组代表某个排列组合,求出比其代表的数字大的下一个排列组合,通过重组数组中的数字实现(2)Permutations:已知一组数字,求出所有可以表示出的排列组合(3)Permutations II:已知一组数字可能包含重复,求出所有可以表示出的排列组合(无重复的)(4)Permut原创 2014-09-03 23:02:20 · 1008 阅读 · 0 评论 -
【leetcode】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the n原创 2014-08-31 16:18:14 · 476 阅读 · 0 评论 -
【leetcode KMP算法实现】Implement strStr()
题目:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.解析:实现strStr()方法,给定字符串haystack和needle,原创 2014-08-31 21:51:16 · 733 阅读 · 0 评论 -
【leetcode】Divide Two Integers
题目:Divide two integers without using multiplication, division and mod operator.解析:原创 2014-08-31 22:33:52 · 829 阅读 · 0 评论 -
【leetcode】Container 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 line i is at (i, ai)原创 2014-08-26 23:39:30 · 508 阅读 · 0 评论 -
【leetcode】Regular Expression Matching
题目:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover th原创 2014-08-26 21:53:52 · 519 阅读 · 0 评论 -
【leetcode】Container With Most Water
题目:给一组数组作为水池的高度,以其中两个高度作为水池的壁,求最大的水池容量。假设左边的壁为left,右边的壁为right,则水池容量capacity= (right-left)*min(height(right),height(right))定义两个指针left和right从数组两头开始遍历:如果left的高度低于right,则将left指针右移找原创 2014-07-17 15:33:31 · 562 阅读 · 0 评论 -
【leetcode】Valid Parentheses
题目:这道题原创 2014-07-17 15:21:55 · 396 阅读 · 0 评论 -
【leetcode】Spiral Matrix
今天在命令行用ant跑junit时候一直出现错误java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing查了很多资料,原来要将junit的jar包拷贝到ANT_HOME/lib下。但是之后错误还是存在,看到解释是版本问题,有提出要添加hamcrest.jar包的,试过没用。原创 2014-07-17 14:45:58 · 514 阅读 · 0 评论 -
【leetcode】 Longest Common Prefix
求字符串数组中所有数组的最长公共前缀,代码很简单,但是要很注重细节边界值输入要特别注意,比如:[] 输入空的字符串数组,要判断 if (strs .size() == 0) return "";[""] 字符串数组里面只有一个空字符串等情况等等。原创 2014-07-17 15:24:37 · 499 阅读 · 0 评论 -
【leetcode】Roman to Integer | Integer to Roman
题目:【leetcode】12 Integer to Roman(将阿拉伯数字转换成罗马数字)原创 2014-07-17 15:32:32 · 536 阅读 · 0 评论 -
【leetcode】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 ne原创 2014-07-29 22:46:36 · 668 阅读 · 0 评论 -
【leetcode】Edit Distance
题目:编辑距离问题是原创 2014-07-17 15:15:17 · 481 阅读 · 0 评论 -
【leetcode】Decode Ways
题目:A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the原创 2014-07-21 21:25:44 · 454 阅读 · 0 评论 -
【leetcode】Restore IP Addresses
题目:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]原创 2014-07-21 22:41:00 · 460 阅读 · 0 评论 -
【leetcode】Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the lengt原创 2014-08-08 23:39:06 · 619 阅读 · 0 评论 -
【leetcode X Sum 系列】Two Sum|3Sum|3Sum Closest|4Sum
Two Sum 题目:My SubmissionsGiven 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, where index1 must be less than ind原创 2014-08-07 20:40:47 · 928 阅读 · 0 评论 -
【leetcode】Surrounded Regions
类似于中国围棋的二维字符数组,将被'X'包围的'O'全部转化为'X',不同的是,与边缘接轨的'O'不用转化为'X',所以切入点在于边缘的‘O’,从边缘开始做DFS或者BFS,由于用例有200*200的棋盘,使用Java写DFS会因为递归太多层报Stackoverflow的错误,用C++实现能够AC:原创 2014-08-04 20:08:03 · 590 阅读 · 0 评论 -
【leetcode】Median of Two Sorted Arrays
解析:求两个排序数组的中位数,数组长度分别是m和n,要求时间复杂度为O(log(m+n))。刚开始思路是利用类似合并排序的思路分别遍历两个数组,但是算法复杂度为O(n)。查看网上的解题方法,原问题其实是一个寻找第k小数的问题,中位数实际上是第(m+n)/2小的数。对于数组a和b,比较A的第k/2小的元素A[k/2-1]和B的第k/2小的元素B[k/2-1]两个元素,有三种情况可以考虑:1. A[k/2-1]<B[k/2-1]表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中,可以把A原创 2014-08-07 22:53:09 · 450 阅读 · 0 评论 -
【leetcode】Add Two Numbers 解析以及拓展
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Outpu原创 2014-08-10 10:19:04 · 1074 阅读 · 0 评论 -
【leetcode】Longest Palindromic Substring
题目:Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.解析:求一个字符串的最长回文子串。有三种解法,并且时间不断在优化:解法一:暴力解法,遍历字符串,将每个字符做子串中心,向两端扩展,时间复杂度O(N^2原创 2014-08-10 11:40:56 · 402 阅读 · 0 评论 -
【leetcode】Substring with Concatenation of All Words
题目:You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly原创 2014-09-03 22:51:47 · 476 阅读 · 0 评论