jbai_98@yahoo.com
BAN USERThis requirement looks like to have the intersection for three hash table. Use Java Set can get it.
public static Set<Integer> threeSetIntersection(Set<Integer> set1, Set<Integer> set2, Set<Integer> set3) {
Set<Integer> tempSet1 = new HashSet<Integer>(set1);
Set<Integer> tempSet2 = new HashSet<Integer>(set1);
tempSet1.retainAll(set2);
tempSet2.retainAll(set3);
tempSet1.retainAll(tempSet2);
return tempSet1;
}
public static void printDiagonally(int[][] array) {
// Get row number and column number
int rowNum = array.length;
int columnNum = array[0].length;
// Print each diagonals that start at the first row
for (int k = 0; k < columnNum; k++) {
System.out.println();
int x = 0; // Always start from the first row
int y = k;
while (y >= 0) {
System.out.print(array[x++][y--]);
}
}
// Print each diagonals that start at the last column (start from the second row)
for (int k = 1; k < rowNum; k++) {
System.out.println();
int y = columnNum - 1; // Always start from the last column
int x = k;
while (x < rowNum) {
System.out.print(array[x++][y--]);
}
}
} // End of printDiagonally()
Yes, it seems the count is not necessary. Thanks
public static boolean findDuplicateWithinN(int[] array, int n) {
HashSet<Integer> set = new HashSet<Integer>(n);
for (int i = 0; i < array.length; i++) {
Integer current = new Integer(array[i]);
// Duplicate found
if (set.contains(current)) {
return true;
}
// Always keep set size as n
if (i >= n) {
set.remove(new Integer(array[i - n]));
}
set.add(current);
}
return false;
}
public static String removeBAndAC(String s) {
Stack<Integer> stack = new Stack<Integer>();
StringBuffer newString = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char currentChar = s.charAt(i);
switch (currentChar) {
case 'a':
stack.push(i);
break;
case 'b':
break;
case 'c':
if (!(stack.isEmpty())) {
stack.pop();
}
else {
newString.append(currentChar);
}
break;
default:
if (!(stack.isEmpty())) {
stack.pop();
newString.append('a');
}
newString.append(currentChar);
break;
}
}
while (!(stack.isEmpty())) {
stack.pop();
newString.append('a');
}
return newString.toString();
} // End of removeBAndAC()
public static boolean findDuplicateWithingN(int[] array, int n) {
HashSet<Integer> set = new HashSet<Integer>(n);
int count = 0;
for (int i = 0; i < array.length; i++) {
Integer current = new Integer(array[i]);
// Duplicate found
if (set.contains(current)) {
return true;
}
// Always keep set size as n
if (count < n) {
count++;
}
else {
set.remove(new Integer(array[i - n]));
}
set.add(current);
}
return false;
}
Repbettylfoster, University dean at Littler's
Hi , I am Betty from the USA , working as Dien in Littler's University for the last three years. Previously ...
Repemileyrollens, Architect at Cloudera
I am Emily, a detail-oriented and certified junior Architect who excels at developing construction drawings, generating 3D models. I love ...
Rephallieriddic, HR Executive Trainee at Barclays Capital
I am Hallie, Dedicated and experienced administrative secretary who excels at prioritizing , completing multiple tasks simultaneously and following through to ...
Repjenniferdray9, Accountant at ABC TECH SUPPORT
Hi I am Jennifer D. Ray from san Diego.Currently i am working as a parts salesperson in Rite solution ...
Repfloydmsnyder, Theoretical mathematician at STM Auto Parts
As a Theoretical Mathematician,I’m little more interested in the theory involved with mathematics. In my free time , I ...
For this problem, the possible solution is as follows
Time is O(n * log n) and Space is O(n)
- jbai_98@yahoo.com May 21, 2013