Skip to content

Commit 4956af7

Browse files
committed
20180604
1 parent 817b941 commit 4956af7

25 files changed

+307
-2052
lines changed

.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
test/target/*
2-
test/.idea/*
3-
/.idea/*
1+
target/*
2+
.idea/*
43

5-
test/.idea/*
6-
test/out/*
4+
out/*
75
*.xml
86
*.iml
97

algorithm.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/test/src" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
78
</content>
89
<orderEntry type="inheritedJdk" />
910
<orderEntry type="sourceFolder" forTests="false" />
Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,4 @@
1-
<<<<<<< HEAD
2-
package leetcode.dfsbacktracing;
3-
4-
import java.util.ArrayList;
5-
import java.util.HashSet;
6-
import java.util.List;
7-
8-
/**
9-
* Created by fay on 2017/12/12.
10-
* <p>
11-
* Given a collection of numbers that might contain duplicates, return all possible unique permutations.
12-
* For example,
13-
* [1,1,2]have the following unique permutations:
14-
* [1,1,2],[1,2,1], and[2,1,1].
15-
*/
16-
public class Permutations2 {
17-
public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
18-
HashSet<ArrayList<Integer>> res = new HashSet<>();
19-
int level = 0;
20-
backtracking(num, level,num.length-1,res);
21-
//Set转换成List
22-
ArrayList<ArrayList<Integer>> result=new ArrayList<>();
23-
result.addAll(res);
24-
return result;
25-
}
26-
private void backtracking(int[] num, int level, int n,HashSet<ArrayList<Integer>> result) {
27-
if (level == n) {
28-
ArrayList<Integer> res=new ArrayList<>();
29-
for(int i:num){
30-
res.add(i);
31-
}
32-
result.add(res);
33-
return;
34-
}
35-
for (int i = level; i <= n; i++) {
36-
if(i!=level&&num[i]==num[level]){
37-
continue;
38-
}
39-
swap(num, level, i);
40-
backtracking(num, level + 1, n, result);
41-
swap(num, level, i);
42-
}
43-
}
44-
public void swap(int[] num, int i, int j) {
45-
int temp = num[i];
46-
num[i] = num[j];
47-
num[j] = temp;
48-
}
49-
public static void main(String[] args) {
50-
Permutations2 test = new Permutations2();
51-
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
52-
int[] num = {1, 1, 2,2};
53-
/*java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
54-
List res=Arrays.asList(num);
55-
result.add((ArrayList) res);
56-
List res = Arrays.asList(num);
57-
result.add(new ArrayList<Integer>(res));*/
58-
/* ArrayList<ArrayList<String>> stuff = new ArrayList<ArrayList<String>>();
59-
String[] titles = {"ticker", "grade", "score"};
60-
stuff.add(new ArrayList<String>(Arrays.asList(titles)));*/
61-
result=test.permuteUnique(num);
62-
for (ArrayList<Integer> res : result) {
63-
for (Integer i : res) {
64-
System.out.print(i + " ");
65-
}
66-
System.out.println();
67-
}
68-
}
69-
}
70-
=======
1+
712
package leetcode.dfsbacktracing;
723

