forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditNum.java
More file actions
41 lines (36 loc) · 918 Bytes
/
EditNum.java
File metadata and controls
41 lines (36 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package CommonProblems.Dynamic;
/**
* @Author MaoTian
* @Classname EditNum
* @Description TODO
* @Date 下午4:05 2019/9/17
* @Version 1.0
* @Created by mao<[email protected]>
*/
public class EditNum {
public int helper(int a,int b){
if(a==b){
return 0;
}else {
StringBuilder sb=new StringBuilder(String.valueOf(a));
String s=sb.reverse().toString();
int tmp=Integer.parseInt(s);
if(tmp==b){
return 1;
}
int c=Math.abs(a-b);
int d=Math.abs(tmp-b);
if(c>d){
a=tmp;
}
int e=a>b?helper(a-1,b):helper(a+1,b);
return e+1;
}
}
public static void main(String[] args) {
int a=36925814;
int b=96925814;
EditNum e=new EditNum();
System.out.println(e.helper(a,b));
}
}