Skip to content

Commit d0f18b5

Browse files
committed
增加代码
1 parent 552d775 commit d0f18b5

File tree

64 files changed

+6803
-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.

64 files changed

+6803
-0
lines changed

code/LeetCode/src/AddBinary.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
public class AddBinary {
2+
/*
3+
Add Binary
4+
Description
5+
Given two binary strings, return their sum (also a binary string).
6+
7+
The input strings are both non-empty and contains only characters 1 or 0.
8+
9+
Example 1:
10+
11+
Input: a = "11", b = "1"
12+
Output: "100"
13+
Example 2:
14+
15+
Input: a = "1010", b = "1011"
16+
Output: "10101"
17+
Tags: Math, String
18+
*/
19+
20+
/*
21+
思路
22+
题意是给你两个二进制串,求其和的二进制串。我们就按照小学算数那么来做,用 carry 表示进位,从后往前算,依次往前,
23+
每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
24+
*/
25+
26+
public static String addBinary(String a, String b) {
27+
StringBuilder sb = new StringBuilder();
28+
int carray = 0;
29+
int aLastIndex = a.length() - 1;
30+
int bLastIndex = b.length() - 1;
31+
while (aLastIndex >= 0 && bLastIndex >= 0) {
32+
carray = carray + (aLastIndex >= 0 ? a.charAt(aLastIndex--) - '0' : 0);
33+
carray = carray + (bLastIndex >= 0 ? b.charAt(bLastIndex--) - '0' : 0);
34+
sb.insert(0, (char)(carray % 2 + '0'));
35+
carray = carray >> 1;
36+
}
37+
while (aLastIndex >= 0) {
38+
carray = carray + (aLastIndex >= 0 ? a.charAt(aLastIndex--) - '0' : 0);
39+
sb.insert(0, (char)(carray % 2 + '0'));
40+
carray = carray >> 1;
41+
}
42+
while (bLastIndex >= 0) {
43+
carray = carray + (bLastIndex >= 0 ? b.charAt(bLastIndex--) - '0' : 0);
44+
sb.insert(0, (char)(carray % 2 + '0'));
45+
carray = carray >> 1;
46+
}
47+
if (carray > 0) {
48+
sb.insert(0, '1');
49+
}
50+
51+
return sb.toString();
52+
}
53+
54+
public static void main(String[] args) {
55+
String a1 = "11";
56+
String b1 = "1";
57+
System.out.println(a1+ " + " +b1+ " = " + addBinary(a1, b1));
58+
String a2 = "1010";
59+
String b2 = "1011";
60+
System.out.println(a2+ " + " +b2+ " = " + addBinary(a2, b2));
61+
}
62+
}