734
import java.util.ArrayList;
@@ -135,4 +66,3 @@ public static void main(String[] args) {
13566
}
13667
}
13768
}
138-
>>>>>>> 66d83aa3e6ef68f1f21311bddb82a3035d08cb85
Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,6 @@
1-
<<<<<<< HEAD
2-
package leetcode.list;
3-
/**/
4-
public class AddTwoNumbers {
5-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
6-
if(l1==null||l2==null){
7-
return null;
8-
}
9-
ListNode dummy=new ListNode(-1);
10-
ListNode curr=dummy;
11-
int carry=0;
12-
int num=0;
13-
while(l1!=null&&l2!=null){
14-
num=l1.val+l2.val+carry;
15-
carry=num>=10?1:0;
16-
num=num>=10?num-10:num;
17-
curr.next=new ListNode(num);
18-
curr=curr.next;
19-
l1=l1.next;
20-
l2=l2.next;
21-
}
22-
while(l1!=null){
23-
num=l1.val+carry;
24-
carry=num>=10?1:0;
25-
num=num>=10?num-10:num;
26-
curr.next=new ListNode(num);
27-
curr=curr.next;
28-
l1=l1.next;
29-
}
30-
while(l2!=null){
31-
num=l2.val+carry;
32-
carry=num>=10?1:0;
33-
num=num>=10?num-10:num;
34-
curr.next=new ListNode(num);
35-
curr=curr.next;
36-
l2=l2.next;
37-
}
38-
if(carry!=0){
39-
curr.next=new ListNode(carry);
40-
}
41-
return dummy.next;
42-
}
43-
}
44-
=======
451
package leetcode.list;
2+
import utils.*;
3+
464
/*
475
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 linked list.
486
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
@@ -88,4 +46,3 @@ public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
8846
return dummy.next;
8947
}
9048
}
91-
>>>>>>> 66d83aa3e6ef68f1f21311bddb82a3035d08cb85
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode.sort;
2+
3+
public class BubbleSort {
4+
public int[] bubbleSort(int[] arr){
5+
for(int i=0;i<arr.length;i++){
6+
for(int j=i+1;j<arr.length;j++){
7+
if(arr[i]>arr[j]){
8+
int temp=arr[i];
9+
arr[i]=arr[j];
10+
arr[j]=temp;
11+
}
12+
}
13+
}
14+
return arr;
15+
}
16+
public static void main(String[] args) {
17+
int[] arr = {49, 38, 65, 97, 76, 13, 27, 49};
18+
BubbleSort qs = new BubbleSort();
19+
arr = qs.bubbleSort(arr);
20+
for (int num : arr) {
21+
System.out.println(num);
22+
23+
}
24+
}
25+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package leetcode.sort;
2+
3+
public class MergeSort {
4+
public void merge(int[] arr,int[] temp,int i,int l1,int l2){
5+
int k,j;
6+
for(k=i, j=l1+1;i<=l1&&j<=l2; k++){
7+
if(arr[i]<=arr[j]){
8+
temp[k]=arr[i++];
9+
}else{
10+
temp[k]=arr[j++];
11+
}
12+
}
13+
if(i<=l1){
14+
temp[k++]=arr[i++];
15+
}
16+
if(j<=l2){
17+
temp[k++]=arr[j++];
18+
}
19+
}
20+
public void mergeSort(int[] arr,int[] result,int start,int end){
21+
if(start==end){
22+
result[start]=arr[start];
23+
}else{
24+
int m=(start+end)/2;
25+
mergeSort(arr,result,start,m);
26+
mergeSort(arr,result,m+1,end);
27+
merge(arr,result,start,m,end);
28+
}
29+
}
30+
31+
public static void mergeSort1(int[] arr,int p,int r){
32+
if(p<r){
33+
int q=(p+r)/2;
34+
mergeSort1(arr,p,q);
35+
mergeSort1(arr,q+1,r);
36+
merge1(arr,p,q,r);
37+
38+
39+
}
40+
}
41+
42+
public static void merge1(int[] arr,int p,int q,int r){
43+
int n1=q-p+1;
44+
int n2=r-q;
45+
int [] arr1=new int [n1+1];
46+
int [] arr2=new int [n2+1];
47+
48+
for(int i=0;i<n1;i++){
49+
arr1[i]=arr[p+i];
50+
}
51+
for(int j=0;j<n2;j++){
52+
arr2[j]=arr[q+1+j];
53+
}
54+
arr1[n1]=Integer.MAX_VALUE;
55+
arr2[n2]=Integer.MAX_VALUE;
56+
int i=0;
57+
int j=0;
58+
for(int k=p;k<=r;k++){
59+
if(arr1[i]<arr2[j]){
60+
arr[k]=arr1[i];
61+
i++;
62+
}else{
63+
arr[k]=arr2[j];
64+
j++;
65+
}
66+
}
67+
}
68+
public static void main(String[] args) {
69+
int[] arr = {49, 38, 65, 97, 76, 13, 27, 49};
70+
int[] result=new int[arr.length];
71+
MergeSort qs = new MergeSort();
72+
qs.mergeSort1(arr, 0, arr.length-1 );
73+
for (int num : arr) {
74+
System.out.println(num);
75+
76+
}
77+
}
78+
79+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode.sort;
2+
3+
public class QuikSort {
4+
int partition(int[] arr, int low, int high) {
5+
int base = arr[low];
6+
7+
while (low < high) {
8+
while (low < high && arr[high] >= base) {
9+
high--;
10+
}
11+
int temp = arr[low];
12+
arr[low] = arr[high];
13+
arr[high] = temp;
14+
while (low < high && arr[low] <= base) {
15+
low++;
16+
}
17+
temp = arr[high];
18+
arr[high] = arr[low];
19+
arr[low] = temp;
20+
}
21+
return low;
22+
}
23+
24+
int[] quickSort(int[] arr, int low, int high) {
25+
if (low <high) {
26+
int base = partition(arr, low, high);
27+
quickSort(arr, 0, base - 1);
28+
quickSort(arr, base + 1, high);
29+
}
30+
31+
return arr;
32+
}
33+
34+
public static void main(String[] args) {
35+
int[] arr = {49, 38, 65, 97, 76, 13, 27,49};
36+
QuikSort qs = new QuikSort();
37+
arr = qs.quickSort(arr, 0, arr.length - 1);
38+
for (int num : arr) {
39+
System.out.println(num);
40+
41+
}
42+
43+
}
44+
45+
}

0 commit comments

Comments
 (0)