-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2447c8e
commit f1b4131
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//{ Driver Code Starts | ||
//Initial template for JAVA | ||
|
||
import java.util.Scanner; | ||
|
||
class aToi | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
sc.nextLine(); | ||
while(t>0) | ||
{ | ||
String str = sc.nextLine(); | ||
|
||
Solution obj = new Solution(); | ||
int num = obj.atoi(str); | ||
|
||
System.out.println(num); | ||
t--; | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function template for JAVA | ||
|
||
/*You are required to complete this method */ | ||
class Solution | ||
{ | ||
int atoi(String s) | ||
{ | ||
// Your code here | ||
if(s.charAt(s.length()-1)=='-') | ||
return -1; | ||
|
||
for(int i=0;i<s.length();i++) | ||
{ | ||
try | ||
{ | ||
int a=Integer.parseInt(String.valueOf(s.charAt(i))); | ||
} | ||
catch(Exception e) | ||
{ | ||
if(s.charAt(i)=='-') | ||
continue; | ||
|
||
else | ||
return -1; | ||
} | ||
} | ||
|
||
return Integer.parseInt(s); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(log(high)) | ||
Space complexity - O(log(high)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(s) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Solution | ||
{ | ||
public List<Integer> sequentialDigits(int low, int high) | ||
{ | ||
List<Integer> ans = new ArrayList<>(); | ||
Queue<Integer> q = new ArrayDeque<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)); | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
int num = q.poll(); | ||
if (num > high) | ||
return ans; | ||
|
||
if (low <= num && num <= high) | ||
ans.add(num); | ||
|
||
int lastDigit = num % 10; | ||
if (lastDigit < 9) | ||
q.offer(num * 10 + lastDigit + 1); | ||
} | ||
|
||
return ans; | ||
} | ||
} |