#codingexercise
Remove spaces from a string
Void removeSpaces(stringbuilder str)
{
Int count = 0;
For(int i =0; i < str.count; i++)
{
If (str[i] != ' '){
Str[count] = str[i];
Count++;
}
}
Str[count] ='/0';
}
Given a string that can be formed from the letters arranged as follows:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z
and with only the following positions
Void getPath(string s)
{
Int len = 0;
Int curx = 0;
Int cury = 0;
for( int i = 0; i < s.length; i++)
{
Int nextx = (str[i] - 'A')/5;
Int nexty = (str[i] - 'A')%5;
While (curx > nextx){
Curx--;
len++;
}
While (curx < nextx){
Curx++;
len++;
}
While (cury < nexty){
Cury++;
len++;
}
While (cury > nexty){
Cury--;
len++;
}
}
Return len;
}
Get longest Common Prefix in strings
String walkTrie(ref TrieNode root, int k)
{
Node cur = root;
Int index=0;
String prefix;
While(countChildren(cur, ref index) > k && isleaf(cur) == false){
Cur = cur.children[index];
Prefix+= cur.data;
}
Return prefix;
}
Convert a binary tree into a binary search tree
void ConvertBTtoBST(ref Node root)
{
if (root == null) return null
var inorder = new List<int>();
InorderTraversal(root, ref inorder);
inorder.sort();
InorderToTree(inorder, ref root);
}
Remove spaces from a string
Void removeSpaces(stringbuilder str)
{
Int count = 0;
For(int i =0; i < str.count; i++)
{
If (str[i] != ' '){
Str[count] = str[i];
Count++;
}
}
Str[count] ='/0';
}
Given a string that can be formed from the letters arranged as follows:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z
and with only the following positions
Void getPath(string s)
{
Int len = 0;
Int curx = 0;
Int cury = 0;
for( int i = 0; i < s.length; i++)
{
Int nextx = (str[i] - 'A')/5;
Int nexty = (str[i] - 'A')%5;
While (curx > nextx){
Curx--;
len++;
}
While (curx < nextx){
Curx++;
len++;
}
While (cury < nexty){
Cury++;
len++;
}
While (cury > nexty){
Cury--;
len++;
}
}
Return len;
}
Get longest Common Prefix in strings
String walkTrie(ref TrieNode root, int k)
{
Node cur = root;
Int index=0;
String prefix;
While(countChildren(cur, ref index) > k && isleaf(cur) == false){
Cur = cur.children[index];
Prefix+= cur.data;
}
Return prefix;
}
Convert a binary tree into a binary search tree
void ConvertBTtoBST(ref Node root)
{
if (root == null) return null
var inorder = new List<int>();
InorderTraversal(root, ref inorder);
inorder.sort();
InorderToTree(inorder, ref root);
}
No comments:
Post a Comment