Leetcode 787 Cheapest Flights Within K Stops Java Solution

There are n cities connected by m flights. Each flight starts from city u and arrives at v with a price w. Now given all the cities and flights, together with starting city src and the destination dst, your task is to find the cheapest price from src to dst with up to k stops. If there is no…… Continue reading Leetcode 787 Cheapest Flights Within K Stops Java Solution

Leetcode 27 Remove Element Java Solution

Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn’t matter what you leave beyond the new length. Example 1: Given nums…… Continue reading Leetcode 27 Remove Element Java Solution

Leetcode 26 Remove Duplicates from Sorted Array Java Solution

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums…… Continue reading Leetcode 26 Remove Duplicates from Sorted Array Java Solution

Leetcode 24 Swap Nodes in Pairs Java Solution

Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list’s nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Solution 1: public class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.next == null) return…… Continue reading Leetcode 24 Swap Nodes in Pairs Java Solution

Leetcode 21 Merge Two Sorted Lists Java Solution

Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 Solution 1: public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1; ListNode p2 = l2; ListNode…… Continue reading Leetcode 21 Merge Two Sorted Lists Java Solution

Leetcode 20 Valid Parentheses Java Solution

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: “()” Output: true Example 2:…… Continue reading Leetcode 20 Valid Parentheses Java Solution

Leetcode 19 Remove Nth Node From End of List Java Solution

Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Solution 1: public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null) return…… Continue reading Leetcode 19 Remove Nth Node From End of List Java Solution

Leetcode 15 3 Sum

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution 1:…… Continue reading Leetcode 15 3 Sum

Leetcode 5 Longest Palindromic Substring Java Solution

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: “babad” Output: “bab” Note: “aba” is also a valid answer. Example 2: Input: “cbbd” Output: “bb” Solution 1: class Solution { public String longestPalindrome(String s) { if(s==null || s.length()<=1) return s; boolean dp[][] =…… Continue reading Leetcode 5 Longest Palindromic Substring Java Solution

Leetcode 4 Median of Two Sorted Arrays Java Solution

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3,…… Continue reading Leetcode 4 Median of Two Sorted Arrays Java Solution