Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfde517c

Browse files
author
applewjg
committed
Add
Change-Id: Id40fa127b22fda2949a457e6fa7679ee941e0b13
1 parent433387e commitfde517c

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

‎CombinationSum.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Author: Annie Kim, anniekim.pku@gmail.com
3+
Date: May 25, 2013
4+
Problem: Combination Sum
5+
Difficulty: Easy
6+
Source: http://leetcode.com/onlinejudge#question_39
7+
Notes:
8+
Given a set of candidate numbers (C) and a target number (T), find all unique
9+
combinations in C where the candidate numbers sums to T.
10+
The same repeated number may be chosen from C unlimited number of times.
11+
Note:
12+
All numbers (including target) will be positive integers.
13+
Elements in a combination (a1, a2, .. , ak) must be in non-descending order. (ie, a1 <= a2 <= ... <= ak).
14+
The solution set must not contain duplicate combinations.
15+
For example, given candidate set 2,3,6,7 and target 7,
16+
A solution set is:
17+
[7]
18+
[2, 2, 3]
19+
20+
Solution: Sort & Recursion.
21+
*/
22+
23+
publicclassSolution {
24+
publicList<List<Integer>>combinationSum(int[]candidates,inttarget) {
25+
List<List<Integer>>res =newArrayList<List<Integer>>();
26+
Arrays.sort(candidates);
27+
ArrayList<Integer>path =newArrayList<Integer>();
28+
combinationSumRe(candidates,target,0,path,res);
29+
returnres;
30+
}
31+
voidcombinationSumRe(int[]candidates,inttarget,intstart,ArrayList<Integer>path,List<List<Integer>>res) {
32+
if (target ==0) {
33+
ArrayList<Integer>p =newArrayList<Integer>(path);
34+
res.add(p);
35+
return;
36+
}
37+
for (inti =start;i <candidates.length &&target >=candidates[i]; ++i) {
38+
path.add(candidates[i]);
39+
combinationSumRe(candidates,target-candidates[i],i,path,res);
40+
path.remove(path.size() -1);
41+
}
42+
}
43+
}

‎MergeIntervals.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Merge Intervals
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/merge-intervals/
7+
Notes:
8+
Given a collection of intervals, merge all overlapping intervals.
9+
For example,
10+
Given [1,3],[2,6],[8,10],[15,18],
11+
return [1,6],[8,10],[15,18].
12+
13+
Solution: 1. Sort in ascending order of 'start'.
14+
2. Traverse the 'intervals', merge or push...
15+
*/
16+
17+
/**
18+
* Definition for an interval.
19+
* public class Interval {
20+
* int start;
21+
* int end;
22+
* Interval() { start = 0; end = 0; }
23+
* Interval(int s, int e) { start = s; end = e; }
24+
* }
25+
*/
26+
publicclassSolution {
27+
publicList<Interval>merge(List<Interval>intervals) {
28+
Comparator<Interval>comp =newComparator<Interval>(){
29+
publicintcompare(Intervala,Intervalb) {
30+
if(a.start <b.start) {
31+
return -1;
32+
}elseif(a.start >b.start){
33+
return1;
34+
}else {
35+
if (a.end <b.end)return -1;
36+
elseif (a.end >b.end)return1;
37+
return0;
38+
}
39+
}
40+
};
41+
ArrayList<Interval>res =newArrayList<Interval>();
42+
intN =intervals.size();
43+
if (N <=1)returnintervals;
44+
Collections.sort(intervals,comp);
45+
Intervallast =intervals.get(0);
46+
for (inti =0;i <N; ++i) {
47+
if (intervals.get(i).start >last.end) {
48+
res.add(last);
49+
last =intervals.get(i);
50+
}else {
51+
last.end =Math.max(last.end,intervals.get(i).end);
52+
}
53+
}
54+
res.add(last);
55+
returnres;
56+
}
57+
}

‎MergekSortedLists.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
Solution: Find the smallest list-head first using minimum-heap(lgk).
1111
complexity: O(N*KlgK)
1212
*/
13-
14-
/**
15-
* Definition for singly-linked list.
16-
* struct ListNode {
17-
* int val;
18-
* ListNode *next;
19-
* ListNode(int x) : val(x), next(NULL) {}
20-
* };
21-
*/
2213
/**
2314
* Definition for singly-linked list.
2415
* public class ListNode {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp