Integer/String Conversions
来源:互联网 发布:java file.length单位 编辑:程序博客网 时间:2024/06/06 12:42
Integer/String Conversions
Write two conversion routines. The first routine converts a string to a signed integer. You may assume that the string only contains digits and the minus character (‘-’), that it is a properly formatted integer number, and that the number is within the range of an int type. The second routine converts a signed integer stored as anint back to a string.
class Solution { public static int strToInt(String s) { boolean isNeg = false; char[] str = s.toCharArray(); int i = 0; if (str[0] == '-') { isNeg = true; i = 1; } int num = 0; while (i < s.length()) { num = num * 10; num = num + (str[i++] - '0'); } if (isNeg) { num = num * (-1); } return num; } public static String IntToStr(int num) { boolean isNeg = false; int i = 0; int len = 0; int numTemp = num; if (num < 0) { num = num * -1; isNeg = true; len++; } do { numTemp = numTemp/10; len++; } while (numTemp != 0); char[] temp = new char[len]; do { temp[i++] = (char)((num % 10) + '0'); num = num / 10; } while (num != 0); StringBuilder b = new StringBuilder(); if (isNeg) { b.append('-'); } while (i > 0) { b.append(temp[--i]); } System.out.print(len); return b.toString(); } public static void main(String[] args) { int number = -2000; String str = IntToStr(number); System.out.print(str); }}
0 0
- Integer/String Conversions
- EPI 5.6 STRING AND INTEGER CONVERSIONS
- Understanding fast float/integer conversions
- string to integer & integer to string
- 【String转Integer】String to Integer (atoi)
- string to integer
- Freemarker string转Integer
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String to Integer (atoi)
- String和Integer回顾。
- [LeetCode]String to Integer
- string to integer (atoi)
- 模板方法 - 行为模式
- oracle数据类型
- C语言编码积累
- Python基础:28正则表达式
- 【CF】【312div2】【A Simple Task】
- Integer/String Conversions
- C语言初级之strstr、strcmp
- swift闭包总结
- C/C++的自动类型转换
- 每周前端知识整理(15.07.12)
- Java 队列 Queue、Deque、AbstractQueue 源码分析
- openwrt rt5350 SPI I2C驱动简单说明
- 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中
- jetty