leetcode
文章平均质量分 66
一头食量超大的小猪
要吃要玩更要学
展开
-
stack&queue
71. simplify path:简化ubuntu中的文件路径当遇到"/../"时,需要返回上级目录,返回之间应该检查上级是否为空,若为空的话,则就代表"/"当遇到"//"或"/./"时,不用做任何操作,这都代表的是本级目录;遇到其它字符时,则代表的是文件夹名;*代码处理时:字符串间的比较采用equals()方法,使用"=="比较出错. public String s原创 2016-06-24 12:48:14 · 282 阅读 · 0 评论 -
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 on原创 2016-09-07 21:47:48 · 368 阅读 · 0 评论 -
11. 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 linei is at (i, ai) and (i, 0). Find t原创 2016-09-12 10:47:37 · 158 阅读 · 0 评论 -
3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.最简单的解决方式,逐渐遍历time o(n^2) space o(n^2) public int lengthOfLongestSubstring(String s) { int len=s.l原创 2016-08-31 13:38:58 · 189 阅读 · 0 评论 -
5.Longest Palindromic Substring
Given a string S, find the longest palindromic substring inS. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.循环遍历:回文字符串有2种可能,一种是abba原创 2016-08-31 21:27:36 · 195 阅读 · 0 评论 -
1.two sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.time:o(n) space:o(n)最初做法用2个循环,但是用一个循环会节省一点时间,虽然时间复杂度一样。 public int[] twoSum(int[] n原创 2016-08-31 23:06:19 · 191 阅读 · 0 评论 -
2.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 link原创 2016-08-31 23:07:43 · 179 阅读 · 0 评论 -
12. Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.解题思路:将其从大到小进行比较,相同的将多个相同数字写在一起. public String intToRoman(int num) { int[]原创 2016-09-12 17:17:31 · 160 阅读 · 0 评论 -
13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.时间和空间复杂度都是O(N).从后往前计算. public int romanToInt(String s) { //:Ⅰ(1)Ⅴ(5原创 2016-09-12 17:26:46 · 155 阅读 · 0 评论 -
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.最简单的解题思路:首先遍历一遍找到长度最短的字符串a,因为最长的前缀小于等于a;这样处理之后减少比较次数,然后将其它字符串依次与其比较前缀,并逐渐更新前缀的长度,如果更小则更新,最后返回字串即可.时间复杂原创 2016-09-12 18:03:51 · 159 阅读 · 0 评论 -
17. 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:Digit string原创 2016-09-12 21:20:12 · 129 阅读 · 0 评论 -
15. 16. 18.3Sum 4sum
Given an array S of n integers, are there elementsa, b, c in S such that a + b +c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The solution set must not contain d原创 2016-09-12 19:43:28 · 165 阅读 · 0 评论 -
19. 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 from the end, the li原创 2016-09-13 10:05:44 · 143 阅读 · 0 评论 -
20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all val原创 2016-09-13 10:52:57 · 143 阅读 · 0 评论 -
22. Generate Parentheses
public List generateParenthesis(int n) { List list=new ArrayList(); if(n<=0) return list; if(n==1){ list.add("()"); return list; }原创 2016-09-13 16:20:08 · 184 阅读 · 0 评论 -
21.23. 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.思路:选择两个list中第一个元素最小的为基础,然后将另外一个list的元素逐渐和第一个list中的元素相比,原创 2016-09-13 15:38:09 · 194 阅读 · 0 评论 -
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.负数不算回文串;将此整数求逆可能会发生溢出,当不属于回文串的情况下;只用转换一般进行比较即可; public boolean isPalindrome(int x) { if(x<0||(x%10==0&原创 2016-09-06 15:24:38 · 134 阅读 · 0 评论 -
24. 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 constant space. You m原创 2016-09-13 21:13:32 · 198 阅读 · 0 评论 -
26. 27. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2016-09-14 09:26:34 · 166 阅读 · 0 评论 -
28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.最直接的方式: 从头开始逐渐匹配,若不一样,则后移一位继续比较.时间复杂度:O(M*N) M/N分别为两个字符串的长度pub原创 2016-09-14 10:29:32 · 143 阅读 · 0 评论 -
29. Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.思路: 需要注意的是Math.abs(Integer.MIN_VALUE)=Integer.MAX_VALUE+1;因此当遇到被除数是Integer.MIN_V原创 2016-09-14 19:29:36 · 290 阅读 · 0 评论 -
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2016-09-15 10:26:42 · 193 阅读 · 0 评论 -
32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is"()", whi原创 2016-09-18 10:17:13 · 148 阅读 · 0 评论 -
34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(log n).If the target is not found in原创 2016-09-18 14:45:53 · 144 阅读 · 0 评论 -
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2016-09-18 15:17:00 · 138 阅读 · 0 评论 -
6.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)时间复杂度是:O(N) 空间复杂度也原创 2016-09-06 12:13:50 · 169 阅读 · 0 评论 -
7.Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321some tips:数字反过来之后可能会出现越界情况,因此每次要判断是否会出现越界情况; public int reverse(int x) { int result=0原创 2016-09-06 13:21:57 · 155 阅读 · 0 评论 -
8. String to Integer (atoi)
Implement atoi to convert a string to an integer.首先检查字符串开头是否是空格,若是空格则忽略;接着检查是否有正负号,正负号最多只能出现一个;其次是检查后面是不是数字,若是数字则进行转换,越界的返回临界值;若出现的不是数字,则返回已检测到的数字即可. public int myAtoi(String str)原创 2016-09-06 14:36:58 · 163 阅读 · 0 评论 -
36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.思路: 即判断每行每列和9个正方形中有没原创 2016-09-19 13:10:36 · 134 阅读 · 0 评论 -
62.63.64.Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2016-10-08 20:05:24 · 212 阅读 · 0 评论 -
65. Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => truepublic class Solution { public boolean isNumber(St原创 2016-10-09 01:55:21 · 222 阅读 · 0 评论 -
66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.code:public原创 2016-10-09 02:14:18 · 297 阅读 · 0 评论 -
67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".public class Solution { public String addBinary(String a, String b) {原创 2016-10-09 02:35:08 · 227 阅读 · 0 评论 -
37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.思路: 对每一个未定的元素,原创 2016-09-19 21:39:41 · 190 阅读 · 0 评论 -
136. Single Number
Given an array of integers, every element appears twice except for one. Find that single one.要求:线性时间,常量空间异或:2个相同数异或为0,0与任意数异或还是等于原来的数。public int singleNumber(int[] nums) { int result原创 2016-09-07 09:43:47 · 143 阅读 · 0 评论 -
46.47. Permutations
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]原创 2016-09-22 23:45:11 · 183 阅读 · 0 评论 -
48. Rotate Image
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?思路: 先将数组以中间行为标准翻转,然后再以主对角线为标准交换对应元素。时间复杂度:O(N*N)空间复原创 2016-09-23 00:13:08 · 171 阅读 · 0 评论 -
68. Text Justification
Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.You should pack your words in a greedy approach; that is原创 2016-10-09 09:45:19 · 165 阅读 · 0 评论 -
69. Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.最简单的思路: 从1开始,直到其平方大于为止;public class Solution { public int mySqrt(int x) { if(x<=0) return 0;原创 2016-10-09 11:08:45 · 149 阅读 · 0 评论 -
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?分析:动态规划问题, 一层1,二层2,第i层等于第i-1原创 2016-10-09 11:18:22 · 186 阅读 · 0 评论