当前位置:网站首页>6.2- convert string to integer
6.2- convert string to integer
2022-07-21 16:36:00 【syc596】
Convert a string to an integer _ Niuke Tiba _ Cattle from (nowcoder.com)
// Need to put string->char[]
// because string Low level maintenance value The array is final Modification cannot be changed
//ASCII clock
//'0'-48
//'1'-49
import java.util.*;
public class Solution {
public int StrToInt(String str){
if(str.isEmpty()){
return 0;
}
char[] ch=str.toCharArray();
//② Possible in string +/- And can only appear at the beginning of the string .
// Deal with negative numbers
int flg=1;
if(ch[0]=='-'){
flg=-1;
ch[0]='0';
}else if(ch[0]=='+'){
ch[0]='0';
}
// Business logic
int sum=0;
for(int i=0;i<ch.length;i++){
//① Any symbol... May appear in the string , Occurrence Division +/- Output directly when other symbols 0
if(ch[i]<'0'||ch[i]>'9'){
sum=0;
break;
}
sum=sum*10+ch[i]-'0';
}
return flg*sum;
}
}
边栏推荐
- JZ76 删除链表中重复的结点
- Swiftui uses spritekit to create rain animation effects (tutorial includes source code)
- deadlock
- PHP cross domain solution
- [super easy to understand competition questions] 2021 Sichuan University Student Program Design Competition
- In the standard mode, the SQL node of the production environment can perform routine_ sql_ test_ Tianyi conducts sel
- 一文解析Ansible配置管理文件
- Is there any risk in opening a mobile stock account? Is it safe?
- Several stumbling blocks of it entrepreneurs
- JZ22 链表中倒数最后k个结点
猜你喜欢
Vr virtual reality display of industrial manufacturing plant, real three-dimensional presentation to customers
[stc15 controls ws2812 RGB color lamp cascade]
Use priority queue_ Queue implements unordered to base_ Value filtering of map on value value value
How cloud primitives support enterprise IT governance | Alibaba cloud user group
What information can the real-time market of London Silver bring to people
In the e-commerce industry with fierce competition, why can the chain 2+1 mode survive for so long?
Signal integrity (SI) power integrity (PI) learning notes (XXIX) power distribution network (I)
This domestic API artifact tool is too strong Let me give up postman
L'ipaylinks, un service d'intégration des paiements transfrontaliers, a remporté le prix 3A Asia de l'actif!
Lora base station coverage
随机推荐
JZ27 二叉树的镜像
How to conduct user retention analysis?
Mohist patrol source code
Wang fried power to create wealth fast! Xiangling giant panda 2.0 power products officially launched
Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of the asset!
Code implementation of binary tree node number and leaf node number (C language)
Dama Chapter 11 (data warehouse and business intelligence)
At present, pytest, the hottest testing framework, is a magical explanation
deadlock
PHP cross domain solution
How to ensure data security while saving 30% disk space DB · insight
Signal integrity (SI) power integrity (PI) learning notes (xxx) power distribution network (II)
Realize joint testing through TPT fusion platform
6.2-把字符串转换成整数
Esp8266 nodemcu -- use u8g2 library to light OLED
6.1-WY16 不要二
[JS] event communication
JZ24 反转链表
2022 the 5th China (Jinan) International Youth eye health industry exhibition was held in September
[super easy to understand competition questions] 2021 Sichuan University Student Program Design Competition