作者:egg
邮箱:xtfggef@gmail.com
博客:http://blog.csdn.net/zhangerqing(转载请说明出处)
一、将字符串转成整型
字符串转整形是一个比较简单的算法,关键在于转换之前的一系列判断,
1、判断正负
2、去掉字符串中不能转化成整型的因素(包括各种符号、小数点、字母、空格)
3、去掉第一个数字前的所有0
4、结果是否超出整型范围(-2^31~2^31-1),处理
通过以上判断,我们得出了下面的程序:
- package com.xtfggef.algo.stringtoint;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * @time 2013/04/23
- * @author egg
- * @weibo http://weibo.com/xtfggef
- */
- public class StringToInt {
- static int flag = 1;
- static String count = "";
- static int value = 0;
- /**
- * get rid of the unuse char
- *
- * @param oldStr
- * @return
- */
- public static String judgeString(String oldStr) {
- String regEx = "[^0-9]";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(oldStr);
- String newStr = m.replaceAll("").trim();
- return newStr;
- }
- /**
- * judge first char for '+' or '-'
- *
- * @param oldStr
- * @return
- */
- public static int judgeFirst(String oldStr) {
- String ch = oldStr.substring(0, 1);
- if ("-".equals(ch)) {
- return -1;
- } else {
- return 1;
- }
- }
- /**
- * get rid of the '0' front of first number
- *
- * @param oldStr
- * @return
- */
- public static String getRidZero(String oldStr) {
- return oldStr.replaceAll("^(0+)", "");
- }
- /**
- * string to int core code
- *
- * @param count
- * @return
- */
- public static int stringToInt(String count) {
- char value[] = count.toCharArray();
- double sum = 0;
- for (int i = 0; i < value.length; i++) {
- sum = sum * 10 + value[i] - '0';
- }
- double tmp = sum * flag;
- if (tmp <= Integer.MAX_VALUE && tmp >= Integer.MIN_VALUE) {
- return (int) tmp;
- } else {
- return 0;
- }
- }
- public static void main(String[] args) {
- String s = "-07sadff65-=.,,,/,0849";
- /* judge the first char */
- flag = judgeFirst(s);
- /* get rid of the non-validity char */
- count = judgeString(s);
- /* get rid of the 0 from the first number */
- count = getRidZero(count);
- /* string 2 int */
- value = stringToInt(count);
- if (value != 0) {
- System.out.println(value);
- } else {
- System.out.println("value out of range of int!");
- }
- }
- }
二、判断ip的合法性
1、从.的数量来看,必须等于3个
2、每两个点儿之间的数必须在0~255之间
3、每个数必须<9且>0,且不能是非数字的字符
4、第一个、最后一个字符不能是“.”,且第一个亦不能为0
5、每小节第一个数不能是0
6、不能有连续的.
7、每节不能有连续的0,或者如果第一个为0,第二个不能为0
为了代码月的方便,我建了一个常量类:
- package com.xtfggef.algo.judgeip;
- public class Constant {
- public static String DOTMORETHANTHREE = "多于三个dot";
- public static String LESSTHANTHREE = "少于三个dot";
- public static String GREATTHAN = "单节不能大于255或者小于0";
- public static String ISZERO = "每节开头不能为0";
- public static String INVALIDCHAR = "无效的字符";
- public static String ERRORS = "第一个字符不能为0和.,最后一个不能为.!";
- public static String VALID = "IP 合法";
- public static String INVALID = "IP 不合法!";
- public static String TWODOTS = "不能有连续的dot";
- public static String CANNOTISZERO = "每节不能连续两个数为0,或者第一个为0但是第二个不为0";
- }
- package com.xtfggef.algo.judgeip;
- public class JudgeIp {
- static int is_valid_ip(char[] ip) {
- int section = 0;
- int dot = 0;
- int flag = 0;
- int flag2 = 0;
- int flag3 = 0;
- if (ip[0] != '.' && ip[0] != '0' && ip[ip.length - 1] != '.') {
- for (int i = 0; i < ip.length; i++) {
- if (flag == 1) {
- if (ip[i] == '.') {
- System.out.println(Constant.TWODOTS);
- return 0;
- }
- }
- if (ip[i] == '.') {
- dot++;
- if (dot > 3) {
- System.out.println(Constant.DOTMORETHANTHREE);
- return 0;
- }
- if (section >= 0 && section <= 255) {
- section = 0;
- } else {
- System.out.println(Constant.GREATTHAN);
- return 0;
- }
- flag = 1;
- flag2 = 0;
- flag3 = 0;
- } else if (ip[i] >= '0' && ip[i] <= '9') {
- flag2++;
- if (flag == 1) {
- if (ip[i] == '0') {
- flag3 = 1;
- }
- }
- if (flag2 == 2 && flag3 == 1) {
- System.out.println(Constant.CANNOTISZERO);
- return 0;
- }
- section = section * 10 + ip[i] - '0';
- flag = 0;
- } else {
- System.out.println(Constant.INVALIDCHAR);
- return 0;
- }
- }
- if (section >= 0 && section <= 255) {
- if (3 == dot) {
- section = 0;
- System.out.println("IP address success!");
- return 1;
- } else {
- System.out.println(Constant.LESSTHANTHREE);
- return 0;
- }
- }
- } else {
- System.out.println(Constant.ERRORS);
- return 0;
- }
- return 0;
- }
- public static void main(String[] args) {
- String ip = "23.252.49.22";
- System.out.println("ip:" + ip);
- char new_ip[] = ip.toCharArray();
- int flag = is_valid_ip(new_ip);
- if (flag == 1) {
- System.out.println(Constant.VALID);
- } else {
- System.out.println(Constant.INVALID);
- }
- }
- }
三、求最大公约数
这个非常简单,估计大多数人都能马上写出来:
- package com.xtfggef.algo.gcd;
- public class MaxGcd {
- public static void main(String[] args) {
- System.out.println(gcd(24, 12));
- }
- public static int gcd(int a, int b) {
- while (a != b) {
- if (a > b) {
- a = a - b;
- } else {
- b = b - a;
- }
- }
- return a;
- }
- }