Skip to content

Instantly share code, notes, and snippets.

View yitonghe00's full-sized avatar

YitongHe yitonghe00

View GitHub Profile
@yitonghe00
yitonghe00 / 28. Implement strStr() (#1 Two Pointers).java
Last active October 24, 2019 20:23
28. Implement strStr() (https://leetcode.com/problems/implement-strstr/submissions/): Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
// Two pointer solution: one pointer for each array
// Time:O(m * n) where m is the length of haystack and n is the length of needle, 3ms
// Space: O(1), 37mb
class Solution {
public int strStr(String haystack, String needle) {
// Corner case
if(needle.length() == 0) return 0;
// hp points to char in haystack, np points to char in needle
for(int hp = 0, np = 0; hp < haystack.length(); hp++) {
@yitonghe00
yitonghe00 / 75. Sort Colors (#1 Two pointer).java
Last active October 26, 2019 04:59
75. Sort Colors (https://leetcode.com/problems/sort-colors/): Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You…
// Two pointer solution
// Time: O(n), 0ms
// Space: O(1), 35.1mb
class Solution {
public void sortColors(int[] nums) {
// 2 pointers: [0, l) - 0, [l, r] - 1, [r, nums.length - 1] - 2
// m for traversing
int l = 0, r = nums.length - 1, m = 0;
while(m <= r) {
@yitonghe00
yitonghe00 / 80. Remove Duplicates from Sorted Array II (#1 Two pointer).java
Last active October 26, 2019 00:15
80. Remove Duplicates from Sorted Array II (https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/): Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place …
// Two pointer: read/write pointers (slow/fast pointers)
// Time: O(n), 0ms
// Space: O(1), 37.2mb
class Solution {
public int removeDuplicates(int[] nums) {
// Corner cases
if(nums.length < 3) return nums.length;
int w = 2, r = 2;
// Read pointer moves every loop
@yitonghe00
yitonghe00 / 88. Merge Sorted Array (#1 Two pointer).java
Last active October 26, 2019 03:04
88. Merge Sorted Array (https://leetcode.com/problems/merge-sorted-array/): Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold …
// Two pointer solution: read/write (end/end) pointer
// Time: O(m + n), 0ms
// Space: O(1), 36.3mb
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
// Two read pointers for nums1 and nums2, and 1 write pointer for nums1
int r1 = m - 1, r2 = n - 1, w = m + n - 1;
while(w >= 0) {
// **: Merge two into one: handle the cases when one of them is out of elements 658 #1.1
@yitonghe00
yitonghe00 / 125. Valid Palindrome (#1 Two pointer).java
Last active October 25, 2019 21:33
125. Valid Palindrome (https://leetcode.com/problems/valid-palindrome/): Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome.
// Two pointer solution
// Time: O(n), 4ms
// Space: O(1), 37.5mb
class Solution {
public boolean isPalindrome(String s) {
int l = 0, r = s.length() - 1;
while(l < r) {
// Ignore non alphanumeric characters
while(l < r && !Character.isLetterOrDigit(s.charAt(l))) {
@yitonghe00
yitonghe00 / 167. Two Sum II - Input array is sorted (#1 Two Pointers).java
Last active October 25, 2019 05:25
167. Two Sum II - Input array is sorted (https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/): Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, wher…
// Two pointer solution: begin/end pointer
// Time: O(n), 1ms
// Space: O(1), 37.9mb
class Solution {
public int[] twoSum(int[] numbers, int target) {
int left = 0, right = numbers.length - 1;
while(left < right) {
if(numbers[left] + numbers[right] == target) {
return new int[]{left + 1, right + 1};
@yitonghe00
yitonghe00 / 209. Minimum Size Subarray Sum (#1 Two pointer substring).java
Last active October 31, 2019 19:13
209. Minimum Size Subarray Sum (https://leetcode.com/problems/minimum-size-subarray-sum/): Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
// Two pointer solution: min/max substring template
// Time: O(n), 1ms
// Space: O(1), 37.9mb
class Solution {
public int minSubArrayLen(int s, int[] nums) {
// Window is [beign, end)
int begin = 0, end = 0, minLen = nums.length + 1, sum = 0;
while(end < nums.length) {
// Expand window to the right
sum += nums[end++];
@yitonghe00
yitonghe00 / 142. Linked List Cycle II (#1 Two pointer).java
Last active November 1, 2019 03:58
142. Linked List Cycle II (https://leetcode.com/problems/linked-list-cycle-ii/): Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, th…
// Two pointer solution: slow/fast pointers
// Time: O(n), 0ms
// Space: O(1), 36mb
public class Solution {
public ListNode detectCycle(ListNode head) {
// Slow fast pointer
ListNode slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
@yitonghe00
yitonghe00 / 141. Linked List Cycle (#1 Two pointer).java
Last active November 1, 2019 03:29
141. Linked List Cycle (https://leetcode.com/problems/linked-list-cycle/): Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
// Two pointer solution: slow/fast pointers
// Time: O(n), 0ms
// Space: O(1), 36.9mb
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(slow == fast) return true;
@yitonghe00
yitonghe00 / 287. Find the Duplicate Number (#1 Two pointer).java
Last active November 1, 2019 04:15
287. Find the Duplicate Number (https://leetcode.com/problems/find-the-duplicate-number/): Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
// Two pointer solution: slow/fast pointer
// Time: O(n), 0ms
// Space: O(1), 36.8mb
class Solution {
public int findDuplicate(int[] nums) {
// Treat the nums as a singly linked list
// The duplicate num is where the cycle begins
int slow = 0, fast = 0;
while(true) {
slow = nums[slow];