Java LeetCode Solutions Part 1

Abhishek vt
5 min readJan 7, 2024

--

Commonly asked interview questions

Hello all! In today’s session, we’ll tackle common interview questions sourced from LeetCode, offering straightforward explanations with simple examples. Stay tuned for an upcoming series of posts dedicated to various LeetCode solutions. These resources are tailored to assist individuals preparing for interviews, providing valuable insights and enhancing problem-solving skills. Whether you’re navigating technical interviews or seeking a comprehensive understanding of coding challenges, this series aims to be a valuable asset in your preparation for success.

  1. Two Sum
Taken screenshot from leetcode site

if a=3 c=9 a+b=c

3+b=9

b= 9–3=6

then b=6

We will follow the above logic to solve the problem. We will add the elements into Map and then check the target element by substracting with the keys. if the key found then return the array with indexes.

public static int[] twoSum(int[] nums, int target) {

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

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

int req = target - nums[i];

if (map.containsKey(req)) {

int arr[] = { map.get(req), i };
return arr;

}

map.put(nums[i], i);

}

return null;

}

2. Longest Substring without repeating the characters.

Taken screenshot from leetcode site
 public static int lengthOfLongestSubstring(String s) {

int start = 0;
int end = 0;
int maxLength = 0;
List<Character> list = new ArrayList<Character>();

while (end < s.length()) {

if (!list.contains(s.charAt(end))) {

list.add(s.charAt(end));
end++;
maxLength = Math.max(maxLength, list.size());

} else {
list.remove(Character.valueOf(s.charAt(start)));
start++;
}

}
return maxLength;
}

3. Median of two sorted arrays.

Taken screenshot from leetcode site
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

int n1 = nums1.length;
int n2 = nums2.length;
int merge[] = new int[n1 + n2];

int i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {

if (nums1[i] <= nums2[j]) {
merge[k] = nums1[i];
i++;
} else {
merge[k] = nums2[j];
j++;
}

k++;

}

while (i < n1) {
merge[k] = nums1[i];
i++;
k++;
}

while (j < n2) {
merge[k] = nums2[j];
j++;
k++;
}

int len = merge.length;

if (len % 2 == 0) {

return (merge[len / 2] + merge[len / 2 - 1]) / 2.0;

} else {
return merge[len / 2];
}

}

4. Reverse the integer

Taken screenshot from leetcode site
public static int reverse(int x) {
int rev = 0;
while (x != 0) {

int digit = x % 10;
if (rev > Integer.MAX_VALUE / 10 || rev < Integer.MIN_VALUE / 10) {
return 0;
}

rev = (rev * 10) + digit;

x = x / 10;

}

return rev;

}

5. Palindrome Number

Taken screenshot from leetcode site
 public static boolean isPalindrome(int s) {

int temp = s;
int rev = 0;
while (s > 0) {

int digit = s % 10;
rev = (rev * 10) + digit;
s = s / 10;

}

if (rev == temp) {
return true;
}

return false;
}

6. Longest common prefix

Taken screenshot from leetcode site
public static String longestCommonPrefix(String[] s) {

Arrays.sort(s);

String s1 = s[0];
String s2 = s[s.length - 1];
int i = 0;

while (i < s1.length()) {

if (s1.charAt(i) == s2.charAt(i)) {

i++;

} else {
break;
}

}

return i == 0 ? "" : s1.substring(0, i);

}

7. Remove Element

Taken screenshot from leetcode site
public int removeElement(int[] nums, int val) {

int count = 0;

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

if (nums[i] != val) {
nums[count] = nums[i];
count++;
}

}

return count;
}

8. Find the index of first occurrence in string.

Taken screenshot from leetcode site
public static int find(String haystack, String needle) {

for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {

if (needle.charAt(0) == haystack.charAt(i)) {

if (haystack.substring(i, needle.length() + i).equals(needle)) {
return i;

}

}

}

return -1;

}

9. Find the first and last position of element in sorted array.

screenshot taken from leetcode site
public static int[] searchRange(int[] nums, int target) {

int[] arr = new int[2];
arr[0] = -1;
arr[1] = -1;

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

if (nums[i] == target) {
if (arr[0] == -1) {
arr[0] = i;
}

arr[1] = i;
}

}

return arr;

}

10. Length of the last word.

Taken screenshot from leetcode site
public static  int lengthOfLastWord(String s) {

String[] words = s.trim().split(" ");

return words[words.length - 1].length();

}

Thank you for taking the time to read my article until the end. I sincerely hope that you have gained valuable insights and knowledge from it. If you found the article enjoyable and informative, I kindly ask you to share it with your friends and colleagues.

Part 2 will come soon…

--

--

Abhishek vt

👨‍💻Java & Blockchain Developer 🌐 | Trainer 📚 | Interview Coach 🎥 | Sharing tutorials and tips on Java ☕️ & Blockchain tech | www.abhishekvt.com