Last active
December 21, 2015 04:12
-
-
Save junminstorage/cadf0ffcfaedfde8353a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void phoneNum2StringIter(int[] phone){ | |
int len = phone.length; | |
char[] curr = new char[phone.length]; | |
for(int i=0;i<phone.length; i++) | |
curr[i] = store.get(phone[i])[0]; | |
System.out.println(curr); | |
while(findNext(phone, curr)){ | |
System.out.println(curr); | |
} | |
} | |
private static boolean findNext(int[] phone, char[] curr){ | |
//we loop through from right | |
for(int i=phone.length-1;i>=0;i--){ | |
char[] chars = store.get(phone[i]); | |
int found = -1; | |
for(int index = 0; index<chars.length; index++) | |
if(chars[index] == curr[i]) | |
found = index; | |
if(found!=-1 && found+1<chars.length){ | |
//we find the index of curr[i] and set it to next char | |
curr[i] = chars[found+1]; | |
//also set all of the chars to its right to mimimum values | |
for(int j=i+1;j<phone.length;j++){ | |
curr[j] = store.get(phone[j])[0]; | |
} | |
return true; | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment