0% found this document useful (0 votes)
10 views

LeetCode_Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

LeetCode_Solutions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Two Sum

#include <iostream>

#include <vector>

#include <unordered_map>

using namespace std;

class Solution {

public:

vector<int> twoSum(vector<int>& nums, int target) {

unordered_map<int, int> map;

for (int i = 0; i < nums.size(); i++) {

int complement = target - nums[i];

if (map.find(complement) != map.end()) {

return {map[complement], i};

map[nums[i]] = i;

return {};

};

int main() {

Solution solution;

vector<int> nums = {2, 7, 11, 15};

int target = 9;

vector<int> result = solution.twoSum(nums, target);


for (int index : result) {

cout << index << " ";

return 0;

}
2. Best Time to Buy and Sell Stock (121)

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

class Solution {

public:

int maxProfit(vector<int>& prices) {

int minPrice = INT_MAX, maxProfit = 0;

for (int price : prices) {

minPrice = min(minPrice, price);

maxProfit = max(maxProfit, price - minPrice);

return maxProfit;

};

int main() {

Solution solution;

vector<int> prices = {7, 1, 5, 3, 6, 4};

cout << "Max Profit: " << solution.maxProfit(prices) << endl;

return 0;

}
3. Contains Duplicate (217)

#include <iostream>

#include <vector>

#include <unordered_set>

using namespace std;

class Solution {

public:

bool containsDuplicate(vector<int>& nums) {

unordered_set<int> seen;

for (int num : nums) {

if (seen.find(num) != seen.end()) return true;

seen.insert(num);

return false;

};

int main() {

Solution solution;

vector<int> nums = {1, 2, 3, 1};

cout << (solution.containsDuplicate(nums) ? "Contains Duplicate" : "No Duplicates")

<< endl;

return 0;

}
4. Maximum Subarray (53)

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

class Solution {

public:

int maxSubArray(vector<int>& nums) {

int maxSum = nums[0], currentSum = nums[0];

for (int i = 1; i < nums.size(); i++) {

currentSum = max(nums[i], currentSum + nums[i]);

maxSum = max(maxSum, currentSum);

return maxSum;

};

int main() {

Solution solution;

vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};

cout << "Maximum Subarray Sum: " << solution.maxSubArray(nums) << endl;

return 0;

}
5. Palindrome Number (9)

#include <iostream>

using namespace std;

class Solution {

public:

bool isPalindrome(int x) {

if (x < 0 || (x % 10 == 0 && x != 0)) return false;

int revertedNumber = 0;

while (x > revertedNumber) {

revertedNumber = revertedNumber * 10 + x % 10;

x /= 10;

return x == revertedNumber || x == revertedNumber / 10;

};

int main() {

Solution solution;

int x = 121;

cout << (solution.isPalindrome(x) ? "Palindrome" : "Not a Palindrome") << endl;

return 0;

You might also like