Java面试宝典系列之字符串转整型、判断IP合法性、求最大公约数

 

Java面试宝典系列之字符串转整型、判断IP合法性、求最大公约数


作者:egg

邮箱:xtfggef@gmail.com

微博:http://weibo.com/xtfggef

博客:http://blog.csdn.net/zhangerqing(转载请说明出处)

一、将字符串转成整型

字符串转整形是一个比较简单的算法,关键在于转换之前的一系列判断,

1、判断正负

2、去掉字符串中不能转化成整型的因素(包括各种符号、小数点、字母、空格)

3、去掉第一个数字前的所有0

4、结果是否超出整型范围(-2^31~2^31-1),处理

通过以上判断,我们得出了下面的程序:

[java]  view plain copy
  1. package com.xtfggef.algo.stringtoint;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. /** 
  7.  * @time 2013/04/23 
  8.  * @author egg 
  9.  * @weibo http://weibo.com/xtfggef 
  10.  */  
  11. public class StringToInt {  
  12.   
  13.     static int flag = 1;  
  14.     static String count = "";  
  15.     static int value = 0;  
  16.   
  17.     /** 
  18.      * get rid of the unuse char 
  19.      *  
  20.      * @param oldStr 
  21.      * @return 
  22.      */  
  23.     public static String judgeString(String oldStr) {  
  24.         String regEx = "[^0-9]";  
  25.         Pattern p = Pattern.compile(regEx);  
  26.         Matcher m = p.matcher(oldStr);  
  27.         String newStr = m.replaceAll("").trim();  
  28.         return newStr;  
  29.     }  
  30.   
  31.     /** 
  32.      * judge first char for '+' or '-' 
  33.      *  
  34.      * @param oldStr 
  35.      * @return 
  36.      */  
  37.     public static int judgeFirst(String oldStr) {  
  38.         String ch = oldStr.substring(01);  
  39.         if ("-".equals(ch)) {  
  40.             return -1;  
  41.         } else {  
  42.             return 1;  
  43.         }  
  44.     }  
  45.   
  46.     /** 
  47.      * get rid of the '0' front of first number 
  48.      *  
  49.      * @param oldStr 
  50.      * @return 
  51.      */  
  52.     public static String getRidZero(String oldStr) {  
  53.         return oldStr.replaceAll("^(0+)""");  
  54.     }  
  55.   
  56.     /** 
  57.      * string to int core code 
  58.      *  
  59.      * @param count 
  60.      * @return 
  61.      */  
  62.     public static int stringToInt(String count) {  
  63.         char value[] = count.toCharArray();  
  64.         double sum = 0;  
  65.         for (int i = 0; i < value.length; i++) {  
  66.             sum = sum * 10 + value[i] - '0';  
  67.         }  
  68.         double tmp = sum * flag;  
  69.         if (tmp <= Integer.MAX_VALUE && tmp >= Integer.MIN_VALUE) {  
  70.             return (int) tmp;  
  71.         } else {  
  72.             return 0;  
  73.         }  
  74.     }  
  75.   
  76.     public static void main(String[] args) {  
  77.   
  78.         String s = "-07sadff65-=.,,,/,0849";  
  79.   
  80.         /* judge the first char */  
  81.         flag = judgeFirst(s);  
  82.   
  83.         /* get rid of the non-validity char */  
  84.         count = judgeString(s);  
  85.   
  86.         /* get rid of the 0 from the first number */  
  87.         count = getRidZero(count);  
  88.   
  89.         /* string 2 int */  
  90.         value = stringToInt(count);  
  91.   
  92.         if (value != 0) {  
  93.             System.out.println(value);  
  94.         } else {  
  95.             System.out.println("value out of range of int!");  
  96.         }  
  97.     }  
  98. }  

二、判断ip的合法性

1、从.的数量来看,必须等于3个
2、每两个点儿之间的数必须在0~255之间
3、每个数必须<9且>0,且不能是非数字的字符
4、第一个、最后一个字符不能是“.”,且第一个亦不能为0

5、每小节第一个数不能是0

6、不能有连续的.

7、每节不能有连续的0,或者如果第一个为0,第二个不能为0

为了代码月的方便,我建了一个常量类:

