Java 程序编写自己的 atoi() 函数
在 C 编程语言 中,atoi() 函数用于将作为参数传递给它的字符串转换为整数值,如果字符串是有效的整数,则转换为整数值,否则会显示未定义的行为。我们将使用 Java 编程语言 实现 atoi() 函数。
示例场景 1
Input: string str = "123" Output: res = 123
给定一个表示数字的字符串,因此我们只需获得相同的输出。
示例场景 2
Input: string str = "897c7" Output: res = Invalid Input
给定的字符串不是有效的整数,因此我们给出了相应的输出。
示例场景 3
Input: string str = "-123" Output: res = -123
字符串有效
在这种方法中,我们将假设给定的字符串是有效的字符串,并且它仅包含数字,并且可能包含一个“-”字符,表示该数字是负数。
此字符串在开头、中间或结尾都不会包含任何空格。
这里,首先,我们将获取一个整数来存储答案,另一个整数来标记当前数字是否为负数。如果第一个字符是减号,我们将负整数标记为 -1 并从第一个索引遍历字符串,否则我们将从第 0 个索引遍历。
在每个索引处,我们将当前数字乘以 10 以增加一个小数位,然后将当前数字加到它上面。此外,我们将通过从字符串中获取当前数字来获取 ASCII 值,因此我们必须从当前字符中减去“0”的 ASCII 值。
示例
让我们看看 Java 中的实际实现:
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),因为我们没有使用任何额外的空间。
字符串可能无效
在这个 Java 程序中,我们将检查当前字符串是否可能无效,因此我们将设置两个条件,一个是检查当前字符串是否可能包含任何其他字符(而不是数字),例如小写英文字母、大写英文字母、空格和特殊符号。
此外,我们还实现了条件来检查字符串中表示的当前数字是否超出整数范围。因此,在这种情况下,我们将返回当前数字溢出。其他条件对于这两种方法都是相同的。
示例
以下是实际演示:
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)。
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP