Skip to content

Commit 892c452

Browse files
author
yangjunbao
committed
init 工程,提交几个简单算法。
0 parents  commit 892c452

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

src/com/apptao/leetcode/APlusB.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.apptao.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by a on 2017-12-25.
8+
*/
9+
public class APlusB {
10+
11+
12+
public static void main(String args[]){
13+
System.out.println("aplusb" + "----------------------------------------------");
14+
int aplusb = aplusb(3, 5);
15+
System.out.println(aplusb + "");
16+
17+
System.out.println("twoSum" + "----------------------------------------------");
18+
int[] twoSum = twoSum(new int[]{2, 3, 5, 7, 11, 12, 15}, 20);
19+
for (int i = 0; i < twoSum.length; i++) {
20+
System.out.println(twoSum[i]);
21+
}
22+
}
23+
24+
/** aplusb 不用运算符+ commit
25+
* @param a
26+
* @param b
27+
* @return
28+
*/
29+
private static int aplusb(int a, int b) {
30+
31+
while (b!=0) {
32+
int tempA = a ^ b;
33+
int tempB = (a & b) << 1;
34+
a = tempA;
35+
b = tempB;
36+
37+
}
38+
return a;
39+
}
40+
41+
42+
/**
43+
* 给定数组,给定目标,计算数组中有没有相加的和为目标值;
44+
* @param nums
45+
* @param target
46+
* @return
47+
*/
48+
public static int[] twoSum(int[] nums, int target) {
49+
//map 存放 运算值
50+
Map<Integer, Integer> map = new HashMap<>();
51+
for (int i = 0; i<nums.length; i ++) {
52+
//反向计算获取 数组中的值
53+
int complement = target - nums[i];
54+
55+
if (map.containsKey(complement)) {
56+
return new int[]{map.get(complement), i};
57+
}
58+
59+
map.put(nums[i], i);
60+
61+
}
62+
63+
throw new IllegalArgumentException("no two sum solution");
64+
65+
66+
}
67+
68+
69+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.apptao.leetcode;
2+
3+
/**
4+
* Created by a on 2017-12-25.
5+
*/
6+
public class AddTwoNumbers {
7+
public class ListNode {
8+
int val;
9+
ListNode next;
10+
11+
ListNode(int x) {
12+
val = x;
13+
}
14+
}
15+
16+
public static void main(String[] args) {
17+
18+
}
19+
20+
private static ListNode addTwoNumbers(ListNode L1, ListNode L2) {
21+
22+
return null;
23+
24+
}
25+
26+
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.apptao.leetcode;
2+
3+
/**
4+
* Created by a on 2017-12-25.
5+
*/
6+
public class BubbleSort
7+
{
8+
public static void main(String[] args) {
9+
int[] ints = {3, 4, 56, 12, 32, 453, 23, 44, 32, 11, 1};
10+
bubblesort(ints);
11+
}
12+
13+
public static void bubblesort(int[] arr) {
14+
15+
int length = arr.length;
16+
int i, temp;
17+
boolean changed;
18+
do {
19+
changed = false;
20+
for (i = 0; i < length - 1; i++) {
21+
if (arr[i] > arr[i + 1]) {
22+
temp = arr[i];
23+
arr[i] = arr[i + 1];
24+
arr[i + 1] = temp;
25+
changed = true;
26+
}
27+
}
28+
} while (changed);
29+
for (int j = 0; j < arr.length; j++) {
30+
System.out.println(arr[j]);
31+
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)