plan454的博客
plan454 · 共 22 篇文章
Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这题我写的太难看了,就直接看网上copy下来的吧,具体是哪里copy过来的我忘记了。几天的思想就是分治。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * …
2015-03-12 19:55 · leetcode刷题 · 评论(0)
Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 这道题我是用递归的方式做的,用堆栈来判断是否复合条件,同时加上一个判断有多少左括号的函数。 public class Solution { public boolean isValid(String s…
2015-03-12 19:50 · leetcode刷题 · 评论(0)
Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '', determine if the input string is valid. The brackets must close in the correct order, "()" and "(){}" are all valid but "(]" and "(" are not. 这是一道匹配题,最简单的方式就是用栈来做了,JAVA中有Strack类,所以直接用就可以了。 public boolean isValid(String s) { boole…
2015-03-05 22:33 · leetcode刷题 · 评论(0)
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 linked list becomes 1->2->3->5. Note: Given n will always be valid. Try to do this in one pass. 基础题,考验对链表的理解。…
2015-03-05 22:31 · leetcode刷题 · 评论(0)
Letter Combinations of a Phone Number
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 "23" Output: . Note: Although the above answer is in lexicograph…
2015-03-05 22:30 · leetcode刷题 · 评论(0)
4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d) The solution set must not co…
2015-03-05 22:26 · leetcode刷题 · 评论(0)
3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to…
2015-03-05 22:25 · leetcode刷题 · 评论(0)
3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solution set must not contain duplicate triplets. For …
2015-03-03 22:34 · leetcode刷题 · 评论(0)
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 额,这个暴力点就成 public String longestCommonPrefix(String[] strs) { String result = ""; if(strs == null || strs.length==0){ return ""; } int s = strs[0].length(); int len = strs.length; int index= 0; boolean temp = fals…
2015-03-03 22:21 · leetcode刷题 · 评论(0)
Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 这个和之前的相比就是倒序一下 public int romanToInt(String s) { int result = 0; if(s != null && s.length()>0){ /* String[] ge = {"I", "II","III", "IV","V","VI","VII", "VIII","IX"}; String[] hen = {"X","XX",…
2015-03-03 22:20 · leetcode刷题 · 评论(0)
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) { String result = ""; String[] ge = {"","I", "II","III", "IV","V","VI","VII", "VIII","IX"}; String[] hen = {"","X","XX","XXX…
2015-03-01 23:35 · leetcode刷题 · 评论(0)
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) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most wate…
2015-03-01 22:55 · leetcode刷题 · 评论(0)
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 the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some…
2015-03-01 20:19 · leetcode刷题 · 评论(0)
Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断是否为回文数,比较简单,可以用两种方式做,一种是用java中的stringbuffer的一个反转函数,一种直接判断数字。后一种效率比较高。前者代码量少点。 1: public class Solution { public boolean isPalindrome(int x) { StringBuffer sb = new StringBuffer(); sb.append(x); StringBuffer sb1 = new Strin…
2015-02-13 22:08 · leetcode刷题 · 评论(0)
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 specified vaguely (ie, no given input specs). You ar…
2015-02-13 11:07 · leetcode刷题 · 评论(0)