Let’s continue… Problem Statement: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Note: Your algorithm should use only constant extra space. You may not modify the values in the list's nodes, only nodes itself may be changed.… Continue reading Swap Nodes in Pairs and Reverse Nodes in K Groups
Category: Articles
Merge K Sorted Lists
Let’s continue… Problem Statement: Merge K Sorted Lists 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 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input:… Continue reading Merge K Sorted Lists
Remove Nth Node from End of List and Generate Parentheses
Let’s continue… Problem Statement: Remove Nth Node from End of List 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. Note: Given n will always… Continue reading Remove Nth Node from End of List and Generate Parentheses
4 sums and letter combinations of a phone number
Let’s continue… Problem Statement: 4 Sums Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set… Continue reading 4 sums and letter combinations of a phone number
3 Sums and 3 Sums Closest
Let's continue... Problem Statement: 3 Sums 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… Continue reading 3 Sums and 3 Sums Closest
Container with Most Water and Longest Common Prefix
Problem Statement: Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the… Continue reading Container with Most Water and Longest Common Prefix
Regular_Expression _Matching
Let me continue.... Problem Statement: Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: s could be empty and contains only… Continue reading Regular_Expression _Matching
ZigZag && ATOI
OK, Let's continue.... ZIGZAG Problem Statement: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line… Continue reading ZigZag && ATOI
Longest_Palindromic_ Sub_String
OK, Let's continue.... Problem Statement: Given a string s, find the longest palindromic sub string 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 One: Brute Force Simple but not efficient.… Continue reading Longest_Palindromic_ Sub_String
Median_of_Two_ Sorted_Arrays
Median of Two Sorted Arrays Problem Statement: 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 =… Continue reading Median_of_Two_ Sorted_Arrays
Longest_Sub_String_ No_Repeat_Characters
Let me continue my work here. Longest substring without Repeating Characters Problem Statement: Given a string, find the length of the longest sub string without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", which the length is 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length… Continue reading Longest_Sub_String_ No_Repeat_Characters
Two_Sum and Add_Two_Numbers
I am trying to solve some algorithm problems in Leetcode recently. I plan to write down all the solutions either written by myself or found in the internet... I want to keep track of all these solutions and plan to write an article like this weekly, and hopefully, these articles could be helpful to you,… Continue reading Two_Sum and Add_Two_Numbers