241. String to Integer
描述
- Given a string, convert it to an integer. * You may assume the string is a valid
- integer number that can be presented by a * signed 32bit integer (-231 ~ 231-1).
样例
给出 "123", 返回 123.
public class Solution {
/**
* @param str: A string
* @return: An integer
*/
public int stringToInteger(String str) {
// write your code here
return Integer.parseInt(str);
//return Integer.valueOf(str);
}
}
public class Solution {
/**
* @param str: A string
* @return: An integer
*/
public int stringToInteger(String str) {
// write your code here
int integer = 0;
int negative = 0;//1为true是负数,0为false是正数
if (str.charAt(0) == '-'){
negative = 1;
}
for (int i = negative; i < str.length(); i++){
integer = integer * 10 + str.charAt(i)-'0';
//假设str=1000
//i=0 integer=0*10+1=1;
//i=1 integer=1*10+0=10;
//i=2 integer=10*10+0=100;
//i=3 integer=100*10+0=1000;
}
if (negative == 1){
integer = -integer;
}
return integer;
}
}
241. String to Integer的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- String与Integer问题
今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
随机推荐
- Axure之全局变量
****全局变量*****1.定义:变量是一个数据的容器,是一个字符串,可设置默认值:2.功能:两个功能:读.写3.特点:随时随地可以对变量进行读和写,不限页面.也就是在不同的页面也可以访问同一个全局 ...
- 对象存储服务(Object Storage Service,简称 OSS)
阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务.它具有与平台无关的RESTful API接口,能够提供99.99 ...
- 常见的爬虫分析库(4)-爬虫之PyQuery
PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同. 官方文档:http://pyquery.readthedocs.io/ 安装 1 pip ins ...
- 复习reactnative....
import React, { Component } from 'react'; import { AppRegistry, Text, Image, View, TextInput, Scroll ...
- 我的第一个Java程序和Java简介
public calss HelloWorld{ public static void main(String[] args){ System.out.println("Hello Worl ...
- asp.net core WebApi 返回 HttpResponseMessage
ASP.NET WebApi 2 中的示例代码: [Route("values/{id}")] public async Task<HttpResponseMessage&g ...
- Zabbix监控——Zabbix自定义用户参数制作监控项
https://blog.51cto.com/183530300/2087774 https://www.cnblogs.com/richardzgt/articles/7889404.html
- 修改ini文件的批处理
用VBS更简单: vbs代码: On Error Resume Next Dim Fso,TxtFl,Str Set Fso = CreateObject("Scripting.FileSy ...
- python各个包的用途
python中的多个包的用途 1.Numpy Numpy提供了两种基本的对象:ndarray和ufunc.ndarray是存储单一数据类型的多维数组,而ufunc是能够对数组进行处理的函数. N维数组 ...
- ELK使用1-Elasticsearch使用
一.es 1.通过curl命令获取es进群信息 a.curl -i(设置协议的头信息) -XGET 'http:192.168.30.41:9200/_count' b.查看集群状态 curl -XG ...