Posts

Showing posts from 2024

Leetcode Problem(Easy) Roman to Integer

  Roman numerals are represented by seven different symbols:  I ,   V ,   X ,   L ,   C ,   D   and   M . Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example,  2  is written as  II  in Roman numeral, just two ones added together.  12  is written as  XII , which is simply  X + II . The number  27  is written as  XXVII , which is  XX + V + II . Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not  IIII . Instead, the number four is written as  IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as  IX . There are six instances where subtraction is used: I  can be placed before  V  (5) and  X  (10) to make 4 an...

Leetcode Problem (Easy) IsPalindrome

  Given an integer   x , return   true  if  x  is a  palindrome , and  false  otherwise .   Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.   Constraints: -2 31  <= x <= 2 31  - 1 Solution:- class Solution {     public boolean isPalindrome ( int x ) {             int temp = x;       int sum = 0 ;       int r  = 0 ;       if (x> 0 ){           while (x> 0 ){               r = x % 10 ;       ...

Leetcode : (Find Minimum in Rotated Sorted Array)

Image
   package com.demo.leetcode ; public class FindMinInRotatedArray { public static void main ( String [] args ) { int [] numArr = new int []{ 3 , 4 , 5 , 1 , 2 }; System . out . println ( "Min is = " + findMin ( numArr )); } private static int findMin ( int [] nums ) { int low = 0 ; int high = nums .length - 1 ; while ( low < high ){ int mid = low + ( high - low ) / 2 ; // just to escape integer overflow if ( nums [ mid ] > nums [ high ]){ low = mid + 1 ; } else { high = mid ; } } return nums [ low ]; } }