Java 编写自己atoi()

Java 编写自己atoi()

atoi()函数用于C编程语言中,它被用来将作为参数传递的字符串转换为整数值,如果字符串是一个有效的整数,则转换成功,否则它会显示未定义行为。我们将在Java编程语言中实现atoi()函数。

输入:

string str = "123"

输出

123

解释

我们给定了一个表示数字的字符串,所以我们只需要得到相同的输出。

输入

string str = "897c7"

输出

Invalid Input

解释

给定的字符串不是有效的整数,所以我们给出了相应的输出。

输入

string str = "-123"

输出

-123

字符串有效性

在这个方法中,我们假设给定的字符串是有效的,并且它只包含数字,并且可能包含一个表示负数的”-“字符。

这个字符串在开头、中间或结尾都不会包含任何空格。

在这个方法中,我们首先会使用一个整数来存储答案,另外一个整数来标记当前数字是否为负数。

如果第一个字符是减号符号,我们将负整数标记为-1,并从第一个索引开始遍历字符串,否则我们将从第零个索引开始遍历。

在每个索引处,我们将当前数字乘以10,以增加一个小数点,然后将当前数字加到它上面。同时,我们将通过从字符串中获取当前数字来获取ASCII值,因此我们必须从当前字符中移除ASCII值为’0’的值。

最后,我们将返回最终答案,让我们看一下完整的代码:

示例

public class Solution{    
   // function to convert the string to an integer 
   public static int atoi(String str){
      // Assuming the string is valid 
      int neg = 1; // checking for the negative number         
      if(str.charAt(0) == '-'){
         neg = -1;
      }
      int ans = 0; 
      int i = 0; 
      // if the number is the negative number then start from the next index 
      if(neg  == -1){
         i++;
      }        
      for(; i < str.length(); i++){
         ans = ans * 10 + str.charAt(i) - '0';
      }        
      ans =  ans* neg;
      return ans; // returning the answer 
   }    
   public static void main(String []args){
      String str = "-354663"; // given string         
      // calling the function 
      int ans = atoi(str);        
      // printing the answer
      System.out.printf("The value of the current number is %d", ans);
   }
}

输出

The value of the current number is -354663

时间和空间复杂度

上述代码的时间复杂度是O(N),其中N是给定字符串中的字符数。但由于字符数的最大值可以为32,所以时间复杂度几乎是常数。

上述代码的空间复杂度为O(1),因为我们没有使用任何额外的空间。

字符串可能无效

在此程序中,我们将检查当前字符串是否可能无效,因此我们将加入两个条件,一个是检查当前字符串是否可能包含任何不是数字的其他字符,例如小写英文字符、大写英文字符、空格和特殊符号。

此外,我们已经实现了条件来检查字符串中表示的当前数字是否可能超出整数范围。所以在这种情况下,我们将返回当前数字溢出。对于这两种方法,其他条件是相同的。

示例

public class Solution{    
   // creating the function to convert the string to integer 
   public static void atoi(String str){
      int neg = 1; // checking for the negative number         
      if(str.charAt(0) == '-'){
         neg = -1;
      }
      int ans = 0; 
      int i = 0; 
      // if the number is the negative number than start from the next index 
      if(neg  == -1){
         i++;
      }        
      for(; i < str.length(); i++){
         // checking for the base conditions
         // if the current character is not a digit return the invalid answer 
         if(str.charAt(i) < '0' || str.charAt(i) > '9'){
            System.out.println("The given string represents the invalid number");
            return;
         }
         else if( (ans > Integer.MAX_VALUE / 10) || (ans == Integer.MAX_VALUE / 10  && str.charAt(i) - '0' > 7)){
           // overflow condition correct 
           System.out.println("The given string represents the number not in range of integer");
           return;
        }
        ans = ans * 10 + str.charAt(i) - '0';
     }
     ans =  ans* neg;        
     // printing the answer
     System.out.printf("The value of the current number is %d", ans);
   }    
   // main function 
   public static void main(String []args){
      String str = "-354663"; // given string         
      // calling the function 
      atoi(str);
   }
}

输出

The value of the current number is -354663

时间和空间复杂度

上述代码的时间复杂度为O(N),其中N是给定字符串中字符的数量。但由于字符的最大数量可以为32,所以时间复杂度几乎是恒定的。

上述代码的空间复杂度为O(1),因为我们没有使用任何额外的空间。

结论

在本教程中,我们实现了一个Java程序,将以字符串形式呈现的数字转换为整数。我们遍历了字符串,并检查当前字符串是否表示一个有效的数字。如果数字无效,我们将使用if-else条件来检查溢出和除数字以外的字符。上述代码的时间复杂度为O(N)。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程