leetcode系列---atoiFunction C#code
Function:
/// <summary>
/// ToInt
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int atoi(string str)
{
string strNew = str.Trim();
string ch = strNew.Substring(0, 1);
string result = "";
int bl = 0;
double re;
bool flag = true;
for (int i = 1; i < strNew.Length; i++)
{
string s = strNew.Substring(i, 1);
if (int.TryParse(s, out bl))
{
if (Convert.ToInt32(s) == 0)
continue;
switch (ch)
{
case "0":
result = result + s.ToString();
break;
case "+":
result = result + s.ToString();
break;
case "-":
flag = false;
result = result + s.ToString();
break;
default:
if (i == 1)
result = result + ch.ToString();
result = result + s.ToString();
break;
}
}
else
break;
}
if (!string.IsNullOrEmpty(result))
{
re = Convert.ToDouble(result);
if (!flag)
re = -re;
if (re < int.MinValue || re > int.MaxValue)
re = 0;
}
else
re = 0;
return (int)re;
}
控制台展示:
static void Main(string[] args)
{
Console.WriteLine("请输入要转化的字符串:");
string str = Console.ReadLine();
int re = atoi(str);
if (re != 0)
Console.WriteLine("转化后:" + re);
else
Console.WriteLine("该字符串不能转换为整数!");
Console.ReadKey();
}
leetcode系列---atoiFunction C#code的更多相关文章
- leetcode系列---3Sum C#code
Function: public static List<int[]> SumSet(int[] array) { List<int[]> result = new List& ...
- CRL快速开发框架系列教程一(Code First数据表不需再关心)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
原文链接:http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx EF 6 Code-First系列文章目录 ...
- leetcode 系列文章目录
leetcode 系列文章目录 0. 两数之和1. 两数相加 2. 无重复字符的最长子串 3. 寻找两个有序数组的中位数 4. 最长回文子串 5. Z 字形变换 6. 整数反转 7. 字符串转换整数 ...
- C#刷遍Leetcode系列连载 索引
C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...
- Leetcode系列之两数之和
Leetcode系列之两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你 ...
- leetcode系列---Two Sum C#code
/// <summary> /// 方法一:双循环 /// </summary> /// <param name="array"></pa ...
- 【一天一道LeetCode】#89. Gray Code
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gra ...
- 8天掌握EF的Code First开发系列之2 Code First开发系列之领域建模和管理实体关系
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 理解Code First及其约定和配置 创建数据表结构 管理实体关系 三种继承模式 本章小结 本人的实验环境是V ...
随机推荐
- ExcelDataReader read excel file
上篇文章向大家介绍了用DocumentFormat.OpenXml.dll读取excel的方法,这里再向大家介绍一种轻量级简便的方法,用的是Excel.dll,及ICSharpCode.SharpZi ...
- FIT文件CRC校验
校验FIT文件CRC代码做个记录,分为两步先校验头部然后再校验整个FIT文件.校验头部不是必需的看个人需要吧.为了偷懒使用Okio库,还有计算CRC的时候用的Garmin的FitSDK. public ...
- 说说 Java 线程间通信
序言 正文 一.Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在一个 ...
- Spring Cloud Stream 整合 RabbitMQ
简介 Spring Cloud Stream是一个构建消息驱动微服务的框架,应用程序通过input(相当于consumer).output(相当于producer)来与Spring Cloud Str ...
- [Week 2][Guarantee of PLA] the Correctness Verification of PLA
Conditions: For the data set D, there exists a $\displaystyle W_{f}$ which satisfies that for every ...
- 什么是Affordance?
什么是Affordance? 在人机交互领域中,我们常常提到某个设计的affordance.其中文对应的意思并没有一个统一的意见.Wikipedia2上先这个词被译为“承担特质”或者“环境赋使”(非常 ...
- 从 axios 源码中了解到的 Promise 链与请求的取消
axios 中一个请求取消的示例: axios 取消请求的示例代码 import React, { useState, useEffect } from "react"; impo ...
- 最近太多人问Protobuf的问题了,把这个重新搬出来!
pb杀手 我先让pbkiller做个自我介绍 pbkiller:我是一位专业的争对 protobuf 问题训练有素的杀手,我可以为您轻松搞定 protobuf 在 Cocos Creaotr 开发中的 ...
- Python 爬虫入门实战
1. 前言 首先自我介绍一下,我是一个做 Java 的开发人员,从今年下半年开始,一直在各大技术博客网站发表自己的一些技术文章,差不多有几个月了,之前在 cnblog 博客园加了网站统计代码,看到每天 ...
- js悬浮、回到顶部
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...