Leetcode solutions to codes
// proble to reverse the integer number
import java.util.*;
import java.io.*;
import java.lang.*;
class Solution {
public int reverse(int x) {
int rev=0;
int pop;
while(x !=0){
pop = x%10;
if(rev>Integer.MAX_VALUE/10 || rev== Integer.MAX_VALUE && pop>7 )
return 0;
if(rev<Integer.MIN_VALUE/10 || rev== Integer.MIN_VALUE && pop<-8 )
return 0;
rev = (rev*10) + x%10;
x=x/10;
}
return rev;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
Solution s = new Solution();
s.reverse(x);
}
}
//check whether it is palindrome or not
class Solution {
public boolean isPalindrome(int x) {
int rev=0;
int y=x;
while(x>0){
rev = (rev*10) + x%10;
x=x/10;
}
if(y!=rev || x<0){
return false;
}
else
return true;
}
}
// roman to int
class Solution { public int romanToInt(String s) { int num=0,value=0,prev=0; for(int i=s.length()-1;i>=0;i--){ switch(s.charAt(i)){ case 'I': num=1; break; case 'V': num=5; break; case 'X': num=10; break; case 'L': num=50; break; case 'C': num=100; break; case 'D': num=500; break; case 'M': num=1000; break; } if(num<prev){ value -=num; } else value +=num; prev=num; } return value; } }
// Write a function to find the longest common prefix string amongst an array of strings.
// If there is no common prefix, return an empty string
""
.class Solution { public String longestCommonPrefix(String[] strs) { String s=""; int arrlenght=strs.length; int lowlenght=200; int c=0; char ch='a'; boolean b=true; for(String a : strs){ c=a.length(); if(c<lowlenght){ lowlenght=c; } } for(int i=0;i<c;i++){ for(int j=0;j<arrlenght;j++){ try{ if(j==0){ ch= strs[j].charAt(i); } else{ if(ch!=strs[j].charAt(i)){ b=false; return s; } } } catch(Exception e){ return s; } } s=s+ch; } return s; } }
import java.util.*; class Solution { public boolean isValid(String s) { Stack<Character> st = new Stack<>(); Character ch1; if(s.length()%2 !=0){ return false; } for(char ch : s.toCharArray()){ if(ch=='(' || ch=='[' || ch=='{'){ st.push(ch); } else if(!st.isEmpty()){ switch(ch){ case ')': if(st.pop()!='('){ return false; } break; case ']': if(st.pop()!='['){ return false; } break; case '}': if(st.pop()!='{'){ return false; } break; default : return false; } } else return false; } return st.isEmpty(); } }
// write a function to check the parenhtesis are matched or not in given string import java.util.*; class Solution { public boolean isValid(String s) { Stack<Character> st = new Stack<>(); Character ch1; if(s.length()%2 !=0){ return false; } for(char ch : s.toCharArray()){ if(ch=='(' || ch=='[' || ch=='{'){ st.push(ch); } else if(!st.isEmpty()){ switch(ch){ case ')': if(st.pop()!='('){ return false; } break; case ']': if(st.pop()!='['){ return false; } break; case '}': if(st.pop()!='{'){ return false; } break; default : return false; } } else return false; } return st.isEmpty(); } }
// NOTE: though you have written return statement in the cases, as it is switch case you have to use the brake statement
if break is not used then every case will be checked
Comments
Post a Comment