Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 56 additions & 25 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run LeetCode Solution",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
},
{
"name": "Run All Tests",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/run_all_tests.py",
"args": [
"test-results.xml"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
"version": "0.2.0",
"inputs": [
{
"id": "problemNumber",
"description": "Enter the 3-digit problem number to DEBUG:",
"default": "001",
"type": "promptString"
}
],
"configurations": [
{
"name": "C++: Run All Tests",
"type": "cppdbg",
"request": "launch",
"program": "C:/Windows/System32/cmd.exe",
"preLaunchTask": "Run All Tests",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/c++"
},
{
"name": "C++: Debug Single Problem",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/c++/build/problem_${input:problemNumber}/runner.exe",
"args": [
"-f",
"problems/problem_${input:problemNumber}/problem_${input:problemNumber}.json"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/c++",
"MIMode": "gdb",
"miDebuggerPath": "C:/msys64/ucrt64/bin/gdb.exe",
// This is the only change: point to the new build task
"preLaunchTask": "C++: Build active problem for debugger",
"console": "integratedTerminal"
},
{
"name": "Python: Run Active File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${fileDirname}"
},
{
"name": "Python: Run All Tests",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/run_all_tests.py",
"args": [
"test-results.xml"
],
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
}
69 changes: 69 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"version": "2.0.0",
"inputs": [
{
"id": "problemNumber",
"description": "Enter the 3-digit problem number to build:",
"default": "001",
"type": "promptString"
}
],
"tasks": [
{
"label": "C++: Build active problem for debugger",
"type": "process",
"command": "C:/msys64/ucrt64/bin/g++.exe",
"args": [
"-g",
"-std=c++20",
"-Wall",
// --- Include Paths ---
"-IC:/msys64/ucrt64/include",
// CORRECTED: Added "problem_" prefix
"-Iproblems/problem_${input:problemNumber}",
"-Itests_runner",
// --- Source Files ---
"tests_runner/tests_runner.cpp",
// CORRECTED: Added "problem_" prefix
"problems/problem_${input:problemNumber}/solution.cpp",
// --- Output File ---
"-o",
"build/problem_${input:problemNumber}/runner.exe",
// --- Linker Flags ---
"-LC:/msys64/ucrt64/lib",
"-ljsoncpp",
"-static-libstdc++",
"-static-libgcc"
],
"options": {
"cwd": "${workspaceFolder}/c++"
},
"problemMatcher": ["$gcc"],
"group": "build"
},
{
"label": "Run All Tests",
"type": "process",
"command": "C:/msys64/usr/bin/bash.exe",
"args": [
"-ic",
"cd \"$(cygpath '${workspaceFolder}')/c++\" && make clean && make test-all"
],
"options": {
"env": {
"PATH": "C:\\msys64\\ucrt64\\bin;C:\\msys64\\usr\\bin;${env:PATH}"
}
},
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "dedicated",
"clear": true
},
"problemMatcher": ["$gcc"]
}
]
}
1 change: 1 addition & 0 deletions c++/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.exe
35 changes: 35 additions & 0 deletions c++/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# --- Compiler and Flags ---
CXX := C:/msys64/ucrt64/bin/g++.exe
CXXFLAGS := -O2 -std=c++20 -g -Wall -IC:/msys64/ucrt64/include
# Add the -static-libgcc flag to this line
LDFLAGS := -LC:/msys64/ucrt64/lib -ljsoncpp -static-libstdc++ -static-libgcc

# --- Project Structure ---
BUILD_DIR := build
TEST_RUNNER_SRC := tests_runner/tests_runner.cpp

# --- Auto-discovery of Problems ---
PROBLEM_DIRS := $(wildcard problems/problem_*)
PROBLEM_NUMS := $(patsubst problems/problem_%,%,$(PROBLEM_DIRS))

# --- Main Targets ---
.PHONY: test-all
test-all: $(addprefix test-, $(PROBLEM_NUMS))
@echo ""
@echo "✅ All tests completed."

.PHONY: test-%
test-%: $(BUILD_DIR)/problem_%/runner.exe
@echo "--- Running tests for Problem $* ---"
@$(BUILD_DIR)/problem_$*/runner.exe -f problems/problem_$*/problem_$*.json

# --- Build Rules ---
$(BUILD_DIR)/problem_%/runner.exe: $(TEST_RUNNER_SRC) problems/problem_%/solution.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) -Iproblems/problem_$* -Itests_runner $^ -o $@ $(LDFLAGS)

# --- Utility Targets ---
.PHONY: clean
clean:
@echo "Cleaning build directory..."
@rm -rf $(BUILD_DIR)
16 changes: 16 additions & 0 deletions c++/hello_world/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
26 changes: 26 additions & 0 deletions c++/problems/problem_001/problem_001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"method": "twoSum",
"tests": [
{
"Input": {
"nums": "[2,7,11,15]",
"target": "9"
},
"Output": "[0,1]"
},
{
"Input": {
"nums": "[3,2,4]",
"target": "6"
},
"Output": "[1,2]"
},
{
"Input": {
"nums": "[3,3]",
"target": "6"
},
"Output": "[0,1]"
}
]
}
23 changes: 23 additions & 0 deletions c++/problems/problem_001/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "solution.hpp"

// Standard LeetCode solution for Two Sum
std::vector<int> Solution::twoSum(std::vector<int>& nums, int target) {
// Input: nums = [2,7,11,15], target = 9
// Output: [0,1]
// Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

for(auto it=nums.begin(); it != nums.end(); ++it){
for(auto j=(it + 1); j != nums.end(); ++j){
if(*it + *j == target) {
auto retval = std::vector<int>();
int index = std::distance(nums.begin(), it);
int index2 = std::distance(nums.begin(), j);
retval.push_back(index);
retval.push_back(index2);
return retval;
}
}
}

return std::vector<int>(); // Should not happen for valid inputs
}
11 changes: 11 additions & 0 deletions c++/problems/problem_001/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef SOLUTION_HPP_
#define SOLUTION_HPP_

#include <vector>

class Solution {
public:
std::vector<int> twoSum(std::vector<int>& nums, int target);
};

#endif // SOLUTION_HPP_
Loading