Skip to content

Commit 7735759

Browse files
committed
initial commit
1 parent 32f067f commit 7735759

File tree

134 files changed

+7036
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+7036
-0
lines changed

AddBinary.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
public class AddBinary {
2+
public String addBinary(String a, String b) {
3+
String l = a.length() > b.length() ? a : b;// long string
4+
String s = l == a ? b : a;// short string
5+
int carry = 0;
6+
StringBuilder sb = new StringBuilder();
7+
for (int i = l.length() - 1, j = s.length() - 1; j >= 0; i--, j--) {
8+
Character cl = l.charAt(i);
9+
Character cs = s.charAt(j);
10+
if (cl == cs && cl == '0') {
11+
sb.append(0 + carry);
12+
carry = 0;
13+
} else if (cl == cs && cl == '1') {
14+
sb.append(0 + carry);
15+
carry = 1;
16+
} else if (cl != cs && carry == 0) {
17+
sb.append(1);
18+
} else if (cl != cs && carry == 1) {
19+
sb.append(0);
20+
carry = 1;
21+
}
22+
}
23+
24+
for (int i = l.length() - s.length() - 1; i >= 0; i--) {
25+
Character cl = l.charAt(i);
26+
if (cl == '0' && carry == 1) {
27+
sb.append(1);
28+
carry = 0;
29+
} else if (cl == '1' && carry == 1) {
30+
sb.append(0);
31+
carry = 1;
32+
} else {
33+
sb.append(cl);
34+
}
35+
}
36+
if(carry==1){
37+
sb.append(1);
38+
}
39+
return sb.reverse().toString();
40+
}
41+
42+
43+
public static void main(String[] args){
44+
String c = "111";
45+
String d = "10";
46+
AddBinary o = new AddBinary();
47+
String result = o.addBinary(c, d);
48+
System.out.println("result "+result);
49+
50+
51+
}
52+
}

AddTwoNumbersInLinkedList.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class ListNode {
2+
int val;
3+
ListNode next;
4+
5+
ListNode(int v) {
6+
val = v;
7+
next = null;
8+
}
9+
}
10+
11+
public class AddTwoNumbersInLinkedList {
12+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13+
int carry = 0;
14+
int value = 0;
15+
ListNode head;
16+
ListNode temp;
17+
ListNode node;
18+
if (l1 == null || l2 == null) {
19+
return null;
20+
}
21+
value = l1.val + l2.val;
22+
head = new ListNode(value % 10);
23+
node = head;
24+
carry = value / 10;
25+
l1 = l1.next;
26+
l2 = l2.next;
27+
28+
while (l1 != null || l2 != null) {
29+
value = 0;
30+
if (l1 != null) {
31+
value += l1.val;
32+
l1 = l1.next;
33+
}
34+
if (l2 != null) {
35+
value += l2.val;
36+
l2 = l2.next;
37+
}
38+
value += carry;
39+
carry = value / 10;
40+
temp = new ListNode(value % 10);
41+
node.next = temp;
42+
node = node.next;
43+
}
44+
if(carry != 0){
45+
temp = new ListNode(carry);
46+
node.next = temp;
47+
}
48+
49+
return head;
50+
}
51+
public void printListNode(ListNode head){
52+
while(head!=null){
53+
System.out.print(head.val+"=>");
54+
head = head.next;
55+
}
56+
}
57+
58+
public static void main(String[] args){
59+
AddTwoNumbersInLinkedList o = new AddTwoNumbersInLinkedList();
60+
ListNode l1 = new ListNode(1);
61+
ListNode head = l1;
62+
for(int i = 2;i<5;i++){
63+
ListNode l = new ListNode(i);
64+
l1.next = l;
65+
l1=l1.next;
66+
}
67+
o.printListNode(head);
68+
System.out.println();
69+
ListNode l2 = new ListNode(1);
70+
ListNode head2 = l2;
71+
for(int i = 2;i<4;i++){
72+
ListNode l22 = new ListNode(i);
73+
l2.next = l22;
74+
l2=l2.next;
75+
}
76+
o.printListNode(head2);
77+
System.out.println();
78+
79+
ListNode result = o.addTwoNumbers(head, head2);
80+
o.printListNode(result);
81+
82+
}
83+
84+
}

Anagram.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.*;
2+
3+
public class Anagram {
4+
public ArrayList<String> anagrams(String[] strs) {
5+
ArrayList<String> result = new ArrayList<String>();
6+
HashMap<String, LinkedList<String>> map = new HashMap<String, LinkedList<String>>();
7+
LinkedList<String> l = new LinkedList<String>();
8+
for (String s : strs) {
9+
String si = inorderString(s);
10+
if (!map.containsKey(si)) {
11+
l = new LinkedList<String>();
12+
13+
} else {
14+
l = map.get(si);
15+
}
16+
l.push(s);
17+
map.put(si, l);
18+
System.out.println("si"+si+" l "+l);
19+
}
20+
21+
System.out.println("key:"+map.keySet());
22+
for (String si : map.keySet()) {
23+
System.out.println("si "+si);
24+
l = map.get(si);
25+
if (l.size() > 1) {
26+
for (String s : l) {
27+
result.add(s);
28+
}
29+
}
30+
}
31+
return result;
32+
33+
}
34+
35+
public String inorderString(String str) {
36+
char[] ch = str.toCharArray();//string => char array
37+
Arrays.sort(ch);
38+
System.out.println("ch:"+ch.toString());
39+
return new String(ch);//char array => string
40+
}
41+
42+
public static void main(String[] args) {
43+
ArrayList<String> result;
44+
String[] st = { "abc", "bca", "a", "bb", "ac", "ca", "" };
45+
Anagram a = new Anagram();
46+
result = a.anagrams(st);
47+
System.out.println(result);
48+
}
49+
50+
}

