>g++ -v
>gcc 4.5.2
make sure to include the two headers, and use boost::
#include<boost/shared_ptr.hpp>
#include<memory>
boost::shared_ptr<int> p;
int main()
{
}
>g++ -v
>gcc 4.5.2
make sure to include the two headers, and use boost::
#include<boost/shared_ptr.hpp>
#include<memory>
boost::shared_ptr<int> p;
int main()
{
}
Reverse string is good example to use StringBuffer and char[] array in java
import java.io.*;
class reverseString
{
public static void main(String args[])
{
String cool="abcd";
String str = reverse(cool);
System.out.println("str=" + str);
String str1 = reverse2(cool);
System.out.println("str1=" + str1);
}
//using StringBuffer
public static String reverse(String str)
{
StringBuffer sb = null;
if(str != null)
{
sb = new StringBuffer(str);
int len= str.length();
for(int i=0; i<len/2; i++)
{
char c=sb.charAt(i);
sb.setCharAt(i, sb.charAt(len-1-i));
sb.setCharAt(len-1-i, c);
}
}
return sb.toString();
}
//using char[]
public static String reverse2(String str)
{
String ret=null;
if(str != null)
{
int len= str.length();
char[] charArr = str.toCharArray();
for(int i=0; i<len/2; i++)
{
char c = charArr[i];
charArr[i]=charArr[len-1-i];
charArr[len-1-i]=c;
}
ret = new String(charArr);
}
return ret;
}
}
import java.io.*;
class MyHeap
{
final int Max = 100;
int[] Arr = new int[Max];
int inx;
int size;
public MyHeap()
{ inx = 0;}
public void insert(int n)
{
if(inx < Max)
{
inx++;
Arr[inx] = n;
heapUp(inx);
}
}
public void remove()
{
if(inx == 1)
inx--;
else if(inx > 1)
{
int tmp = Arr[1];
Arr[1] = Arr[inx];
inx--;
heapDown(1);
}
}
public void heapDown(int index)
{
int leftchild = index*2;
int rightchild = index*2 + 1;
if(rightchild <= inx)
{
if(Arr[leftchild] < Arr[rightchild])
{
if(Arr[index] > Arr[leftchild])
{ int tmp = Arr[index];
Arr[index] = Arr[leftchild];
Arr[leftchild] = tmp;
heapDown(leftchild);
}
}
else
{
if(Arr[index] > Arr[rightchild])
{ int tmp = Arr[index];
Arr[index] = Arr[rightchild];
Arr[rightchild] = tmp;
heapDown(rightchild);
}
}
}
else
{
if(leftchild <= inx)
{
if(Arr[index] > Arr[leftchild])
{ int tmp = Arr[index];
Arr[index] = Arr[leftchild];
Arr[leftchild] = tmp;
heapDown(leftchild);
}
}
}
}
public void heapUp(int index)
{
int parent = index/2;
if(parent >= 1)
{
if(Arr[index] < Arr[parent])
{
int tmp = Arr[index];
Arr[index] = Arr[parent];
Arr[parent] = tmp;
heapUp(parent);
}
}
}
public void show()
{
for(int i=1; i<=inx; i++)
{
System.out.println("Arr[" + i + "]=" + Arr[i]);
}
}
}
class heap
{
public static void main(String args[])
{
MyHeap h = new MyHeap();
h.insert(100);
h.insert(11);
h.show();
h.remove();
System.out.println("cool");
h.show();
System.out.println("cool");
}
}
How to find the majority of element in a given integer Array.
This is pretty popular interview question for software engineer.
majority means: the numbers of repeating elements is greater than the half of length of Array
(# elements > Array.length/2)
e.g Arra = {1, 2, 2, 3}
2 is not majority (# of 2 is 2, and length is Array.length/2 = 2)
e.g Array = {1, 2, 3, 3, 3}.
3 is majority because # of 3 > 5/2
1) one of the obvious solution is to sort the array and find the max longth of continuous integer in the array,
complexity: sort the array, need O(nlogn) (e.g quick sort, merge sort, heap sort etc..)
find the max length of continuous integer: O(n) (just scan through the array, and keep the max length value)
total: O(nlogn) which is not bad.
but you can do better than O(nlogn). O(n)
if the majority exist in the array, then # of element has to be greater than the size of the Array.
if we start count the first element, initialize the count = 1, and initialize the majority element mj=0
and check the next element, if they are same, then increase the count, else decrease the count,
after decreasing the count and the count reaches to zero, then we need reset the majority element to new index mj=i;
import java.io.*;
import java.lang.String;
import java.util.*;
class Majority
{
public static void main(String args[])
{
int[] A={2, 3, 2};
System.out.println(majority(A));
}
public static boolean majority(int[] A)
{
boolean ret = false;
if( A != null)
{
int len = A.length;
if(len == 1)
ret = true;
else if(len > 1)
{
int c=1;
int mj=0;
for(int i=1; i<len; i++)
{
if(A[mj] == A[i])
c++;
else
{ c--;
if(c == 0)
{ mj = i; c=1;}
}
}
int count=0;
for(int i=0; i<len; i++)
{
if(A[mj] == A[i])
count++;
}
if(count > len/2)
ret = true;
}
}
return ret;
}
}
Double linkedlist is used for dynamic memory allocation, there are several adivantages compares to Array
1) you can add elements in run time.
2) you have access to head and tail in O(1) time
3) you remove element from the list and you did not need to shift the element around like a Array.
Disadvantage compares to Array
1) you can’t access(index) any element in O(1) like an Array
2) harder to implement
how to remove node from double linked list?
we consider four cases:
1) if the link has only one node,
2) if the removed node is the head node and more than one node for the list
3) if the remove node is tail node and more than one node for the list
4) if the removed node is not a head or the tail node.
from the pic, we can see the four cases very easily.
import java.io.*;
import java.lang.String;
class Node
{
Node prev;
Node next;
int data;
public Node(int n)
{ prev = next = null; data = n;}
}
class DLL
{
Node head;
Node tail;
public DLL()
{ head = tail = null;}
public void append(Node no)
{
if(head == null)
{ head = no; tail = head; }
else
{
tail.next = no;
no.prev = tail;;
tail = tail.next;
}
}
public void addfront(Node no)
{
if(head == null)
{ head = no; tail = head;}
else
{
head.prev = no;
no.next = head;
head = no;
}
}
public void addNext(Node front, Node no)
{
if(front != null)
{
if(front.next == null)
{ append(no);}
else
{
Node cur = no;
Node prev = front;
Node next = front.next;
cur.prev = prev;
cur.next = next;
prev.next = cur;
next.prev = cur;
}
}
}
public void Remove(Node no)
{
if(no != null)
{
Node cur = head;
while(cur != no) cur=cur.next;
if(cur != null)
{
if(cur.prev == null)
{
System.out.println ("here1");
if(cur.next != null)
{
Node prev = cur;
cur = cur.next;
head = cur;
cur.prev = null;
prev.next = null;
}
else
{
head = tail = null;
}
}
else
{
System.out.println ("here2");
if(cur.next != null)
{
Node prev = cur.prev;
Node next = cur.next;
prev.next = next;
next.prev = prev;
cur.next = null;
cur.prev = null;
}
else
{
tail = cur.prev;
tail.next = null;
cur.prev = null;
}
}
}
else
System.out.println("error");
}
}
public void show()
{
Node cur = head;
while(cur != null)
{
System.out.println ("cur=" + cur.data);
cur = cur.next;
}
}
public void show1()
{
Node cur = tail;
while(cur != null)
{
System.out.println ("rev cur=" + cur.data);
cur = cur.prev;
}
}
}
class Dlinkedlist
{
public static void main(String args[])
{
DLL dll = new DLL();
Node p1 = new Node(3);
dll.append(p1);
Node p2 = new Node(4);
dll.append(p2);
Node p3 = new Node(5);
dll.append(p3);
dll.addNext(p3, new Node(7));
Node p4 = new Node(10);
dll.append(p4);
dll.show();
dll.show1();
dll.Remove(p4);
System.out.println ("====");
dll.show();
}
}
//convert String to int
String str = "123";
int num = Integer.parseInt(str);
//convert int to String
int num = 123;
String str = Integer.toString(num);
//convert Integer to int
Integer in = new Integer(3);
int n = in.intValue();
//convert char to String
String str = Character.toString('1');
Method 1
full code
import java.io.*;
import java.lang.String;
import java.util.*;
class Reverse
{
public static void main(String args[])
{
Reverse(" nice! hello world ");
}
public static void Reverse(String str)
{
if(str != null)
{
String s = "";
Vector<String> vet = new Vector<String>();
for(int i = 0; i < str.length(); i++)
{
if(str.charAt(i) == ' ')
{
if(!s.equals(""))
vet.add(s);
s="";
}
else
s = s + str.charAt(i) + "";
}
if(str.charAt(str.length() - 1) != ' ')
vet.add(s);
System.out.println(vet);
for(int i = 0; i<vet.size(); i++)
System.out.println(vet.get(vet.size() - 1 - i));
}
}
}
print all permutation of given string
there are a few way to do it, but I will show a very simple way and can be remembered.
this is not very elegant way to do it, there is recursive way to do and very elegant, but it is not easy to remember
Ideal from the Pic
Method 1 (Java)
import java.io.*;
import java.lang.String;
import java.util.*;
class Permu2
{
public static void main(String args[])
{
per("abcd");
}
public static void per(String str)
{
Vector<String> vet = new Vector<String>();
if(str != null)
{
int len = str.length();
if(len > 0)
{
vet.add(str.charAt(0) + "");
Vector<String> vet2 = new Vector<String>();
for(int j = 1; j < len; j++)
{
for(int i = 0; i < vet.size(); i++)
{
Vector<String> vet1 = new Vector<String>();
vet1 = Combin2(vet.get(i), str.charAt(j) + "");
for(int k=0; k<vet1.size(); k++)
vet2.add(vet1.get(k));
}
vet.clear();
for(int x=0; x<vet2.size(); x++)
vet.add(vet2.get(x));
vet2.clear();
}
for(int i = 0; i < vet.size(); i++)
{
System.out.println(vet.get(i));
}
}
}
}
public static Vector<String> Combin2(String str, String ch)
{
Vector<String> vet = new Vector<String>();
for(int i = 0; i < str.length() + 1; i++)
{
StringBuffer sb = new StringBuffer(str);
sb.insert(i, ch);
vet.add(sb.toString());
}
return vet;
}
public static Vector<String> Combin(String str, String ch)
{
Vector<String> vet = new Vector<String>();
int i = 0;
for(i = 0; i < str.length() + 1; i++)
{
String s = "";
for(int j = 0; j < str.length(); j++)
{
if(i == j)
s = s + ch;
s = s + str.charAt(j);
}
if(i == str.length())
s = s+ch;
System.out.println (s);
}
return vet;
}
}
This is very typical interview question.
a Binary Tree is Binary Search Tree is:
1) left subtree and right subtree are Binary Search Tree.
2) the max(left subtree) < parent node < max(right subtree)
3) the null node is Binary Search Tree.
Method 1 in Java
1) do the Inorder traversal, store it in the Vector
2) if the Vector
void Inorder(Node root, Vector<Integer> vet)
{
if(root != null)
{
Bin(root.left);
vet.add(new Integer(root.data));
Bin(root.right);
}
}
boolean isBST(Vector<int> vet)
{
boolean ret = true;
if(vet != null && vet.size() > 0)
{
for(int i=0; i < vet.size() - 1 && ret; i++)
{
int in = vet.get(i).intValue();
int in1= vet.get(i+1).intValue();
if(in > in1)
ret = false;
}
}
return ret;
}
Method 2
recursively way to check in C++
bool isBST(struct node* root)
{
static struct node *prev = NULL;
// traverse the tree in inorder fashion and keep track of prev node
if (root)
{
if (!isBST(root->left))
return false;
// Allows only distinct valued nodes
if (prev != NULL && root->data <= prev->data)
return false;
prev = root;
if(!isBST(root->right))
return false;
}
return true;
}
recursively way to check in Java
public static Node prev = null;
public static boolean isBST(Node r)
{
if(r == null)
return true;
else
{
if(!isBST(r.left))
return false;
if(prev != null && prev.data >= r.data)
return false;
prev = r;
if(!isBST(r.right))
return false;
}
return true;
}
Powered by WordPress