Skip to content

Commit b081da4

Browse files
committed
update
1 parent 25f1d63 commit b081da4

7 files changed

Lines changed: 305 additions & 1 deletion

‎_posts/2014-06-06-LeetCode目录.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ tags : [LeetCode,面试,目录]
2121
11. Longest Common Prefix
2222
12. 3Sum
2323
13. [3Sum Closet]()
24-
24+
14. 4Sum
25+
15. Letter Combinations of a Phone Number
26+
16. Remove Nth Node From End of List
2527

2628

2729

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "和最接近k的3个数"
5+
tags : [LeetCode,面试,和最接近k的n个数]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4 id="3Sum-Closest">3Sum Closest</h4>
10+
11+
>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.
12+
>
13+
>Note:
14+
>
15+
For example, given array S = {-1 2 1 -4}, and target = 1.
16+
>
17+
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
18+
19+
解法:和3Sum那题类似,这题还限制了很只有一个解。第一种解法,复杂度`O(n^2logn)`,先对数组进行排序,,确定前两个数,对第三个数使用二分查找。去重,因为已经排过序,相同的数只需确定搜索其中一个即可。
20+
21+
代码如下:
22+
23+
//{java}
24+
public class Solution {
25+
public int threeSumClosest(int[] num, int target) {
26+
Arrays.sort(num);
27+
int numl=num.length;
28+
int close = target-99999;
29+
for(int i=0;i<numl-2;i++)
30+
{
31+
for(int j=i+1;j<numl-1;j++)
32+
{
33+
int t = findcloseX(num,j+1,numl-1,target-num[i]-num[j]);
34+
close=closeX(num[i]+num[j]+t,close,target);
35+
}
36+
}
37+
return close;
38+
}
39+
40+
public int findcloseX(int num[],int s,int e,int x)
41+
{
42+
while(s+1<e)
43+
{
44+
int t = num[(s+e)/2];
45+
if(t>x)
46+
{
47+
e=(s+e)/2;
48+
}else if(t<x)
49+
{
50+
s=(s+e)/2;
51+
}
52+
else
53+
{
54+
return t;
55+
}
56+
}
57+
return closeX(num[s],num[e],x);
58+
}
59+
60+
public int closeX(int a,int b,int x)
61+
{
62+
return Math.abs(a-x)>Math.abs(b-x)?b:a;
63+
}
64+
}
65+
66+
第二种解法,在有序数组中求和最接近k的两个数是一个线性时间,算法的时间复杂度是`O(n)`,就是一个双向遍历过程。(**在数组中查找符合某一条件的元素,首先应该想到双向、二分等**)。
67+
68+
代码如下:
69+
70+
//{java}
71+
public class Solution {
72+
public int threeSumClosest(int[] num, int target) {
73+
Arrays.sort(num);
74+
int numl=num.length;
75+
int close = target-99999;
76+
for(int i=0;i<numl-2;i++)
77+
{
78+
int be=i+1, end=numl-1;
79+
int ta=target-num[i];
80+
while(be<end)
81+
{
82+
if(num[be]+num[end]>ta)
83+
{
84+
end--;
85+
}else if(num[be]+num[end]<ta)
86+
{
87+
be++;
88+
}
89+
else
90+
{
91+
return target;
92+
}
93+
close=closeX(num[i]+num[be+1]+num[be],close,target);
94+
}
95+
}
96+
return close;
97+
}
98+
99+
public int closeX(int a,int b,int x)
100+
{
101+
return Math.abs(a-x)>Math.abs(b-x)?b:a;
102+
}
103+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "括号所有组合"
5+
tags : [LeetCode,面试,组合穷举,括号组合,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4 id="Generate-Parentheses">Generate Parentheses</h4>
10+
11+
> Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
12+
>
13+
>For example, given n = 3, a solution set is:
14+
>
15+
"((()))", "(()())", "(())()", "()(())", "()()()"
16+
17+
18+
解法:穷举组合问题,使用递归。`(())(())`这种情况很容易漏掉。
19+
20+
代码如下:
21+
22+
//{java}
23+
public class Solution {
24+
public List<String> generateParenthesis(int n) {
25+
List<String> ls = new ArrayList<String>();
26+
if(n==0)
27+
{
28+
ls.add("");
29+
return ls;
30+
}
31+
Set<String> set = new HashSet<String>();
32+
for(String temp:generateParenthesis(n-1))
33+
{
34+
set.add("()"+temp);
35+
set.add(temp+"()");
36+
for(int i=0;i<temp.length();i++)
37+
{
38+
if(temp.charAt(i)=='(')
39+
{
40+
set.add(temp.substring(0,i+1)+"()"+temp.substring(i+1,temp.length()));
41+
}
42+
}
43+
}
44+
ls.addAll(set);
45+
return ls;
46+
}
47+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "字符组合"
5+
tags : [LeetCode,面试,字符,组合,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4 id="Letter-Combinations-of-a-Phone-Number">Letter Combinations of a Phone Number</h4>
10+
11+
>Given a digit string, return all possible letter combinations that the number could represent.
12+
>
13+
>A mapping of digit to letters (just like on the telephone buttons) is given below.
14+
>
15+
![picture](img/leetcode_LetterCombinationsOfaPhoneNumber.png)
16+
>
17+
Input:Digit string "23"
18+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
19+
20+
解法:字符组合问题,一般用递归都能很快解决。
21+
22+
代码如下:
23+
24+
//{java}
25+
public class Solution {
26+
public List<String> letterCombinations(String digits) {
27+
String []table = new String []{""," ","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
28+
List<String> ls= new ArrayList<String>();
29+
if(digits.length()==0)
30+
{
31+
ls.add("");
32+
return ls;
33+
}
34+
char c = digits.charAt(0);
35+
36+
for(int i=0;i<table[c-48].length();i++)
37+
{
38+
char t = table[c-48].charAt(i);
39+
for(String temp:letterCombinations(digits.substring(1,digits.length())))
40+
{
41+
ls.add(t+temp);
42+
}
43+
}
44+
return ls;
45+
}
46+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "删除链表中的倒数第k个元素"
5+
tags : [LeetCode,面试,链表,元素删除,倒数]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4 id="Remove-Nth-Node-From-End-of-List">Remove Nth Node From End of List</h4>
10+
11+
>Given a linked list, remove the nth node from the end of list and return its head.
12+
>
13+
>For example,
14+
>
15+
Given linked list: 1->2->3->4->5, and n = 2.
16+
>
17+
After removing the second node from the end, the linked list becomes 1->2->3->5.
18+
>
19+
>Note:
20+
Given n will always be valid.
21+
Try to do this in one pass.
22+
23+
解法:一般的解法都是先计算出数组长度。这里要求只能扫描数组一次,那就用两个标记,让一个标记先走k步,其实性能没有区别。
24+
25+
代码如下:
26+
27+
//{java}
28+
/**
29+
* Definition for singly-linked list.
30+
* public class ListNode {
31+
* int val;
32+
* ListNode next;
33+
* ListNode(int x) {
34+
* val = x;
35+
* next = null;
36+
* }
37+
* }
38+
*/
39+
public class Solution {
40+
public ListNode removeNthFromEnd(ListNode head, int n) {
41+
ListNode p = head;
42+
ListNode q = head;
43+
int k=0;
44+
while(p!=null)
45+
{
46+
if(k>n)
47+
q=q.next;
48+
p=p.next;
49+
k++;
50+
}
51+
if(k<=n)
52+
return head.next;
53+
q.next=q.next.next;
54+
55+
return head;
56+
}
57+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "括号合法问题"
5+
tags : [LeetCode,面试,栈,括号合法]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4 id="Valid-Parentheses">Valid Parentheses</h4>
10+
11+
>Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
12+
>
13+
>The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
14+
15+
解法:用栈存左括号,当匹配到右括号时弹出,如匹配不到,则结束返回错误。注意如果直接一个`右括号`时,栈顶没有元素。
16+
17+
代码如下:
18+
19+
//{java}
20+
public class Solution {
21+
public boolean isValid(String s) {
22+
char stack[] = new char[10000];
23+
int top=0;
24+
for(int i=0;i<s.length();i++)
25+
{
26+
char c = s.charAt(i);
27+
if(c=='('||c=='{'||c=='[')
28+
stack[top++]=c;
29+
else
30+
{
31+
if(top==0||stack[top-1]!=other(c))
32+
return false;
33+
else
34+
top--;
35+
}
36+
}
37+
return top==0;
38+
}
39+
public char other(char c)
40+
{
41+
if(c==')')
42+
return '(';
43+
if(c==']')
44+
return '[';
45+
if(c=='}')
46+
return '{';
47+
return 0;
48+
}
49+
}
24.8 KB
Loading

0 commit comments

Comments
 (0)