Hashing to Solutions:
- Two Sum
- Longest Substring Without Repeating Characters
- Longest Palindromic Substring
- Container With Most Water
- 3Sum
- Remove Nth Node From End of List
- Valid Parentheses
- Merge Two Sorted Lists
- Merge k Sorted Lists
- Search in Rotated Sorted Array
- Combination Sum
- Rotate Image
- Group Anagrams
- Maximum Subarray
- Spiral Matrix
- Jump Game
- Merge Intervals
- Insert Interval
- Unique Paths
- Climbing Stairs
- Set Matrix Zeroes
- Minimum Window Substring
- Word Search
- Decode Ways
- Validate Binary Search Tree
- Same Tree
- Binary Tree Level Order Traversal
- Maximum Depth of Binary Tree
- Construct Binary Tree from Preorder and Inorder Traversal
- Best Time to Buy and Sell Stock
- Binary Tree Maximum Path Sum
- Valid Palindrome
- Longest Consecutive Sequence
- Clone Graph
- Word Break
- Linked List Cycle
- Reorder List
- Maximum Product Subarray
- Find Minimum in Rotated Sorted Array
- Reverse Bits
- Number of 1 Bits
- House Robber
- Number of Islands
- Reverse Linked List
- Course Schedule
- Implement Trie (Prefix Tree)
- Design Add and Search Words Data Structure
- Word Search II
- House Robber II
- Contains Duplicate
- Invert Binary Tree
- Kth Smallest Element in a BST
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Tree
- Product of Array Except Self
- Valid Anagram
- Meeting Rooms
- Meeting Rooms II
- Graph Valid Tree
- Missing Number
- Alien Dictionary
- Encode and Decode Strings
- Find Median from Data Stream
- Longest Increasing Subsequence
- Coin Change
- Number of Connected Components in an Undirected Graph
- Counting Bits
- Top K Frequent Elements
- Sum of Two Integers
- Pacific Atlantic Water Flow
- Longest Repeating Character Replacement
- Non-overlapping Intervals
- Serialize and Deserialize BST
- Subtree of Another Tree
- Palindromic Substrings
- Longest Common Subsequence
1. Two Sum
This code looks through a list to find two different numbers that add up to a specific target number. It does this by using two loops. The first loop starts at the beginning of the list. For each number in the first loop, the second loop looks at the numbers that come after it in the list. If it finds two numbers that add up to the target, it gives back their positions in the list.
Time Complexity: O(n^2)
This is because for each element in the list (and there are 'n' elements), we are potentially looking at every other element in the list to find a match (which in the worst case is 'n-1' other elements), resulting in 'n' times 'n-1' operations.
This code only uses a fixed amount of extra space (for the indices), no matter how big the list gets.
No comments:
Post a Comment