code/LeetCode/src/AddTwoNumbers.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
public class AddTwoNumbers {
2+
/*
3+
Add Two Numbers
4+
Description
5+
You are given two non-empty linked lists representing two non-negative integers. 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.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
Example
10+
11+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
12+
Output: 7 -> 0 -> 8
13+
Explanation: 342 + 465 = 807.
14+
Tags: Linked List, Math
15+
*/
16+
17+
/*
18+
思路
19+
题意是以链表表示一个数,低位在前,高位在后,所以题中的例子就是 342 + 465 = 807,所以我们模拟计算即可。
20+
*/
21+
22+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
23+
ListNode resultNode = new ListNode(0);
24+
ListNode headNode = resultNode;
25+
ListNode node1 = l1;
26+
ListNode node2 = l2;
27+
int sum = 0;
28+
while (node1 != null || node2 != null) {
29+
sum = sum / 10;
30+
if (node1 != null) {
31+
sum += node1.val;
32+
node1 = node1.next;
33+
}
34+
if (node2 != null) {
35+
sum += node2.val;
36+
node2 = node2.next;
37+
}
38+
resultNode.next = new ListNode(sum % 10);
39+
resultNode = resultNode.next;
40+
}
41+
if (sum % 10 != 0) {
42+
resultNode.next = new ListNode(1);
43+
}
44+
45+
return headNode.next;
46+
}
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
public class BalancedBinaryTree {
2+
/*
3+
Balanced Binary Tree
4+
Description
5+
Given a binary tree, determine if it is height-balanced.
6+
7+
For this problem, a height-balanced binary tree is defined as:
8+
9+
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
10+
11+
Example 1:
12+
13+
Given the following tree [3,9,20,null,null,15,7]:
14+
15+
3
16+
/ \
17+
9 20
18+
/ \
19+
15 7
20+
Return true. Example 2:
21+
22+
Given the following tree [1,2,2,3,3,null,null,4,4]:
23+
24+
1
25+
/ \
26+
2 2
27+
/ \
28+
3 3
29+
/ \
30+
4 4
31+
Return false.
32+
33+
Tags: Tree, Depth-first Search
34+
*/
35+
36+
/*
37+
思路
38+
题意是判断一棵二叉树是否是高度平衡的,所谓二叉树高度平衡指的是二叉树的每个节点的两棵子树的高度差都不超过 1,
39+
那么我们只需计算左右子树的高度,判断其高度差是否不超过 1 即可,
40+
如果超过 1,就代表其不是高度平衡的,立即返回不是即可,我这里用返回 -1 代表不是。
41+
*/
42+
43+
public boolean isBalaced(TreeNode root) {
44+
return helper(root) != -1;
45+
}
46+
47+
public int helper(TreeNode node) {
48+
if (node == null) {
49+
return 0;
50+
}
51+
int leftInt = helper(node.left);
52+
if (leftInt == -1) {
53+
return -1;
54+
}
55+
int rightInt = helper(node.right);
56+
if (rightInt == -1) {
57+
return -1;
58+
}
59+
if (Math.abs(leftInt - rightInt) > 1) {
60+
return -1;
61+
}
62+
return 1 + Math.max(leftInt, rightInt);
63+
}
64+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
public class BestTimeToBuyAndSellStock {
2+
/*
3+
Best Time to Buy and Sell Stock
4+
Description
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
8+
9+
Note that you cannot sell a stock before you buy one.
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 5
15+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
16+
Not 7-1 = 6, as selling price needs to be larger than buying price.
17+
Example 2:
18+
19+
Input: [7,6,4,3,1]
20+
Output: 0
21+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
22+
Tags: Array, Dynamic Programmin
23+
*/
24+
25+
/*
26+
思路
27+
题意是给出一个数组代表每天的股票金额,让你在最多买卖一次的情况下算出最大的收益额,
28+
最简单的就是模拟即可,每次记录当前值减去最小值的差值,与上一次的进行比较然后更新最大值即可。
29+
*/
30+
public int maxProfit(int[] prices) {
31+
int maxProfitInt = 0;
32+
int minPriceInt = Integer.MAX_VALUE;
33+
for (int item: prices) {
34+
if (item < minPriceInt) {
35+
minPriceInt = item;
36+
}
37+
int gap = item - minPriceInt;
38+
if (gap > maxProfitInt) {
39+
maxProfitInt = gap;
40+
}
41+
}
42+
43+
return maxProfitInt;
44+
}
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
public class BestTimeToBuyAndSellStockII {
2+
/*
3+
Best Time to Buy and Sell Stock II
4+
Description
5+
Say you have an array for which the ith element is the price of a given stock on day i.
6+
7+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
8+
9+
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
10+
11+
Example 1:
12+
13+
Input: [7,1,5,3,6,4]
14+
Output: 7
15+
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
16+
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
17+
Example 2:
18+
19+
Input: [1,2,3,4,5]
20+
Output: 4
21+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
22+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
23+
engaging multiple transactions at the same time. You must sell before buying again.
24+
Example 3:
25+
26+
Input: [7,6,4,3,1]
27+
Output: 0
28+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
29+
Tags: Array, Greedy
30+
*/
31+
32+
/*
33+
思路
34+
题意是给出一个数组代表每天的股票金额,在每天只能买或卖的情况下求出收益最高值,
35+
这...,这也太简单了吧,把所有相邻递增的值都加起来即可。
36+
实际上为greedy 贪婪算法,分析情况:
37+
1、如果为一直递增,那么相当于最大的减去最小的,比如[1, 4, 5], (4 - 1) + (5 - 4) = 5 - 1;
38+
2、如果有波峰,波谷,那么递增的则累加,比如[1, 4, 2, 8], (4 -1) + (8 - 2)
39+
*/
40+
public int maxProfit(int[] prices) {
41+
int maxProfitInt = 0;
42+
for (int i = 1; i < prices.length; i++) {
43+
if (prices[i] > prices[i - 1]) {
44+
maxProfitInt += (prices[i] - prices[i - 1]);
45+
}
46+
}
47+
48+
return maxProfitInt;
49+
}
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
}

0 commit comments

Comments
 (0)