Linghui
BAN USERpublic static int getMaxProduct(int array[]) {
int max1 = Integer.MIN_VALUE;
int max2 = max1, max3 = max1;
int min1 = Integer.MAX_VALUE, min2 = min1;
for (int i = 0; i < array.length; i++) {
if (array[i] > max1) {
max2 = max1;
max1 = array[i];
}else if(array[i]>max2){
max3 = max2;
max2 = array[i];
}else if(array[i]>max3){
max2 = array[i];
}
if (array[i] < min1) {
min2 = min1;
min1 = array[i];
}else if(array[i]<min2){
min2 = array[i];
}
}
System.out.println("max1 = " + max1 + " max2 = " + max2 + " max3 = "
+ max3);
System.out.println("min1 = " + min1 + " min2 = " + min2);
return max1*max2*max3>max1*min1*min2?max1*max2*max3:max1*min1*min2;
}
public static void printStars(int n) {
if (n % 2 == 0) {
System.out.println("Odd number only");
return;
} else {
int i;
for (i = 0; i < n; i++) {
if (i > n / 2) {
printStar((n - i) * 2 - 1);
} else {
printStar(i * 2 + 1);
}
}
}
}
public static void printStar(int n) {
int i = 0;
while (i < n) {
System.out.print("* ");
i++;
}
System.out.println();
}
public static void findNodes(TreeNode node, int sum) {
List<TreeNode> l = new LinkedList<TreeNode>();
convertIntoList(node, l);
int i = 0;
int j = l.size()-1;
while (i < j) {
if (l.get(i).data + l.get(j).data == sum) {
System.out.println("nodes are: " + l.get(i).data + ", and "
+ l.get(j).data);
i++;
j--;
} else if (l.get(i).data + l.get(j).data < sum) {
i++;
} else {
j--;
}
}
}
public static void convertIntoList(TreeNode node, List<TreeNode> l) {
if (node == null) {
return;
} else {
convertIntoList(node.left, l);
l.add(node);
convertIntoList(node.right, l);
}
}
public static TreeNode firstAncestor(TreeNode root, TreeNode a, TreeNode b) {
Stack<TreeNode> path1 = new Stack<TreeNode>();
Stack<TreeNode> path2 = new Stack<TreeNode>();
pathToRoot(root, path1, a);
pathToRoot(root, path2, b);
for (TreeNode node : path1) {
System.out.print(node.data + " ");
}
System.out.println();
for (TreeNode node : path2) {
System.out.print(node.data + " ");
}
System.out.println();
path1.retainAll(path2);
if (path1.size() > 0) {
return path1.get(path1.size() - 1);
}
return null;
}
public static boolean pathToRoot(TreeNode root, Stack<TreeNode> paths,
TreeNode target) {
if (root == null) {
return false;
}
paths.push(root);
if (root.data == target.data) {
return true;
}
if (root.left != null) {
if (pathToRoot(root.left, paths, target)) {
return true;
}
}
if (root.right != null) {
if (pathToRoot(root.right, paths, target)) {
return true;
}
}
paths.pop();
return false;
}
package com.star.epic;
import java.util.HashSet;
import java.util.Set;
/*
* Determine whether a number is colorful or not.
* 263 is a colorful number because (2,6,3,2x6,6x3,2x3x6) are all different
* whereas 236 is not because (2,3,6,2x3,3x6,2x3x6) have 6 twice.
* So take all consecutive subsets of digits,
* take their product and ensure all the products are different.
*/
public class Colorful {
public static boolean is_color(int num) {
String str = String.valueOf(num);
char digits[] = str.toCharArray();
int length = str.length();
int i = 1;
Set set = new HashSet();
// i is the number of consecutive number
int value = 1;
while (i <= length) {
System.out.println("get " + i + " consecutive number");
int m = 0;
while (m <= length - i) {
System.out.print("start from index "+m+" ,we get ");
int j = 0;
value = 1;
//get value from a[m] to a[m+j]
while (j < i) {
System.out.print(digits[m + j] - 48+" ");
value = value *(digits[m + j] - 48);
j++;
}
System.out.println();
if(set.contains(value)){
return false;
}else{
set.add(value);
}
m++;
}
System.out.println();
i++;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 263;
System.out.println(is_color(number));
number = 236;
System.out.println(is_color(number));
}
}
#include <stdio.h>
int separate(char *,char *);
int main(){
char old[20]="4678912356012356";
printf("before: %s",old);
char new[30];
separate(old,new);
printf("\nafter: %s\n",new);
return 0;
}
void separate(char *old,char*new){
int i = 0;
int j = 0;
while(old[i]!='\0'){
new[j] = old[i];
if(old[i]+1!=old[i+1]){
new[++j]=';';
}
i++;
j++;
}
new[j]='\0';
return;
}
This is not correct. Suppose the array like this [5, 7, 9 ,8], the second largest number is actually 8, but your program gives 7. Current element is larger than current max doesn't mean it is larger than second largest number. So you need one more check.
- Linghui March 10, 2012