Skip to content

Commit ba2fccd

Browse files
committed
Leetcode
1 parent 530551e commit ba2fccd

6 files changed

Lines changed: 232 additions & 16 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Leetcode;
2+
3+
/**
4+
* @author szh
5+
* @create 2018-07-26 22:44
6+
**/
7+
public class CountBinarySubstrings {
8+
public int countBinarySubstrings(String s) {
9+
int[] groups = new int[s.length()];
10+
int t = 0;
11+
groups[0] = 1;
12+
for (int i = 1; i < s.length(); i++) {
13+
if (s.charAt(i-1) != s.charAt(i)) {
14+
groups[++t] = 1;
15+
} else {
16+
groups[t]++;
17+
}
18+
}
19+
20+
int ans = 0;
21+
for (int i = 1; i <= t; i++) {
22+
ans += Math.min(groups[i-1], groups[i]);
23+
}
24+
return ans;
25+
}
26+
27+
public static void main(String[] args) {
28+
String s ="00110011";
29+
System.out.println(new CountBinarySubstrings().countBinarySubstrings(s));
30+
}
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Leetcode;
2+
3+
/**
4+
* @author szh
5+
* @create 2018-08-15 22:24
6+
**/
7+
public class ExcelSheetColumnNumber {
8+
public static void main(String[] args) {
9+
char c='A';
10+
// System.out.println(Math.abs(-64 + c));
11+
System.out.println(((int)Math.pow(26, 1)));
12+
13+
System.out.println(new ExcelSheetColumnNumber().titleToNumber("AA"));
14+
}
15+
public int titleToNumber(String s) {
16+
if(s == null || s.length() ==0){
17+
return 0;
18+
}
19+
int flag =-64;
20+
int result=0;
21+
int length =s.length();
22+
for(int i=0 ;i<s.length() ;i++){
23+
char c = s.charAt(i);
24+
int var1=Math.abs(flag + c);
25+
int var2=(int)Math.pow(--length, 26);
26+
if(var2 ==0){
27+
var2=1;
28+
}
29+
result = var1 * var2 + result;
30+
}
31+
return result;
32+
}
33+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package Leetcode;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author szh
7+
* @create 2018-08-14 23:31
8+
**/
9+
public class LemonadeChange {
10+
public boolean lemonadeChange(int[] bills) {
11+
Stack<Integer> five =new Stack<>();
12+
Stack<Integer> ten =new Stack<>();
13+
int[] money =new int[bills.length];
14+
for(int i =0 ;i<=bills.length ;i++){
15+
if(bills[i] == 5){
16+
five.push(bills[i]);
17+
}
18+
if(bills[i] == 10){
19+
ten.push(bills[i]);
20+
if(five.size() == 0){
21+
return false;
22+
}
23+
five.pop();
24+
}
25+
if(bills[i] == 20){
26+
if(five.size() == 0){
27+
return false;
28+
}
29+
if(ten.size() == 0){
30+
if(five.size() >=3){
31+
five.pop();
32+
five.pop();
33+
five.pop();
34+
}else{
35+
return false;
36+
}
37+
}else{
38+
ten.pop();
39+
five.pop();
40+
}
41+
}
42+
}
43+
return true;
44+
}
45+
46+
public static void main(String[] args) {
47+
int[] a= new int[]{5,5,5,10,5,5,10,20,20,20};
48+
new LemonadeChange().lemonadeChange(a);
49+
}
50+
}

src/main/java/Leetcode/MinimumAbsoluteDifferenceinBST.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@
66
**/
77
public class MinimumAbsoluteDifferenceinBST {
88

9+
TreeNode nodeval=null;
10+
int min=Integer.MAX_VALUE;
11+
12+
///////
13+
public int getMinimumDifferenceTwo(TreeNode root) {
14+
if(root != null)
15+
compareTreeNode(root);
16+
return min;
17+
}
18+
public void compareTreeNode(TreeNode node){
19+
20+
if(node == null){
21+
return;
22+
}
23+
compareTreeNode(node.left);
24+
if(nodeval != null){
25+
min =Math.min(node.val - nodeval.val ,min);
26+
}
27+
nodeval =node;
28+
compareTreeNode(node.right);
29+
}
30+
31+
32+
33+
34+
35+
/////
936
public int getMinimumDifference(TreeNode root) {
1037
if(root != null){
1138
return compareTree(Integer.MAX_VALUE,root);
@@ -74,23 +101,24 @@ public void inorder(TreeNode root) {
74101

75102

76103
public static void main(String[] args) {
77-
// TreeNode t =new TreeNode(1);
78-
// TreeNode t1 =new TreeNode(3);
79-
// TreeNode t2 =new TreeNode(2);
80-
// t1.left=t2;
81-
// t.right=t1;
82-
83-
TreeNode t =new TreeNode(236);
84-
TreeNode t1 =new TreeNode(104);
85-
TreeNode t2 =new TreeNode(227);
86-
TreeNode t3 =new TreeNode(701);
87-
TreeNode t4 =new TreeNode(911);
88-
t1.right=t2;
89-
t3.right=t4;
90-
t.left=t1;
91-
t.right=t3;
104+
TreeNode t =new TreeNode(1);
105+
TreeNode t1 =new TreeNode(5);
106+
TreeNode t2 =new TreeNode(3);
107+
t1.left=t2;
108+
t.right=t1;
109+
110+
// TreeNode t =new TreeNode(236);
111+
// TreeNode t1 =new TreeNode(104);
112+
// TreeNode t2 =new TreeNode(227);
113+
// TreeNode t3 =new TreeNode(701);
114+
// TreeNode t4 =new TreeNode(911);
115+
// t1.right=t2;
116+
// t3.right=t4;
117+
// t.left=t1;
118+
// t.right=t3;
92119
// System.out.println(new MinimumAbsoluteDifferenceinBST().getMinimumDifference(t));
93-
System.out.println(new MinimumAbsoluteDifferenceinBST().getMinimum(t));
120+
// System.out.println(new MinimumAbsoluteDifferenceinBST().getMinimum(t));
121+
System.out.println(new MinimumAbsoluteDifferenceinBST().getMinimumDifferenceTwo(t));
94122
}
95123

96124
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Leetcode;
2+
3+
/**
4+
* @author szh
5+
* @create 2018-08-16 22:48
6+
**/
7+
public class RotateString {
8+
public boolean rotateString(String A, String B) {
9+
String C = A +A ;
10+
for(int i =0 ;i< C.length() ;i++){
11+
int index =i;
12+
int j=0;
13+
for(;j<B.length() ;j++){
14+
if(C.charAt(index) == B.charAt(j)){
15+
index++;
16+
}else{
17+
break;
18+
}
19+
}
20+
if( j == B.length() ){
21+
return true;
22+
}
23+
}
24+
return false;
25+
}
26+
27+
public static void main(String[] args) {
28+
System.out.println(new RotateString().rotateString("abcde", "abced"));
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Leetcode;
2+
3+
/**
4+
* @author szh
5+
* @create 2018-08-12 20:48
6+
**/
7+
public class SearchinaBinarySearchTree {
8+
public TreeNode searchBST(TreeNode root, int val) {
9+
if(root == null){
10+
return null;
11+
}
12+
if(root.val == val){
13+
return root;
14+
}
15+
if(root.val > val){
16+
return compareAndSelect(root.left,val);
17+
}else{
18+
return compareAndSelect(root.right,val);
19+
}
20+
}
21+
public TreeNode compareAndSelect(TreeNode node , int val){
22+
if(node == null){
23+
return null;
24+
}
25+
if(node.val == val){
26+
return node;
27+
}
28+
if(node.val >val){
29+
return compareAndSelect(node.left,val);
30+
}else{
31+
return compareAndSelect(node.right,val);
32+
}
33+
}
34+
35+
class TreeNode {
36+
int val;
37+
TreeNode left;
38+
TreeNode right;
39+
40+
TreeNode(int x) {
41+
val = x;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)