albertchenyu
BAN USER
- 0of 0 votes
AnswersRGBCompare
- albertchenyu in United States
Given a string of RGB value (rr, gg, bb)which represents in hexadecimal. Compare if rr or gg or bb is the biggest, ortwo of those values are equal and larger than the third one, or three valuesare equal with each other.| Report Duplicate | Flag | PURGE
Epic Systems Software Developer
Hi @teargone08 switchrgb is a switch function, what I want to do first is sortting the r, g ,b array. I'm using bubble sort method, so it requires switch in two adjacent element in an array. It's just a helper function for sortting.
- albertchenyu March 09, 2015quick question, no one is considering whether the second string can be transposed to the first one.
- albertchenyu March 07, 2015@Kani90
thank you for neat code, but there's one tiny bug.
in function checkSeq, you need to judge whether 2 * index2 - index is out of boundary.
the solution seems incorrect, at last, scoreX should be 6, and scoreY should be 8
- albertchenyu March 07, 2015Could you give more details about MIIS and some examples?
Thanks
Your matching algorithm is so smart, it let me think of counting sort, which uses the value to search the index.
- albertchenyu March 05, 2015package EPIC;
class IntPair {
public int x;
public int y;
public IntPair(int X, int Y) {
x = X;
y = Y;
}
@Override
public boolean equals(Object other) {
IntPair that = (IntPair) other;
return this.x == that.x && this.y == that.y;
}
}
public class SaddleNumber {
public static void findSaddle(int I[][]) {
if (I == null) {
return;
} else if (I.length == 0) {
return;
}
int height = I.length;
int width = I[0].length;
IntPair max_row[] = new IntPair[height];
IntPair min_col[] = new IntPair[width];
for (int row = 0; row < height; row++) {
int max = I[row][0];
IntPair maxpair = new IntPair(row, 0);
for (int col = 0; col < width; col++) {
if (I[row][col] > max) {
max = I[row][col];
maxpair.x = row;
maxpair.y = col;
}
}
max_row[row] = maxpair;
}
System.out.println("Maxrow");
for (int j = 0; j < max_row.length; j++) {
System.out.println(max_row[j].x + ", " + max_row[j].y);
}
for (int col = 0; col < width; col++) {
int min = I[0][col];
IntPair minpair = new IntPair(0, col);
for (int row = 0; row < height; row++) {
if (I[row][col] < min) {
min = I[row][col];
minpair.x = row;
minpair.y = col;
}
}
min_col[col] = minpair;
}
System.out.println("mincol");
for (int j = 0; j < min_col.length; j++) {
System.out.println(min_col[j].x + ", " + min_col[j].y);
}
for (int row = 0; row < max_row.length; row++) {
for (int col = 0; col < min_col.length; col++) {
if (max_row[row].equals(min_col[col])) {
System.out.println("x: " + max_row[row].x + ", y: "
+ max_row[row].y + " value: "
+ I[max_row[row].x][max_row[row].y]);
}
}
}
}
public static void main(String[] args) {
// int input[][] = {
// { 9, 6, 8, 11 },
// { 7, 3, 1, 32 },
// { 10, 2, 5, 22 },
// { 12, 4, 2, 5 }
// };
int input[][] = {
{ 2, 3, 1},
{ 4, 5, 6},
{ 9, 8, 9 },
};
findSaddle(input);
}
}
I was trying to explain my idea, so I attached my code here. This would be more persuasive.
package EPIC;
class IntPair {
public int x;
public int y;
public IntPair(int X, int Y) {
x = X;
y = Y;
}
@Override
public boolean equals(Object other) {
IntPair that = (IntPair) other;
return this.x == that.x && this.y == that.y;
}
}
public class SaddleNumber {
public static void findSaddle(int I[][]) {
if (I == null) {
return;
} else if (I.length == 0) {
return;
}
int height = I.length;
int width = I[0].length;
IntPair max_row[] = new IntPair[height];
IntPair min_col[] = new IntPair[width];
for (int row = 0; row < height; row++) {
int max = I[row][0];
IntPair maxpair = new IntPair(row, 0);
for (int col = 0; col < width; col++) {
if (I[row][col] > max) {
max = I[row][col];
maxpair.x = row;
maxpair.y = col;
}
}
max_row[row] = maxpair;
}
System.out.println("Maxrow");
for (int j = 0; j < max_row.length; j++) {
System.out.println(max_row[j].x + ", " + max_row[j].y);
}
for (int col = 0; col < width; col++) {
int min = I[0][col];
IntPair minpair = new IntPair(0, col);
for (int row = 0; row < height; row++) {
if (I[row][col] < min) {
min = I[row][col];
minpair.x = row;
minpair.y = col;
}
}
min_col[col] = minpair;
}
System.out.println("mincol");
for (int j = 0; j < min_col.length; j++) {
System.out.println(min_col[j].x + ", " + min_col[j].y);
}
for (int row = 0; row < max_row.length; row++) {
for (int col = 0; col < min_col.length; col++) {
if (max_row[row].equals(min_col[col])) {
System.out.println("x: " + max_row[row].x + ", y: "
+ max_row[row].y + " value: "
+ I[max_row[row].x][max_row[row].y]);
}
}
}
}
public static void main(String[] args) {
// int input[][] = {
// { 9, 6, 8, 11 },
// { 7, 3, 1, 32 },
// { 10, 2, 5, 22 },
// { 12, 4, 2, 5 }
// };
int input[][] = {
{ 2, 3, 1},
{ 4, 5, 6},
{ 9, 8, 9 },
};
findSaddle(input);
}
}
your idea is good, but the last step of finding the saddle point is totally wrong. Finding the same value doesn't mean the same point, you need to compare the position (x, y) of the candidate points.
Also, you compare two arrays element by element, that you assume the min and max appear in the same sequence, which is absolutely wrong.
Think about example
2, 3, 1
4, 5, 6
9, 8, 7
the max array of rows is: 3, 6 ,9
the min array of cols is: 2, 3, 1
3 is the saddle point, however, your algorithm can't find that
package EPIC;
import java.util.StringTokenizer;
public class ReplaceAaONEone {
public static String getString(String s) {
StringTokenizer ST = new StringTokenizer(s);
String next = "";
String output = "";
while (ST.hasMoreElements()) {
next = ST.nextToken();
if (next.equals("A")) {
output += "ONE ";
} else if (next.equals("a")) {
output += "one ";
} else {
output += next + " ";
}
}
System.out.println(output);
return output;
}
public static void main(String args[]) {
getString("A boy is playing in a garden.");
}
}
package EPIC;
import java.util.LinkedList;
import java.util.List;
public class ExtracUniqueElements {
public static List<Integer> extractor(List<Integer> S) {
List<Integer> R = new LinkedList<>();
System.out.println(S.get(0));
R.add(S.get(0));
for (int i = 1; i <= S.size() - 1; i++) {
if (S.get(i - 1) != S.get(i)) {
System.out.println(S.get(i));
R.add(S.get(i));
}
}
return R;
}
public static void main(String[] args) {
List<Integer> L = new LinkedList<>();
L.add(1);
L.add(1);
L.add(3);
L.add(3);
L.add(3);
L.add(5);
L.add(5);
L.add(5);
L.add(5);
L.add(9);
System.out.println(L);
List<Integer> r = extractor(L);
System.out.println(r);
}
}
quick question, is this tedious question in real interview?
- albertchenyu March 05, 2015package EPIC;
public class Palindrome {
public static void printPalindrome(String S) {
boolean dp[][] = new boolean[S.length()][S.length()];
for (int i = 0; i <= dp.length - 1; i++) {
dp[i][i] = true;
}
for (int i = 0; i <= dp.length - 2; i++) {
if (S.charAt(i) == S.charAt(i + 1)) {
dp[i][i + 1] = true;
}
}
for (int diag = 1; diag <= dp.length - 1; diag++) {
for (int i = 0; i <= dp.length - diag - 1; i++) {
if ( S.charAt(i + diag) == S.charAt(i) && dp[i + diag - 1][i + 1] == true ) {
dp[i + diag][i] = true;
if (diag + 1 >= 3) {
System.out.println(S.substring(i, i + diag + 1));
}
}
}
}
pringDP(dp);
}
public static void pringDP(boolean dp[][]) {
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp.length; j++) {
if (dp[i][j]) {
System.out.print(1 + " ");
} else {
System.out.print(0 + " ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
// printPalindrome("abcbbde");
printPalindrome("cabbaabbacasdasdsdsdsdassadsadasdasadsadasda");
}
}
package EPIC;
public class Decimal2Fraction {
public static void decimal2fraction(double input, int numbits){
int numerator;
int denominator = 1;
for (int i = 0; i < numbits; i++) {
denominator *= 10;
}
numerator = (int) (denominator * input);
int GCD = EuclideanAlgorithm(numerator, denominator);
numerator /= GCD;
denominator /= GCD;
System.out.println(numerator + " / " + denominator);
}
public static int EuclideanAlgorithm(int a, int b){
while(a != b){
if (a > b) {
a -= b;
}else{
b -= a;
}
}
return a;
}
public static void main(String[] args) {
decimal2fraction(0.35, 4);
}
}
if you append an invalid char, let's say '@' at the end of the string, we don't need to worry about the last char.
- albertchenyu March 04, 2015If we add an invalid char by the end of the string, we don't need to worry about the last char. Given we are studying alphabet, so I add a space to the end, and the space won't continuous with any char.
package EPIC;
public class ContinuousAlphabets {
public static void printContiAlpha(String in){
String S = in.toLowerCase();
S = S + ' ';
char curr;
char next;
boolean continuous = false;
for (int i = 0; i <= S.length() - 2; i++) {
curr = S.charAt(i);
next = S.charAt(i + 1);
if(curr + 1 == next){
System.out.print(curr);
continuous = true;
}else if( continuous ){
System.out.print(curr + "; ");
continuous = false;
}
}
}
public static void main(String[] args) {
String in = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
printContiAlpha(in);
}
}
You method is great, but you forget to search all possible combinations.
my code is the following:
package EPIC;
public class SubStringAddition {
public static String checkifinsubstring(int [] arr, int sum)
{
String result = "";
for(int i=0;i<arr.length;i++)
{
int total = 0;
for(int j=i;j<arr.length;j++)
{
total = total+arr[j];
if (total == sum)
{
result += i+" - "+j + "\n";
}
}
}
if (result == "") {
return "no such substring";
}
return result;
}
public static void main(String[] args)
{
int [] arr = {1, 5, 9, 11, 2, 4, 9};
int sum = 150;
System.out.println(checkifinsubstring(arr, sum));
}
}
package EPIC;
import java.util.Stack;
public class BalancedString {
public static boolean isBalancedString(String S){
Stack<Character> Q = new Stack<>();
for (int i = 0; i < S.length(); i++) {
char x = S.charAt(i);
if( x == '(' ||
x == '[' ||
x == '{') {
Q.add(x);
continue;
}
else if (x == ')' && Q.pop() != '(') {
return false;
}else if(x == ']' && Q.pop() != '['){
return false;
}else if(x == '}' && Q.pop() != '{'){
return false;
}
}
return true;
}
public static void main(String[] args) {
// System.out.println(isBalancedString("(as{ asdfasdf[asdfas]d} f)"));
System.out.println(isBalancedString("(a[b]c)"));
System.out.println(isBalancedString("(a[a{cb]c}c)"));
}
}
package EPIC;
import java.util.Arrays;
public class RGBCompare {
public static void RGBcompare(String RGB){
int R = Integer.parseInt(RGB.substring(0,2), 16);
int G = Integer.parseInt(RGB.substring(2,4), 16);
int B = Integer.parseInt(RGB.substring(4,6), 16);
System.out.println("R: " + R + " G: " + G + " B: " + B);
int rgb[] = {R,G,B};
if(rgb[0] > rgb[1] ){
switchrgb(rgb, 0, 1);
}
if (rgb[1] > rgb[2]) {
switchrgb(rgb, 1, 2);
}
if (rgb[0] > rgb[1]) {
switchrgb(rgb, 0, 1);
}
System.out.println(Arrays.toString(rgb));
if (rgb[2] == rgb[0]) {
System.out.println("all of them are equal");
}else if(rgb[2] == rgb[1]) {
System.out.println("two equal maximum");
}else{
System.out.println("only one maximum");
}
}
public static void switchrgb(int rgb[], int i, int j){
int t = rgb[i];
rgb[i] = rgb[j];
rgb[j] = t;
}
public static void main(String[] args) {
RGBcompare("010203");
RGBcompare("030201");
RGBcompare("123312");
}
}
Thank you for your great job. But I think if we want to express a pixel
image(x,y)
in an image, the better way to find that in an array is
Array(x * width + y)
. Then we can assign the loop of rows by increase 1, and don't worry about the boundary thing. This maybe easier for me to understand. I noticed you manipulate the rows width by width. But there seems a little bug with this in the above code.
package EPIC;
public class EdgeDetection {
public static int[] processEdges(int[] image, int width, int threshold){
//throw out invalid input
if(image == null){
throw new NullPointerException();
}
if(width < 1){
throw new IllegalArgumentException();
}
if(image.length < width || image.length % width > 0){
throw new IllegalArgumentException();
}
//create output array and preprocess
final int[] output = new int[image.length];
final int numRows = image.length / width;
int xMin, xMax, yMin, yMax, maxDiff, index, pos;
//do the interior of the image
for(int row = 0; row < numRows; row++){
for(int col = 0; col < width; col++){
pos = image[row * width + col];
xMin = (col == 0) ? 0 : col -1;
xMax = (col == width -1) ? col : col + 1;
yMin = (row == 0) ? 0 : row - 1;
yMax = (row == numRows -1) ? row : row + 1;
if(meetsThreshold(xMin, xMax, yMin, yMax, width, image, pos, threshold)){
output[row * width + col] = pos;
}
}
}
return output;
}
private static boolean meetsThreshold(int xMin, int xMax, int yMin, int yMax,
int width, int[] image, int value, int threshold){
int temp;
for(int row = yMin; row <= yMax; row++){
for(int col = xMin; col <= xMax; col++){
temp = Math.abs(value - image[row * width + col]);
if(temp >= threshold){
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int width = 5;
int image[] = {
1,1,1,1,1,
1,9,1,1,1,
1,1,1,1,1,
1,1,1,1,1,
1,1,1,1,1,};
int out[] = processEdges(image, width, 2);
for (int i = 0; i < out.length; i++) {
if (i % width == 0) {
System.out.println();
}
System.out.print(" " + out[i]);
}
}
}
public class RemoveVowel {
public static boolean isVowel(char x){
if( x == 'a' || x == 'A' ||
x == 'e' || x == 'E' ||
x == 'i' || x == 'I' ||
x == 'o' || x == 'O' ||
x == 'u' || x == 'U' ){
return true;
}else{
return false;
}
}
public static String removeVowel( String s){
StringBuilder S = new StringBuilder(s);
int Vstart = -1;
int Vcounter = 0;
for (int i = 0; i < S.length(); i++) {
if (isVowel(S.charAt(i))) {
if( Vstart == -1 ){
Vstart = i;
}
Vcounter++;
}else if( Vstart != -1 ){
char y = S.charAt(i);
char z = S.charAt(Vstart);
S.setCharAt(Vstart, y);
S.setCharAt(i, z);
Vstart++;
}
}
return S.toString().substring(0,s.length() - Vcounter);
}
public static void main(String[] args) {
System.out.println(removeVowel("hello everyone, this is Java program"));
}
}
public static int[] removeDuplicates(int [] input){
HashSet<Integer> H = new HashSet<Integer>();
for (Integer integer : input) {
System.out.println(integer);
H.add(integer);
}
int result[] = new int[H.size()];
int counter = 0;
for(Integer x: H){
result[counter++] = x;
}
return result;
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#define MEMSIZE 32
#define BLOCKSIZE 4
#define MAX_Q 256
#define UNUSED 0
using namespace std;
static unsigned char data[MEMSIZE] = {0};
static bool IDavalability[MAX_Q] = {0};
void on_out_of_memory(){
cout <<"out of memory"<<endl;
exit(0);
}
void on_illegal_operation(){
cout <<"illegal operation"<<endl;
exit(0);
}
class Q {
public:
unsigned char myID;
int headIndex;
int tailIndex;
Q(unsigned char id){
myID = id;
}
};
int newBlock(Q * q){
for (int i = 0 ; i < MEMSIZE; i = i + 4) {
if(data[i] == UNUSED) {
data[i] = q->myID;
cout << "new block start point: "<< i <<endl;
return i + 1;
}
}
on_out_of_memory();
return -1;
}
unsigned char getAvailableID(){
for (int i = 1; i < MAX_Q; i++) {
if (IDavalability[i] == false) {
IDavalability[i] = true;
cout <<"get new id: "<< i<< endl;
return i;
}
}
return 0;
}
Q * creat_queue(){
Q * q = new Q(getAvailableID());
q->headIndex = q->tailIndex = newBlock(q);
cout<<"create queue # "<< (int)q->myID << " #"<<endl;
return q;
}
void destroy_queue(Q * q){
for (int i = 0 ; i < MEMSIZE; i = i + BLOCKSIZE) {
if (data[i] == q->myID) {
data[i] = UNUSED;
data[i + 1] = UNUSED;
data[i + 2] = UNUSED;
data[i + 3] = UNUSED;
}
}
IDavalability[q->myID] = false;
}
void enqueue_byte(Q * q, unsigned char b){
if (q->tailIndex%4 == 0) {
q->tailIndex = newBlock(q);
data[q->tailIndex] = b;
q->tailIndex++;
}else{
data[q->tailIndex] = b;
q->tailIndex++;
}
}
unsigned char dequeue_byte(Q * q){
unsigned char val;
if (q->headIndex == q->tailIndex ) {
cout << "queue underflow"<<endl;
return -1;
}else{
val = data[ q->headIndex ];
if (q->headIndex % 4 != 3) {
q->headIndex++;
}else{
data[q->headIndex & (~ 0b11)] = UNUSED;
data[q->headIndex & (~ 0b11) + 1] = UNUSED;
data[q->headIndex & (~ 0b11) + 2] = UNUSED;
data[q->headIndex & (~ 0b11) + 3] = UNUSED;
for (int i = 0; i < MEMSIZE; i = i + 4) {
if ( data[i + q->headIndex + 1] == q->myID ) {
q->headIndex = i + q->headIndex + 2;
cout << "head in new block: "<< q->headIndex << endl;
break;
}
}
}
}
return val;
}
void printDataArray(){
for (int i = 0; i < MEMSIZE; i++) {
cout << i<<":" << (int)data[i]<<endl;
}
}
int main(){
Q * q0 = creat_queue();
enqueue_byte(q0, 0);
enqueue_byte(q0, 1);
enqueue_byte(q0, 2);
enqueue_byte(q0, 3);
enqueue_byte(q0, 4);
enqueue_byte(q0, 5);
Q * q1 = creat_queue();
enqueue_byte(q1, 33);
enqueue_byte(q0, 6);
enqueue_byte(q1, 44);
Q * q2 = creat_queue();
destroy_queue(q0);
// printf("de: %d ", dequeue_byte(q0));
// printf("%d ", dequeue_byte(q0));
// printf("%d ", dequeue_byte(q0));
// printf("%d ", dequeue_byte(q0));
// printf("%d ", dequeue_byte(q0));
// printf("%d\n", dequeue_byte(q0));
// enqueue_byte(q0, 5);
// enqueue_byte(q1, 6);
//
// printf("%d ", dequeue_byte(q0));
// printf("%d\n", dequeue_byte(q0));
//
// destroy_queue(q0);
//
// printf("%d ", dequeue_byte(q1));
// printf("%d ", dequeue_byte(q1));
// printf("%d\n", dequeue_byte(q1));
// destroy_queue(q1);
printDataArray();
}
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
using namespace std;
class LineSeg {
public:
double x;
double y;
double z;
double l;
LineSeg(double X,double Y, double Z, double L){
x = X;
y = Y;
z = Z;
l = L;
}
};
class Circle {
public:
double centerX;
double centerY;
double centerZ;
double radius;
double normalX;
double normalY;
double normalZ;
Circle(double X, double Y, double Z,double R, double nx, double ny, double nz) {
centerX = X;
centerY = Y;
centerZ = Z;
radius = R;
normalX = nx;
normalY = ny;
normalZ = nz;
}
};
Circle * Configuration(LineSeg * L0, LineSeg * L1 ){
double dx = L0->x - L1->x;
double dy = L0->y - L1->y;
double dz = L0->z - L1->z;
double distance = sqrt( dx*dx + dy*dy + dz*dz);
//cosine theorem
double cos_of_L0_axis = (L0->l * L0->l + distance*distance - L1->l * L1->l )/ (2 * L0->l * distance );
double centerDis_from_L0 = cos_of_L0_axis * L0->l;
double sin_of_Lo_axis = sqrt(1 - cos_of_L0_axis * cos_of_L0_axis);
double radius_of_circle = sin_of_Lo_axis * L0->l;
//parameterized expression
double centerx = L0->x + dx * (centerDis_from_L0 / distance);
double centery = L0->y + dy * (centerDis_from_L0 / distance);
double centerz = L0->z + dz * (centerDis_from_L0 / distance);
if (distance > L0->l + L1->l) {
cout << "two points are too far from each other"<<endl;
return nullptr;
}
double dis = 0;
dis = L0->l - L1->l;
if(dis < 0){
dis = -dis;
}
if (distance < dis) {
cout<< "two points are too closed to each other" << endl;
return nullptr;
}
return new Circle( centerx, centery, centerz, radius_of_circle, dx,dy,dz);
}
#include<stdio.h>
#include <iostream>
using namespace std;
static int marker = 0;
class Node {
public:
int val;
int mark;
Node * p;
Node * leftchild;
Node * rightchild;
Node(int value, Node * parent){
val = value;
p = parent;
mark = 0;
leftchild = nullptr;
rightchild = nullptr;
}
void insterL(int val){
leftchild = new Node(val, this);
}
void insterR(int val){
rightchild = new Node(val, this);;
}
};
void printTree(Node * node){
if (node == nullptr) {
return;
}
printTree(node->leftchild);
marker++;
node->mark = marker;
cout << "node:" <<node->val << " #"<< node->mark <<endl;
printTree(node->rightchild);
}
void reverseTree(Node * node){
if (node == nullptr) {
return;
}
reverseTree(node->leftchild);
// cout << "node:" <<node->val <<endl;
if (node->p == nullptr) {
node->rightchild = nullptr;
}else{
node->rightchild = node->p->rightchild;
}
node->leftchild = node->p;
node->p = node->leftchild;
}
Node * getReverseTree(Node * node){
Node * temp = node;
while(temp->leftchild != nullptr){
temp = temp->leftchild;
}
reverseTree(node);
return temp;
}
int main()
{
Node * root = new Node(1, nullptr);
root->insterL(2);
root->insterR(3);
root->leftchild-> insterL(4);
root->leftchild-> insterR(5);
root->leftchild-> leftchild-> insterL(6);
root->leftchild-> leftchild-> insterR(7);
root->rightchild->insterR(-1);
printTree(getReverseTree(root));
return 0;
}
import java.util.Arrays;
public class ChangeBreakDown {
public static void main(String[] args) {
casher(10.25, 2000.0);
}
public static int units[] = { 10000, 5000, 1000, 500, 100, 25, 10, 5, 1 };
public static String unitsname[] = { "$100", "$50", "$20" };
public static void casher(double Worth, double Paid) {
int counter[] = new int[units.length];
int worthcents = (int) (Worth * 100);
int paidcents = (int) (Paid * 100);
int left = paidcents - worthcents;
int index = 0;
while (left > 0) {
counter[index] = left / units[index];
left = left % units[index];
index++;
}
System.out.println(Arrays.toString(units));
System.out.println(Arrays.toString(counter));
}
}
There's a tiny bug, but I don't know how to delete this blog.
- albertchenyu February 18, 2015public class AddativeNumber {
public static void main(String[] args) {
generator(123, 12412);
}
public static void generator(int start, int end) {
int halflength = (end + "").length() / 2;
int maxgenerator = (int) Math.pow(10, halflength);
for (int i = 1; i < maxgenerator; i++) {
for (int j = 1; j < maxgenerator; j++) {
fibonacci(start, end, i, j);
}
}
}
public static void fibonacci(int start, int end, int f, int s) {
// f = 1;
// s = 2;
String res = Integer.toString(f) + Integer.toString(s);
while (Integer.parseInt(res) < start) {
int temp = f + s;
res += temp;
f = temp;
}
while (Integer.parseInt(res) < end) {
int temp = f + s;
res += temp;
if (Integer.parseInt(res) < end)
System.out.println(res);
f = temp;
}
}
}
public class AddativeNumber {
public static void main(String[] args) {
generator(123, 12412);
}
public static void generator(int start, int end) {
for (int i = 1; i < end / 2; i++) {
for (int j = 1; j < end / 2; j++) {
fibonacci(start, end, i, j);
}
}
}
public static void fibonacci(int start, int end, int f, int s) {
// f = 1;
// s = 2;
String res = Integer.toString(f) + Integer.toString(s);
while (Integer.parseInt(res) < start) {
int temp = f + s;
res += temp;
f = temp;
}
while (Integer.parseInt(res) < end) {
int temp = f + s;
res += temp;
if (Integer.parseInt(res) < end)
System.out.println(res);
f = temp;
}
}
}
import java.util.ArrayList;
public class SnackNumber {
private static final int[][] board = new int[][] {
{ 1, 3, 2, 6, 8 },
{ -9, 7, 1, -1, 2 },
{ 1, 5, 0, 1, 9 } };
public static void snackchecker(int grid[][]) {
int maxlen = 0;
int endr = 0;
int endc = 0;
int[][] dp = new int[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
dp[i][j] = 1;
}
}
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (i == 0 && j == 0) {
continue;
}
if (i > 0 && Math.abs(board[i - 1][j] - board[i][j]) == 1) {
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j] + 1);
maxlen = Math.max(maxlen, dp[i][j]);
endr = i; endc = j;
}
if (j > 0 && Math.abs(board[i][j - 1] - board[i][j]) == 1) {
dp[i][j] = Math.max(dp[i][j], dp[i][j - 1] + 1);
maxlen = Math.max(maxlen, dp[i][j]);
endr = i; endc = j;
}
}
}
System.out.println("snack length: " + maxlen);
snacktrackback(dp, endr, endc);
}
public static void snacktrackback( int grid[][], int i, int j ){
ArrayList<Integer> track = new ArrayList<Integer>();
track.add(board[i][j]);
while(grid[i][j] != 1){
if(i > 0 && j > 0){
if(grid[i][j] == grid[i - 1][j] + 1){
track.add(board[i - 1][j]);
i--;
}else if(grid[i][j] == grid[i][j - 1] + 1){
track.add(board[i][j - 1]);
j--;
}
}else if(j == 0){
if(grid[i][j] == grid[i - 1][j] + 1){
track.add(board[i - 1][j]);
i--;
}
}else if (i == 0){
if(grid[i][j] == grid[i][j - 1] + 1){
track.add(board[i][j - 1]);
j--;
}
}
}
System.out.println(track);
}
public static void main(String[] args) {
snackchecker(board);
}
}
your algorithm is awesome, but there's a little bug, the condition for the while loop should be while (min_i < max_i || min_j > max_j);, otherwise, it doesn't work for even N(column or row).
- albertchenyu February 18, 2015import java.util.ArrayList;
import java.util.HashSet;
public class ColorfulNumber {
public static boolean iscolorfulnumber(int num) {
ArrayList<Integer> list = new ArrayList<Integer>();
int index = 0;
while (num != 0) {
int r = num % 10;
num = num / 10;
int length = list.size();
for (int i = index; i < length; i++) {
list.add(list.get(i) * r);
System.out.println(list.get(i) * r);
}
list.add(r);
System.out.println( r);
index = length;
}
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < list.size();i++) {
if (set.contains(list.get(i))) {
return false;
} else {
set.add(list.get(i));
System.out.println("set add: " + list.get(i) );
}
}
return true;
}
public static void main(String args[]) {
System.out.println(iscolorfulnumber(236));
}
}
- albertchenyu February 17, 2015if start with 0, it doesn't work well, al do you mean '1' will be map to '' or 'NULL' ?
- albertchenyu February 17, 2015
@ teargone08 thank you for you guys, here is my new code, the problem is I need to check whether the Queue is empty at last.
- albertchenyu March 10, 2015