不重复数(一道编程题)

Problem Statement:
如果一个数字十进制表达时,不存在连续两位数字相等,则称之为“不重复数”。例如,105,1234和12121都是“不重复数”,而11,100和1225不算。给定一个long类型数字A,返回大于A的最小“不重复数”。
Definition:
Class: UnrepeatingNumbers
Method: next
Parameters: long
Returns: long
Method signature: long next(long A)
(be sure your method is public)

Constraints:
A 取值范围是[0, 10^17],注意是闭区间。
Examples:
0) 54
returns: 56
大于54的最小数字是55,但55不是“不重复数”。下一个数字是56,它满足条件。

1) 10
returns: 12

2) 9
returns: 10

3) 98
returns: 101
99和100都不是“不重复数”, 101是。

4) 21099
returns: 21201

public class UnrepeatingNumbers {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UnrepeatingNumbers un = new UnrepeatingNumbers();
//		System.out.println(un.next(54));
//		System.out.println(un.next(10));
//		System.out.println(un.next(9));
//		System.out.println(un.next(98));
//		System.out.println(un.next(21099));
		long num = Long.parseLong("1010");
		System.out.println(un.next(num));
	}
	
	public long next(long A){
		long num = 0;
		
		String s = "";
		boolean found = false;
		long max = Long.parseLong("100000000000000000");
		if(A<0 || A > max){
			System.out.println("Out of Range!!!!");
			return 0;
		}
		while(!found){
			A++;
			System.out.println("A:"+A);
			s = String.valueOf(A);
			if(isUnrepeat(s)){
				found = true;
			}
		}
		
		num = A;
		return num;
	}
	
	private boolean isUnrepeat(String s){
		for(int i=0;i<s.length()-1;i++){
			if(s.charAt(i)==s.charAt(i+1)){
				return false;
			}
		}
		return true;
	}
	

}

你可能感兴趣的:(编程)