0% found this document useful (0 votes)
30 views6 pages

Java Solutions for LeetCode Problems

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Java Solutions for LeetCode Problems

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Winning Camp Worksheet (Java)


Day: 21
Name : Saurav Singha UID : 21BCS5421
Subject : Java Section : SC-904 (B)
Date : 21/06/2024

Problem 1: Duplicate Zeroes


Solution:
class Solution {
public void duplicateZeros(int[] arr) {
int[] tempArr = new int[[Link]];
for (int arrIndex = 0, tempIndex = 0; tempIndex < [Link]; arrIndex++)
{
tempArr[tempIndex] = arr[arrIndex];
if (tempArr[tempIndex++] == 0 && tempIndex < [Link]) {
tempArr[tempIndex++] = 0;
}
}
[Link](tempArr, 0, arr, 0, [Link]);
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2: Remove Linked List Elements


Solution:
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode sentinel = new ListNode(0);
[Link] = head;
ListNode prev = sentinel;
while (head != null) {
if ([Link] == val) {
[Link] = [Link];
} else {
prev = head;
}
head = [Link];
}
return [Link];
}
}

class ListNode {
int val;
ListNode next;
ListNode(int val) { [Link] = val; }
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 3: Shortest Subarray with Sum at Least K


Solution:
Output:
import [Link].*;
class Solution {
public int shortestSubarray(int[] nums, int k) {
int n = [Link];
Deque<Pair> dq = new ArrayDeque<>();
long sum = 0;
int shortest = Integer.MAX_VALUE;

for (int i = 0; i < n; ++i) {


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

sum += nums[i];
if (sum >= k) {
shortest = [Link](shortest, i + 1);
}
while (![Link]() && sum - [Link]().sum >= k) {
shortest = [Link](shortest, i - [Link]().index);
}
while (![Link]() && sum <= [Link]().sum) {
[Link]();
}
[Link](new Pair(sum, i));
}

return shortest == Integer.MAX_VALUE ? -1 : shortest;


}
static class Pair {
long sum;
int index;
Pair(long sum, int index) {
[Link] = sum;
[Link] = index;
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 4: Multiply Strings


Solution:
class Solution {
public String multiply(String num1, String num2) {
int n1 = [Link](), n2 = [Link]();
int[] products = new int[n1 + n2];
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
int d1 = [Link](i) - '0';
int d2 = [Link](j) - '0';
products[i + j + 1] += d1 * d2;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int carry = 0;
for (int i = [Link] - 1; i >= 0; i--) {
int temp = (products[i] + carry) % 10;
carry = (products[i] + carry) / 10;
products[i] = temp;
}
StringBuilder sb = new StringBuilder();
for (int num : products) {
[Link](num);
}
while ([Link]() != 0 && [Link](0) == '0') {
[Link](0);
}
return [Link]() == 0 ? "0" : [Link]();
}
}
Output:

You might also like