C++ Cheat Sheet Leetcode

5 min read Jul 01, 2024
C++ Cheat Sheet Leetcode

C++ Cheat Sheet for LeetCode

LeetCode is a popular platform for practicing coding interview questions. While there are many resources available to learn about C++, here's a cheat sheet tailored specifically for solving LeetCode problems in C++.

1. Essential Data Structures and Algorithms

Data Structures

  • Arrays:
    • vector<int> nums = {1, 2, 3};: Create a vector of integers.
    • nums.size();: Get the size of the vector.
    • nums[i]: Access the element at index i.
    • nums.push_back(4);: Add an element to the end.
  • Linked Lists:
    • struct ListNode { int val; ListNode *next; };: Define a linked list node.
    • ListNode* head = new ListNode(1);: Create a new linked list node with value 1.
    • head->next = new ListNode(2);: Connect the nodes.
  • Stacks and Queues:
    • stack<int> st;: Create a stack.
    • st.push(1);: Push an element onto the stack.
    • st.top();: Access the top element.
    • st.pop();: Remove the top element.
    • queue<int> q;: Create a queue.
    • q.push(1);: Push an element to the back of the queue.
    • q.front();: Access the front element.
    • q.pop();: Remove the front element.
  • Hash Tables (Unordered Maps):
    • unordered_map<int, int> hash;: Create an unordered map with integer keys and values.
    • hash[1] = 2;: Insert or update a key-value pair.
    • hash.count(1);: Check if a key exists.
    • hash.erase(1);: Remove a key-value pair.

Algorithms

  • Sorting:
    • sort(nums.begin(), nums.end());: Sort a vector in ascending order.
    • sort(nums.begin(), nums.end(), greater<int>());: Sort a vector in descending order.
  • Binary Search:
    • lower_bound(nums.begin(), nums.end(), target);: Find the first element not less than target.
    • upper_bound(nums.begin(), nums.end(), target);: Find the first element greater than target.
  • Depth-First Search (DFS) and Breadth-First Search (BFS):
    • Often implemented recursively for DFS and iteratively for BFS using a stack/queue.

2. Useful C++ Techniques

  • auto: Automatically deduce data type. Example: auto sum = 1 + 2;
  • const: Declare a constant variable. Example: const int size = 10;
  • nullptr: Represents a null pointer.
  • std::swap: Swap the values of two variables.
  • std::max and std::min: Get the maximum or minimum of two values.
  • std::accumulate: Calculate the sum of elements in a range.
  • std::unique: Remove consecutive duplicates in a range.
  • std::reverse: Reverse the elements in a range.
  • std::find: Find the first occurrence of an element in a range.

3. Example LeetCode Problem (Two Sum)

#include 
#include 

using namespace std;

vector twoSum(vector& nums, int target) {
    unordered_map seen;
    for (int i = 0; i < nums.size(); ++i) {
        int complement = target - nums[i];
        if (seen.count(complement)) {
            return {seen[complement], i};
        }
        seen[nums[i]] = i;
    }
    return {};
}

4. Common Mistakes

  • Incorrectly handling edge cases: Remember to handle empty inputs, null pointers, or unexpected data.
  • Ignoring the constraints: Pay attention to limitations on the input size, range of values, or time complexity.
  • Misusing data structures: Choose the most appropriate data structure for the problem.
  • Not testing thoroughly: Test your code with different inputs, including edge cases.

This C++ cheat sheet aims to provide a starting point for tackling LeetCode problems. Remember to practice consistently and familiarize yourself with the specific requirements of each problem. Good luck!

Latest Posts