Skip to content

Commit 4d33d63

Browse files
author
freemanzhang
committed
init impl
1 parent c4ceb00 commit 4d33d63

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package binarySearchTree;
2+
3+
import utility.TreeNode;
4+
5+
/*
6+
Given a root node reference of a BST and a key, delete the node with the given key in the BST.
7+
Return the root node reference (possibly updated) of the BST.
8+
9+
Basically, the deletion can be divided into two stages:
10+
11+
Search for a node to remove.
12+
If the node is found, delete the node.
13+
Note: Time complexity should be O(height of tree).
14+
15+
Example:
16+
17+
root = [5,3,6,2,4,null,7]
18+
key = 3
19+
20+
5
21+
/ \
22+
3 6
23+
/ \ \
24+
2 4 7
25+
26+
Given key to delete is 3. So we find the node with value 3 and delete it.
27+
28+
One valid answer is [5,4,6,2,null,null,7], shown in the following BST.
29+
30+
5
31+
/ \
32+
4 6
33+
/ \
34+
2 7
35+
36+
Another valid answer is [5,2,6,null,4,null,7].
37+
38+
5
39+
/ \
40+
2 6
41+
\ \
42+
4 7
43+
* */
44+
45+
public class DeleteNodeInABST
46+
{
47+
public TreeNode deleteNode( TreeNode root, int key )
48+
{
49+
if ( root == null )
50+
{
51+
return root;
52+
}
53+
if ( root.val > key )
54+
{
55+
root.left = deleteNode( root.left, key );
56+
}
57+
else if ( root.val < key )
58+
{
59+
root.right = deleteNode( root.right, key );
60+
}
61+
else
62+
{
63+
if ( root.left == null )
64+
{
65+
return root.right;
66+
}
67+
else if ( root.right == null )
68+
{
69+
return root.left;
70+
}
71+
else // two children
72+
{
73+
root.val = getMin( root.right );
74+
root.right = deleteNode( root.right, root.val );
75+
}
76+
}
77+
return root;
78+
}
79+
80+
private int getMin( TreeNode root )
81+
{
82+
int min = root.val;
83+
while ( root.left != null )
84+
{
85+
root = root.left;
86+
min = root.val;
87+
}
88+
return min;
89+
}
90+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package hashtable;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.PriorityQueue;
6+
7+
/*
8+
Given a string, sort it in decreasing order based on the frequency of characters.
9+
10+
Example 1:
11+
12+
Input:
13+
"tree"
14+
15+
Output:
16+
"eert"
17+
18+
Explanation:
19+
'e' appears twice while 'r' and 't' both appear once.
20+
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
21+
Example 2:
22+
23+
Input:
24+
"cccaaa"
25+
26+
Output:
27+
"cccaaa"
28+
29+
Explanation:
30+
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
31+
Note that "cacaca" is incorrect, as the same characters must be together.
32+
Example 3:
33+
34+
Input:
35+
"Aabb"
36+
37+
Output:
38+
"bbAa"
39+
40+
Explanation:
41+
"bbaA" is also a valid answer, but "Aabb" is incorrect.
42+
Note that 'A' and 'a' are treated as two different characters.
43+
* */
44+
45+
public class SortCharactersByFrequency
46+
{
47+
public String frequencySort( String s )
48+
{
49+
if ( s == null || s.length() == 0 )
50+
{
51+
return "";
52+
}
53+
54+
Map<Character, Integer> histogram = new HashMap<>();
55+
for ( Character ch : s.toCharArray() )
56+
{
57+
histogram.put( ch, histogram.getOrDefault( ch, 0 ) + 1 );
58+
}
59+
60+
PriorityQueue<Map.Entry<Character,Integer>> maxQueue = new PriorityQueue<>( ( o1, o2 ) -> o2.getValue() - o1.getValue() );
61+
for ( Map.Entry<Character, Integer> entry : histogram.entrySet() )
62+
{
63+
maxQueue.offer( entry );
64+
}
65+
66+
StringBuilder result = new StringBuilder();
67+
while ( !maxQueue.isEmpty() )
68+
{
69+
Map.Entry<Character, Integer> entry = maxQueue.poll();
70+
for ( int i = 0; i < entry.getValue(); i++ )
71+
{
72+
result.append( entry.getKey() );
73+
}
74+
}
75+
return result.toString();
76+
}
77+
}

src/string/RightRotation.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package string;
2+
3+
/*
4+
判断string1和string2是否互为right rotation。比如:abcd和cdab。CC150原题,
5+
一行代码即可解决。
6+
* */
7+
8+
public class RightRotation
9+
{
10+
public static int rightRotate( String word1, String word2 )
11+
{
12+
if ( word1 == null || word2 == null || word1.length() == 0 || word2.length() == 0
13+
|| word1.length() != word2.length() )
14+
{
15+
return -1;
16+
}
17+
String str = word1 + word1;
18+
return str.indexOf( word2 ) != -1 ? 1 : -1;
19+
}
20+
}

0 commit comments

Comments
 (0)