文章列表
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) {
* ...
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:
"((()))", "(()())", "(())()", "()(())", "()()()"
这道题我是用递归的方式做的,用堆栈来判断是否复合条件,同时加上一个判断有多少左括号的函数。
publi ...
Valid Parentheses
- 博客分类:
- leetcode刷题
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 valid but "(]" and "([)]" are not.
这是一道匹配题,最简单的方式就是用栈来做了,JAVA中有Strac ...
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 thi ...
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: ["ad", "ae", " ...
4Sum
- 博客分类:
- leetcode刷题
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 con ...
3Sum Closest
- 博客分类:
- leetcode刷题
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 cl ...
3Sum
- 博客分类:
- leetcode刷题
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.
F ...
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].l ...
Roman to Integer
- 博客分类:
- leetcode刷题
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 ...
Integer to Roman
- 博客分类:
- leetcode刷题
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","II ...
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 water. ...
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)
So ...
Palindrome Number
- 博客分类:
- leetcode刷题
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);
Stri ...
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 ...