Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1097138

Browse files
author
C5141506
committed
Leetcode problems
1 parent e35687a commit 1097138

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package java_problem.binarytree;
2+
3+
import java.util.Arrays;
4+
5+
6+
class TreeFromPreAndInOrderTree {
7+
8+
public static void main(String args[]) {
9+
int[] preorder = {3, 9, 20, 15, 7}, inorder = {9, 3, 15, 20, 7};
10+
buildTree(preorder, inorder);
11+
}
12+
13+
public static TreeNode buildTree(int[] preorder, int[] inorder) {
14+
if (preorder.length == 0 || inorder.length == 0) return null;
15+
16+
TreeNode root = new TreeNode(preorder[0]);
17+
int mid = 0;
18+
for (int i = 0; i < inorder.length; i++) {
19+
if (preorder[0] == inorder[i]) mid = i;
20+
}
21+
22+
root.left =
23+
buildTree(
24+
Arrays.copyOfRange(preorder, 1, mid + 1),
25+
Arrays.copyOfRange(inorder, 0, mid)
26+
);
27+
root.right =
28+
buildTree(
29+
Arrays.copyOfRange(preorder, mid + 1, preorder.length),
30+
Arrays.copyOfRange(inorder, mid + 1, inorder.length)
31+
);
32+
33+
return root;
34+
}
35+
}
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package java_problem.string;
2+
3+
class StringCompression {
4+
public static void main(String args[]) {
5+
char[] chars = {'a','a','b','b','c','c','c'};
6+
System.out.println( compress(chars));
7+
}
8+
public static int compress(char[] chars) {
9+
String s="";
10+
int n=chars.length;
11+
if(n==1) return 1;
12+
int i=0;
13+
while(i<n){
14+
int count=0;
15+
int j=i;
16+
while(j<n && (chars[i]==chars[j])){
17+
count++;
18+
j++;
19+
}
20+
if(count==1){
21+
s=s+String.valueOf(chars[i]);
22+
}else{
23+
s=s+String.valueOf(chars[i])+count;
24+
}
25+
i=j;
26+
}
27+
for(int c=0;c<s.length();c++){
28+
chars[c]=s.charAt(c);
29+
}
30+
31+
return s.length();
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /