Skip to content

Commit 1ff354f

Browse files
Merge pull request #395 from shironghui0593/master
065-第二周作业
2 parents a5061dc + 5a26521 commit 1ff354f

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

Week_02/id_65/LeetCode_242_65.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.imooc.activiti.helloworld;
2+
3+
import com.eclipsesource.json.JsonArray;
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.Arrays;
9+
10+
/**
11+
* @author shironghui on 2019/4/18.
12+
*/
13+
14+
//排序解法
15+
class Solution8 {
16+
public boolean isAnagram(String s, String t) {
17+
if(s.length()!=t.length()){
18+
return false;
19+
}
20+
char[] sChars=s.toCharArray();
21+
char[] tChars=t.toCharArray();
22+
Arrays.sort(sChars);
23+
Arrays.sort(tChars);
24+
return Arrays.equals(sChars,tChars);
25+
}
26+
}
27+
//hash表解法
28+
class Solution9 {
29+
public boolean isAnagram(String s, String t) {
30+
if(s.length()!=t.length()){
31+
return false;
32+
}
33+
int[] count=new int[26];
34+
char[] sChars=s.toCharArray();
35+
char[] tChars=t.toCharArray();
36+
for(int i=0;i<sChars.length;i++){
37+
count[sChars[i]-'a']++;
38+
count[tChars[i]-'a']--;
39+
}
40+
for (int i=0;i<count.length;i++){
41+
if(count[i]!=0)
42+
{
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
}
49+
50+
public class TestChar1 {
51+
public static String stringToString(String input) {
52+
return JsonArray.readFrom("[" + input + "]").get(0).asString();
53+
}
54+
55+
public static String booleanToString(boolean input) {
56+
return input ? "True" : "False";
57+
}
58+
59+
public static void main(String[] args) throws IOException {
60+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
61+
String line;
62+
while ((line = in.readLine()) != null) {
63+
String s = stringToString(line);
64+
line = in.readLine();
65+
String t = stringToString(line);
66+
67+
boolean ret = new Solution8().isAnagram(s, t);
68+
69+
String out = booleanToString(ret);
70+
71+
System.out.print(out);
72+
}
73+
}
74+
}

Week_02/id_65/LeetCode_671_65.java

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.imooc.activiti.helloworld;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
import java.util.Set;
10+
11+
/**
12+
* @author shironghui on 2019/4/18.
13+
*/
14+
class Solution10 {
15+
16+
//暴力搜索法
17+
public void dfs(TreeNode root,Set<Integer> unique){
18+
if(root!=null){
19+
unique.add(root.val);
20+
dfs(root.left,unique);
21+
dfs(root.right,unique);
22+
}
23+
}
24+
public int findSecondMinimumValue(TreeNode root) {
25+
if(root!=null){
26+
Set<Integer> unique=new HashSet<>();
27+
dfs(root,unique);
28+
int min1=root.val;
29+
long ans=Long.MAX_VALUE;
30+
for(int v:unique){
31+
if(min1<v&&v<ans){
32+
ans=v;
33+
}
34+
}
35+
return (ans==Long.MAX_VALUE)?-1:(int)ans;
36+
}
37+
return -1;
38+
}
39+
40+
}
41+
42+
public class TestBinaryTree2 {
43+
public static TreeNode stringToTreeNode(String input) {
44+
input = input.trim();
45+
input = input.substring(1, input.length() - 1);
46+
if (input.length() == 0) {
47+
return null;
48+
}
49+
50+
String[] parts = input.split(",");
51+
String item = parts[0];
52+
TreeNode root = new TreeNode(Integer.parseInt(item));
53+
Queue<TreeNode> nodeQueue = new LinkedList<>();
54+
nodeQueue.add(root);
55+
56+
int index = 1;
57+
while(!nodeQueue.isEmpty()) {
58+
TreeNode node = nodeQueue.remove();
59+
60+
if (index == parts.length) {
61+
break;
62+
}
63+
64+
item = parts[index++];
65+
item = item.trim();
66+
if (!item.equals("null")) {
67+
int leftNumber = Integer.parseInt(item);
68+
node.left = new TreeNode(leftNumber);
69+
nodeQueue.add(node.left);
70+
}
71+
72+
if (index == parts.length) {
73+
break;
74+
}
75+
76+
item = parts[index++];
77+
item = item.trim();
78+
if (!item.equals("null")) {
79+
int rightNumber = Integer.parseInt(item);
80+
node.right = new TreeNode(rightNumber);
81+
nodeQueue.add(node.right);
82+
}
83+
}
84+
return root;
85+
}
86+
87+
public static void main(String[] args) throws IOException {
88+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
89+
String line;
90+
while ((line = in.readLine()) != null) {
91+
TreeNode root = stringToTreeNode(line);
92+
93+
int ret = new Solution10().findSecondMinimumValue(root);
94+
95+
String out = String.valueOf(ret);
96+
97+
System.out.print(out);
98+
}
99+
}
100+
}

Week_02/id_65/LeetCode_783_65.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.imooc.activiti.helloworld;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
11+
/**
12+
* @author shironghui on 2019/4/18.
13+
*/
14+
class Solution13 {
15+
16+
List<Integer> list=new ArrayList<>();
17+
public int minDiffInBST(TreeNode root) {
18+
int min=Integer.MAX_VALUE;
19+
dfs(root);
20+
for (int i=1;i<list.size();i++){
21+
min=Math.min(min,list.get(i)-list.get(i-1));
22+
}
23+
return min;
24+
}
25+
26+
//中序遍历二叉搜索树,list列表有序
27+
private void dfs(TreeNode root) {
28+
if(root==null) {
29+
return;
30+
}
31+
dfs(root.left);
32+
list.add(root.val);
33+
dfs(root.right);
34+
}
35+
36+
}
37+
38+
public class TestBinaryTree4 {
39+
public static TreeNode stringToTreeNode(String input) {
40+
input = input.trim();
41+
input = input.substring(1, input.length() - 1);
42+
if (input.length() == 0) {
43+
return null;
44+
}
45+
46+
String[] parts = input.split(",");
47+
String item = parts[0];
48+
TreeNode root = new TreeNode(Integer.parseInt(item));
49+
Queue<TreeNode> nodeQueue = new LinkedList<>();
50+
nodeQueue.add(root);
51+
52+
int index = 1;
53+
while(!nodeQueue.isEmpty()) {
54+
TreeNode node = nodeQueue.remove();
55+
56+
if (index == parts.length) {
57+
break;
58+
}
59+
60+
item = parts[index++];
61+
item = item.trim();
62+
if (!item.equals("null")) {
63+
int leftNumber = Integer.parseInt(item);
64+
node.left = new TreeNode(leftNumber);
65+
nodeQueue.add(node.left);
66+
}
67+
68+
if (index == parts.length) {
69+
break;
70+
}
71+
72+
item = parts[index++];
73+
item = item.trim();
74+
if (!item.equals("null")) {
75+
int rightNumber = Integer.parseInt(item);
76+
node.right = new TreeNode(rightNumber);
77+
nodeQueue.add(node.right);
78+
}
79+
}
80+
return root;
81+
}
82+
83+
public static void main(String[] args) throws IOException {
84+
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
85+
String line;
86+
while ((line = in.readLine()) != null) {
87+
TreeNode root = stringToTreeNode(line);
88+
89+
int ret = new Solution13().minDiffInBST(root);
90+
91+
String out = String.valueOf(ret);
92+
93+
System.out.print(out);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)