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