Day 24 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: Contains Duplicate
Link to the problem: https://leetcode.com/problems/contains-duplicate/

class Solution {
public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for(int i:nums){
if(map.containsKey(i))
return true;
map.put(i, 1);
}
return false;
}
}
Problem 2: Move Zeroes
Link to the problem: https://leetcode.com/problems/move-zeroes/

class Solution {
public void moveZeroes(int[] nums) {
List<Integer> list = new ArrayList<>();
for(int i=0; i<nums.length; i++){
if(nums[i]==0)
list.add(i);
else{
if(list.isEmpty())
continue;
else{
int temp = nums[i];
nums[i--] = nums[list.get(0)];
nums[list.get(0)] = temp;
list.remove(0);
}
}
}
}
}
Problem 3: Squares of a sorted array
Link to the problem: https://leetcode.com/problems/squares-of-a-sorted-array/

class Solution {
public int[] sortedSquares(int[] nums) {
for(int i=0; i<nums.length; i++)
nums[i] *= nums[i];
Arrays.sort(nums);
return nums;
}
}
Problem 4: First Bad Version
Link to the problem: https://leetcode.com/problems/first-bad-version/

/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int low=0;
if(isBadVersion(1))
return 1;
while(low<n){
int mid = low+(n-low)/2;
if(isBadVersion(low)){
if(!isBadVersion(low-1))
return low;
}else if(isBadVersion(n)){
if(!isBadVersion(n-1))
return n;
}
if(isBadVersion(mid)){
if(!isBadVersion(mid-1))
return mid;
n=mid;
}else{
low=mid+1;
}
}
return -1;
}
}
Problem 5: Backspace String Compare
Link to the problem: https://leetcode.com/problems/backspace-string-compare/

class Solution {
public boolean backspaceCompare(String s, String t) {
Stack<Character> stack = new Stack<>();
for(char ch:s.toCharArray()){
if(ch!='#')
stack.push(ch);
else if(!stack.isEmpty())
stack.pop();
}
s = stack.toString();
stack.clear();
for(char ch:t.toCharArray()){
if(ch!='#')
stack.push(ch);
else if(!stack.isEmpty())
stack.pop();
}
t = stack.toString();
return s.equals(t)?true:false;
}
}




