Saturday, 25 September 2021

Remove Duplicates from Sorted List - LeetCode 83

 Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

 

Example 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]

 

Solution(Only function):

class Solution {

    public ListNode deleteDuplicates(ListNode head) {

        

        ListNode temp=head;

        while(temp!=null && temp.next!=null )

        {

            if(temp.val==temp.next.val)

            {

                temp.next=temp.next.next;

            }

            else

            {

                temp=temp.next;

            }   

        }      

        return head;

    }

}

Saturday, 11 September 2021

Reverse Integer - LeetCode 7

 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

 

Example 1:

Input: x = 123
Output: 321

Example 2:

Input: x = -123
Output: -321

Example 3:

Input: x = 120
Output: 21

Source Code:

class Solution {

    public int reverse(int x) {

        

        int remainder;

        int reversed=0;

        

        while(x!=0)

        {

            remainder=x%10;

            

            x/=10;

            

            if(reversed>Integer.MAX_VALUE/10 || reversed==Integer.MAX_VALUE/10 && remainder>7)

                return  0;

            if(reversed<Integer.MIN_VALUE/10 || reversed==Integer.MIN_VALUE/10 && remainder<-8)

                return 0;

            

            reversed=(reversed*10) + remainder;

        }

        

        return reversed;

    }

}

Saturday, 31 July 2021

Roman to Integer - LeetCode 13

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 one's 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 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

 

Example 1:

Input: s = "III"
Output: 3

Example 2:

Input: s = "IV"
Output: 4

Example 3:

Input: s = "IX"
Output: 9

Example 4:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: s = "MCMXCIV"
Output: 1994 

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4

Solution:

Note: Only function is written.

class Solution {

    public int romanToInt(String s) {

        

        Map<Character,Integer> map=new HashMap<>();

        

        map.put('I',1);

        map.put('V',5);

        map.put('X',10);

        map.put('L',50);

        map.put('C',100);

        map.put('D',500);

        map.put('M',1000);

        

        int result=map.get(s.charAt(s.length()-1));

        

        for(int i=s.length()-2;i>=0;--i)

        {

            if(map.get(s.charAt(i))<map.get(s.charAt(i+1))){

                result-=map.get(s.charAt(i));

            }else{

                result+=map.get(s.charAt(i));

            }

        }

        

        return result;

    }

}




 

Friday, 30 July 2021

Two Sum II - Input array is sorted - LeetCode -118

 Given an array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number.

Return the indices of the two numbers (1-indexed) as an integer array answer of size 2, where 1 <= answer[0] < answer[1] <= numbers.length.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

 

Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]

Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]

 Constraints:

  • 2 <= numbers.length <= 3 * 104
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order.
  • -1000 <= target <= 1000
  • The tests are generated such that there is exactly one solution.

Solution:

Note: Only function is written

class Solution {

    public int[] twoSum(int[] numbers, int target) {


        int a_pointer=0,b_pointer=numbers.length-1;

        

        while(a_pointer <= b_pointer){

            

            int sum = numbers[a_pointer] + numbers[b_pointer];

            

            if(sum>target){

                b_pointer-=1;

            }else if(sum<target){

                a_pointer+=1;

            }else{

                return new int[] {a_pointer+1,b_pointer+1};

            }  

        }

        

         return new int[] {a_pointer+1,b_pointer+1};

    }

}

Thursday, 29 July 2021

Number of Islands - LeetCode 200

 Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

 

Example 1:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1

Example 2:

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is '0' or '1'

 

Solution:

Note: Only function is written

class Solution {

    public int numIslands(char[][] grid) {

        

        int count=0;

        

        for(int i=0;i<grid.length;i++){

            for(int j=0;j<grid[i].length;j++){

                if(grid[i][j]=='1'){

                    count+=1;

                    fun(grid,i,j);

                }

            }

        }

        return count;

        

    }

    

    public void fun(char[][] grid,int i,int j){

        if(i<0 || i>=grid.length || j<0 || j>=grid[i].length || grid[i][j]=='0')

            return;

        

        grid[i][j]='0';

        fun(grid,i-1,j);

        fun(grid,i+1,j);

        fun(grid,i,j-1);

        fun(grid,i,j+1);    

       }

}

Output:

Your input
[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
Output
1
Expected
1

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...