【算法】 string 转 int

遇到的一道面试题, 当时只写了个思路, 现给出具体实现 ,算是一种比较笨的实现方式

    public class StringToInt
{
/// <summary>
/// 自己实现string转换成int
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int ToInt(string str)
{
if (string.IsNullOrWhiteSpace(str)) // 空字符串直接返回0
{
return ;
}
bool isMinus = false; // 是否是负数
int result = ; // 返回结果
for (int i = ; i < str.Length; i++)
{
int num = ToInt(str[i]);
if (num == -) // 不是数字
{
return ;
}
if (num == -) // 负数的情况
{
if (i == ) // 确实是负数
{
isMinus = true;
}
else // 字符串中间出现负号
{
return ;
}
}
checked // 检查算术溢出
{
result += ToTens(num, str.Length - i - );
} }
return isMinus ? - result : result; } /// <summary>
/// 将char转成int,注意-(负数),不能转的返回-1,负数返回-2
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static int ToInt(char c)
{
switch (c)
{
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '':
return ;
case '-':
return -;
}
return -;
} /// <summary>
/// 根据位数获取对应的数字
/// </summary>
/// <param name="i"></param>
/// <param name="index"></param>
/// <returns></returns>
private static int ToTens(int i, int index)
{
if (i <= )
{
return ;
}
for (int j = ; j < index; j++)
{
i = i * ;
}
return i;
}
}

【算法】 string 转 int的更多相关文章

  1. C#中String转int问题

    String转int主要有四种方法 1. int.Parse()是一种类容转换:表示将数字内容的字符串转为int类型. 如果字符串为空,则抛出ArgumentNullException异常: 如果字符 ...

  2. 编写Java应用程序。首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试Stud

    package zuoye; public class student { int age; String name; int stuNO; void outPut() { System.out.pr ...

  3. js string to int

    一.js中string转int有两种方式 Number() 和 parseInt() <script>     var   str='1250' ;  alert( Number(str) ...

  4. LeetCode 8 String to Integer (string转int)

    题目来源:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an ...

  5. java 13-4 Integer和String、int之间的转换,进制转换

    1.int类型和String类型的相互转换 A.int -- String 推荐用: public static String valueOf(int i) 返回 int 参数的字符串表示形式. B. ...

  6. swift 中String,Int 等类型使用注意,整理中

    swfit中的String和Int是 struct定义的,不同于NSString和NSNumber, 如果想在一个数组中同时包含String和Int,那么这个数组要声明为[Any] 而不是 [AnyO ...

  7. 首先,定义描述学生的类——Student,包括学号(int)、 姓名(String)、年龄(int)等属性;二个方法:Student(int stuNo,String name,int age) 用于对对象的初始化,outPut()用于输出学生信息。其次,再定义一个主类—— TestClass,在主类的main方法中创建多个Student类的对象,使用这些对象来测 试Student类的功能。

    package lianxi; public class Student { String Name; int XveHao,Age; Student(String Name,int XveHao,i ...

  8. caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.

    when I compile caffe file : .build_debug/lib/libcaffe.so: undefined reference to `cv::imread(cv::Str ...

  9. java中字符串String 转 int(转)

    java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...

随机推荐

  1. (第七场)A Minimum Cost Perfect Matching 【位运算】

    题目链接:https://www.nowcoder.com/acm/contest/145/A A.Minimum Cost Perfect Matching You have a complete ...

  2. 更改win7关机菜单选项功能

    说明:如果你不希望别人对你的电脑进行注销切换等操,那么可以使用如下的方法 实现效果:          实现步骤: 效果1 1>切换用户: 2>注销:(需重启资源管理器生效) 效果2:

  3. 【luogu P1231 教辅的组成】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1231 对于每本书只能用一次,所以拆点再建边 #include <queue> #include ...

  4. 【luogu P3388 割点(割顶)】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3388 #include <cstdio> #include <cstring> #i ...

  5. o'Reill的SVG精髓(第二版)学习笔记——第九章

    第九章:文本 9.1 字符:在XML文档中,字符是指带有一个数字值的一个或多个字节,数字只与Unicode标准对应. 符号:符号(glyph)是指字符的视觉呈现.每个字符都可以用很多不同的符号来呈现. ...

  6. dedecms基础整理,

    需求3: 在添加某个商品的时候,我们希望多一个信息,就是付费方式,还希望多一个邮资信息,我们又该怎样处理? 引出修改内容模型的问题 每个模型的字段管理的所有信息 都属于附加表. 步骤: 点击 核心-& ...

  7. 使用redux代码文件的组织方式

    从架构触发,开始一个新应用的时候,代码文件的组织方式一定要考虑好 如果之前使用过mvc的框架那么对按角色组织方式一定不陌生 角色组织方式 reducer/ todoReducer.js filterR ...

  8. 9.异常Exception

    9.1 异常概述 package exception; /* * 异常:程序运行的不正常情况 * * Throwable: 异常的超类 * |-Error * 严重问题,这种问题我们通过异常处理是不能 ...

  9. spring入门(七) spring mvc+mybatis+generator

    1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...

  10. ElasticSearch : 基础

    #新建索引以及类型: PUT http://10.18.43.3:9200/test { "settings": { "number_of_shards": 3 ...