BalancedBianryTree.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
3+
4+
5+
6+
public class BalancedBianryTree {
7+
public boolean isBalanced(TreeNode root) {
8+
// Start typing your Java solution below
9+
// DO NOT write main() function
10+
if(root==null){
11+
return true;
12+
}
13+
int diff = Math.abs(getHeight(root.left)-getHeight(root.right));
14+
if(diff>1){
15+
return false;
16+
}
17+
return isBalanced(root.left)||isBalanced(root.right);
18+
19+
}
20+
public int getHeight(TreeNode root){
21+
if(root==null){
22+
return 0;
23+
}
24+
int leftHeight = getHeight(root.left);
25+
int rightHeight = getHeight(root.right);
26+
return Math.max(leftHeight,rightHeight)+1;
27+
}
28+
29+
30+
}

BestTimeToBuyAndSellStock.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
5+
*/
6+
public class BestTimeToBuyAndSellStock {
7+
public int maxProfit(int[] prices) {
8+
// Start typing your Java solution below
9+
// DO NOT write main() function
10+
if (prices.length == 0 || prices.length == 1) {
11+
return 0;
12+
}
13+
int max = 0;
14+
for (int i = 1; i < prices.length; i++) {
15+
for (int j = 0; j < i; j++) {
16+
if (prices[i] > prices[j]) {
17+
int value = prices[i] - prices[j];
18+
max = Math.max(max, value);
19+
}
20+
}
21+
}
22+
23+
return max;
24+
}
25+
}

BestTimeToBuyAndSellStockII.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
5+
*/
6+
7+
//the problem is actually to find all non-decreasing continuous subsequence
8+
9+
public class BestTimeToBuyAndSellStockII {
10+
public int maxProfit(int[] prices) {
11+
// Start typing your Java solution below
12+
// DO NOT write main() function
13+
if (prices.length == 0 || prices.length == 1) {
14+
return 0;
15+
}
16+
int value = 0;
17+
for (int i = 0; i < prices.length - 1; i++) {
18+
if (prices[i] <= prices[i + 1]) {
19+
value += prices[i + 1] - prices[i];
20+
}
21+
}
22+
return value;
23+
24+
}
25+
}

BestTimeToBuyAndSellStockIII.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Say you have an array for which the ith element is the price of a given stock on day i.
3+
4+
Design an algorithm to find the maximum profit. You may complete at most two transactions.
5+
6+
Note:
7+
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
8+
*/
9+
//idea comes from peking2 of Mitbbs
10+
// use two array dp1 and dp2 to traversal from both end
11+
12+
public class BestTimeToBuyAndSellStockIII {
13+
public int maxProfit(int[] prices) {
14+
// Start typing your Java solution below
15+
// DO NOT write main() function
16+
if (prices.length == 0 || prices.length == 1) {
17+
return 0;
18+
}
19+
int len = prices.length;
20+
int[] dp1 = new int[prices.length];
21+
int[] dp2 = new int[prices.length];
22+
int min = prices[0];
23+
int max = prices[len - 1];
24+
25+
// for dp1: travel from left to right
26+
System.out.print(" " + dp1[0]);
27+
28+
for (int i = 1; i < len; i++) {
29+
min = Math.min(min, prices[i]);
30+
dp1[i] = Math.max(dp1[i - 1], prices[i] - min);
31+
System.out.print(" " + dp1[i]);
32+
}
33+
System.out.println();
34+
// for dp2: travel from right to left
35+
System.out.print(" " + dp2[len - 1]);
36+
for (int j = len - 2; j >= 0; j--) {
37+
max = Math.max(max, prices[j]);
38+
dp2[j] = Math.max(dp2[j + 1], max - prices[j]);
39+
System.out.print(" " + dp2[j]);
40+
}
41+
System.out.println();
42+
int ans = 0;
43+
for (int k = 0; k < len; k++) {
44+
ans = Math.max(ans, dp1[k] + dp2[k]);// You may not engage in
45+
// multiple transactions at
46+
// the same time
47+
}
48+
return ans;
49+
50+
}
51+
52+
public static void main(String[] args) {
53+
BestTimeToBuyAndSellStockIII o = new BestTimeToBuyAndSellStockIII();
54+
int[] prices = { 2, 1, 2, 1, 0, 1, 2 };
55+
System.out.println(o.maxProfit(prices));
56+
}
57+
}

0 commit comments

Comments
 (0)