zoukankan      html  css  js  c++  java
  • 算法: 把字字符串转化为整数;

    算法: 把字字符串转化为整数;

    * @问题: 把字符串转化为整数
    * 题目描述
    * 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),
    * 要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
    * 输入描述:
    * 输入一个字符串,包括数字字母符号,可以为空
    * 输出描述:
    * 如果是合法的数值表达则返回该数字,否则返回0
    * @思路: 转化为数组,判断非空;然后根据数字0到9的ASIC码来确定,判断数组是否处于48到57;返回sum = sum*10 + 数组;

    package LG.nowcoder;
    
    /**
     * @Author liguo
     * @Description
     * @问题: 把字符串转化为整数
     * 题目描述
     * 将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),
     * 要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。
     * 输入描述:
     * 输入一个字符串,包括数字字母符号,可以为空
     * 输出描述:
     * 如果是合法的数值表达则返回该数字,否则返回0
     * @思路: 转化为数组,判断非空;然后根据数字0到9的ASIC码来确定,判断数组是否处于48到57;返回sum = sum*10 + 数组;
     * @Data 2018-09-15 9:41
     */
    public class Solution21 {
        public static int StrToInt(String str) {
            if (str.equals( "" ) || str.length() == 0)
                return 0;
            char[] a = str.toCharArray();
            int shouwei = 0;
            if (a[0] == '-')
                shouwei = 1;
            int sum = 0;
            for (int i = shouwei; i < a.length; i++) {
                if (a[i] == '+')
                    continue;
                if (a[i] < 48 || a[i] > 57)
                    return 0;
                sum = sum * 10 + a[i] - 48;
            }
            return shouwei == 0 ? sum : sum * -1;
        }
    
        public static void main(String[] args) {
            String test2 = "-2133";
            String test1 = "12334";
            System.out.println( Integer.valueOf( test2 ) );
            System.out.println( StrToInt( test2 ) );
        }
    }
     

  • 相关阅读:
    图像检索(image retrieval)- 11
    图像检索(image retrieval)- 10相关
    Mock.js简易教程,脱离后端独立开发,实现增删改查功能
    Azure Monitor (3) 对虚拟机磁盘设置自定义监控
    Azure Monitor (1) 概述
    Azure SQL Managed Instance (2) 备份SQL MI
    Azure Virtual Network (17) Private Link演示
    Azure Virtual Network (16) Private Link
    Azure Virtual Network (15) Service Endpoint演示
    Azure Virtual Network (14) Service Endpoint服务终结点
  • 原文地址:https://www.cnblogs.com/liguo-wang/p/9650098.html
Copyright © 2011-2022 走看看