Day 23 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Invert Binary Tree
Link to the problem: https://leetcode.com/problems/invert-binary-tree/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null)
return null;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
Problem 2: Valid Palindrome
Link to the problem: https://leetcode.com/problems/valid-palindrome/

class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase();
s=s.trim();
s=s.replaceAll("[^a-zA-Z0-9]", "");
int i=0;
int j=s.length()-1;
while(i<=j)
if(s.charAt(i++)!=s.charAt(j--)) return false;
return true;
}
}
Problem 3: Binary Search
Link to the problem: https://leetcode.com/problems/binary-search/

class Solution {
public int search(int[] nums, int target) {
int low=0;
int high = nums.length-1;
while(low<=high){
int mid = low+(high-low)/2;
if(nums[low]==target)
return low;
else if(nums[high]==target)
return high;
else if(nums[mid]==target)
return mid;
else if(nums[mid]<target)
low=mid+1;
else
high=mid-1;
}
return -1;
}
}
Problem 4: Ransom Note
Link to the problem: https://leetcode.com/problems/ransom-note/

class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character, Integer> map = new HashMap<>();
for(char ch:magazine.toCharArray()){
if(map.containsKey(ch))
map.put(ch, map.get(ch)+1);
else
map.put(ch, 1);
}
for(char ch:ransomNote.toCharArray()){
if(map.containsKey(ch)){
if(map.get(ch)==1)
map.remove(ch);
else
map.put(ch, map.get(ch)-1);
}else
return false;
}
return true;
}
}
Problem 5: Climbing Stairs
Link to the problem: https://leetcode.com/problems/climbing-stairs/

class Solution {
public int climbStairs(int n) {
if(n==1)
return 1;
if(n==2)
return 2;
int num1 = 1;
int num2 = 2;
while(n-->2){
int temp = num2;
num2 += num1;
num1 = temp;
}
return num2;
}
}