[java]  view plain copy
  1. package com.xtfggef.algo.judgeip;  
  2.   
  3. public class Constant {  
  4.     public static String DOTMORETHANTHREE = "多于三个dot";  
  5.     public static String LESSTHANTHREE = "少于三个dot";  
  6.     public static String GREATTHAN = "单节不能大于255或者小于0";  
  7.     public static String ISZERO = "每节开头不能为0";  
  8.     public static String INVALIDCHAR = "无效的字符";  
  9.     public static String ERRORS = "第一个字符不能为0和.,最后一个不能为.!";  
  10.     public static String VALID = "IP 合法";  
  11.     public static String INVALID = "IP 不合法!";  
  12.     public static String TWODOTS = "不能有连续的dot";  
  13.     public static String CANNOTISZERO = "每节不能连续两个数为0,或者第一个为0但是第二个不为0";  
  14. }  
下面是判断类:

[java]  view plain copy
  1. package com.xtfggef.algo.judgeip;  
  2.   
  3.   
  4. public class JudgeIp {  
  5.   
  6.     static int is_valid_ip(char[] ip) {  
  7.   
  8.         int section = 0;  
  9.         int dot = 0;  
  10.         int flag = 0;    
  11.         int flag2 = 0;  
  12.         int flag3 = 0;  
  13.           
  14.         if (ip[0] != '.' && ip[0] != '0' && ip[ip.length - 1] != '.') {  
  15.             for (int i = 0; i < ip.length; i++) {  
  16.                 if (flag == 1) {  
  17.                     if (ip[i] == '.') {  
  18.                         System.out.println(Constant.TWODOTS);  
  19.                         return 0;  
  20.                     }  
  21.                 }  
  22.                 if (ip[i] == '.') {  
  23.                     dot++;  
  24.                     if (dot > 3) {  
  25.                         System.out.println(Constant.DOTMORETHANTHREE);  
  26.                         return 0;  
  27.                     }  
  28.                     if (section >= 0 && section <= 255) {  
  29.                         section = 0;  
  30.                     } else {  
  31.                         System.out.println(Constant.GREATTHAN);  
  32.                         return 0;  
  33.                     }  
  34.                     flag = 1;  
  35.                     flag2 = 0;  
  36.                     flag3 = 0;  
  37.   
  38.                 } else if (ip[i] >= '0' && ip[i] <= '9') {  
  39.   
  40.                     flag2++;  
  41.                     if (flag == 1) {  
  42.                         if (ip[i] == '0') {  
  43.                             flag3 = 1;  
  44.                         }  
  45.                     }  
  46.                     if (flag2 == 2 && flag3 == 1) {  
  47.                         System.out.println(Constant.CANNOTISZERO);  
  48.                         return 0;  
  49.                     }  
  50.                     section = section * 10 + ip[i] - '0';  
  51.                       
  52.                     flag = 0;  
  53.                 } else {  
  54.                     System.out.println(Constant.INVALIDCHAR);  
  55.                     return 0;  
  56.                 }  
  57.             }  
  58.             if (section >= 0 && section <= 255) {  
  59.                 if (3 == dot) {  
  60.                     section = 0;  
  61.                     System.out.println("IP address success!");  
  62.                     return 1;  
  63.                 } else {  
  64.                     System.out.println(Constant.LESSTHANTHREE);  
  65.                     return 0;  
  66.                 }  
  67.             }  
  68.         } else {  
  69.             System.out.println(Constant.ERRORS);  
  70.             return 0;  
  71.         }  
  72.         return 0;  
  73.     }  
  74.   
  75.     public static void main(String[] args) {  
  76.         String ip = "23.252.49.22";  
  77.         System.out.println("ip:" + ip);  
  78.         char new_ip[] = ip.toCharArray();  
  79.         int flag = is_valid_ip(new_ip);  
  80.         if (flag == 1) {  
  81.             System.out.println(Constant.VALID);  
  82.         } else {  
  83.             System.out.println(Constant.INVALID);  
  84.         }  
  85.     }  
  86. }  
感觉逻辑不多,但是判断起来还有点儿麻烦,总的来说暂时我是想不出什么问题了,大家去试试,多找几种情况,欢迎提出建议!


三、求最大公约数
这个非常简单,估计大多数人都能马上写出来:

[java]  view plain copy
  1. package com.xtfggef.algo.gcd;  
  2.   
  3. public class MaxGcd {  
  4.     public static void main(String[] args) {  
  5.         System.out.println(gcd(2412));  
  6.     }  
  7.   
  8.     public static int gcd(int a, int b) {  
  9.         while (a != b) {  
  10.             if (a > b) {  
  11.                 a = a - b;  
  12.             } else {  
  13.                 b = b - a;  
  14.             }  
  15.         }  
  16.         return a;  
  17.     }  
  18. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值