hengxingtianxia
BAN USERCompiled! considered from 0 to a given range.
public class TwoTypePalindrom {
public static void main(String[] args){
getTwoTypePalindrom(1000);
}
public static boolean isPalindrom(String s){
int i = 0;
int j = s.length() - 1;
while(i < j){
if (s.charAt(i) != s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
public static String DecimaltoOctal(int decimal){
StringBuilder sb = new StringBuilder();
int reminder;
while(decimal != 0){
reminder = decimal % 8;
decimal /= 8;
sb.append(String.valueOf(reminder));
}
return sb.toString();
}
public static void getTwoTypePalindrom(int bound){
for (int i = 0; i < bound; i++){
String test1 = Integer.toString(i);
if(isPalindrom(test1)){
String test2 = DecimaltoOctal(i);
if(isPalindrom(test2)){
System.out.println(i);
}
}
}
}
}
My solution by DP. Compiled!
public class SnakeSequence {
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 main(String[] args){
int max = 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);
max = Math.max(max, dp[i][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);
max = Math.max(max, dp[i][j]);
}
}
}
System.out.println(max);
}
}
I think I have a good solution by DFS and compiled.
public class ValidPhoneNumber {
public static void main(String[] args) {
String source = "0134568";
StringBuilder sb = new StringBuilder();
validNumber(source, sb, 4);
}
public static void validNumber(String source, StringBuilder sb, int size){
if(sb.length() == size) {
System.out.println(sb.toString());
return;
}
for(int i = 0; i < source.length(); i++){
if(sb.length() == 0 && source.charAt(i) == '4'){
continue;
}
if(sb.length() > 0){
if(sb.charAt(sb.length() - 1) == source.charAt(i) ){
continue;
}
}
sb.append(source.charAt(i));
validNumber(source, sb, size);
sb.deleteCharAt(sb.length() - 1);
}
}
}
I use DFS to solve this problem. compiled!
public class Numbers {
public static void main(String[] args){
int[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};
StringBuilder sb = new StringBuilder();
helper(num, 0, sb, 3);
}
public static void helper(int[] num, int pos, StringBuilder sb, int size){
if (sb.length() == size) {
System.out.println(sb.toString());
}
for (int i = pos; i < num.length; i++) {
sb.append(num[i]);
helper(num, i + 1, sb, size);
sb.deleteCharAt(sb.length() - 1);
}
}
}
public class FindPalindrome {
public static boolean isPalindrome(String s){
if (s.length() < 3) {
return false;
}
int start = 0;
int end = s.length() - 1;
while (start < end ) {
if (s.charAt(start) != s.charAt(end)){
return false;
}
start++;
end--;
}
return true;
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
String s = "abccbadddabccba";
helper(s, sb, 0);
}
public static void helper(String s, StringBuilder sb, int pos){
if (isPalindrome(sb.toString())){
System.out.println(sb.toString());
}
for( int i = pos; i < s.length(); i++) {
sb.append(s.charAt(i));
helper(s, sb, i + 1);
sb.deleteCharAt(sb.length() - 1);
}
}
}
hi,radioactive.
- hengxingtianxia October 20, 2014I know what you mean. But since we have to judge whether it is palindrom,there is no need to reverse sb. Because sb and sb.reverse() should be the same if it is palindrom.