前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

1、字符串转数字

字符串转数字

1.1、解题思路

利用 字符 - 48 (如 ‘1’ - 48 = 1)

1.2、算法

@Test
public void test() {
  String str = "1234";
  int res = 0;
  for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    int t = c - 48; //关键所在
    int b = 1;
    int j = str.length() - i;
    while (j > 1) {
      b *= 10;
      j--;
    }
    res = t * b + res;
  }
  System.out.println(res);
}

1.3、测试

1234

ContactAuthor