A copy of the actual parameter is passed to the function.
Changes made to the parameter inside the function do not reflect in the original variable.
Example:
Call by Reference:
The actual memory location (address) of the variable is passed to the function.
Changes made inside the function affect the original variable.
Example:
Pass by Reference in C++
Pass by reference allows a function to modify a variable directly by passing its reference (address). This avoids creating a copy of the variable, thus being more memory efficient.
Syntax Example:
Solutions for Problems
1. Two Sum
Using int[]:
Using vector (LeetCode way):
What is a Class?
A class is like a blueprint for creating objects.
It can hold:
Data: Variables (like vector<int> nums).
Functions: Methods that operate on the data (like solving a problem).
Think of it as a way to organize code better.
Structure of a Class for LeetCode Problems
Here’s a minimal setup:
Step-by-Step: LeetCode "Two Sum" Example
Code Explanation
Define a Class: Create a ProblemSolver class.
Add a Function: Add a method twoSum that solves the problem.
Call the Function: Use the class to call the function.
Code
Key Points for Beginners
Class Basics:
public: Allows functions to be accessed outside the class.
Methods (functions inside a class) operate on the input provided.
Why Use Classes?
Keeps code neat.
Easier to reuse for multiple problems.
How to Call a Method?
Create an object: ProblemSolver solver;
Use the object to call the method: solver.twoSum(nums, target);