Skip to content

Commit 07e3229

Browse files
authored
Add files via upload
1 parent 5dc2d26 commit 07e3229

8 files changed

+281
-0
lines changed

LongestCommonPrefix.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
string longestCommonPrefix(vector<string>& strs) {
9+
for (int i = 0; i < s.size(); i++ ) {
10+
if (i + 1 < strs.size(); )
11+
}
12+
}
13+
};
14+
15+
int main() {
16+
Solution function;
17+
vector<string> strs1 = {"flower","flow","flight"};
18+
vector<string> strs2 = {"dog","racecar","car"};
19+
}

LongestCommonPrefix.exe

196 KB
Binary file not shown.

RomantoInteger.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <map>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int romanToInt(string s) {
9+
map<char,int>roman_map;
10+
roman_map = {
11+
{'I', 1 },
12+
{'V', 5 },
13+
{'X', 10 },
14+
{'L', 50 },
15+
{'C', 100 },
16+
{'D', 500 },
17+
{'M', 1000 }
18+
};
19+
int t = 0;
20+
for (int i = 0; i < s.size(); i++ ){
21+
char m = s[i];
22+
23+
if (roman_map.find(m) != roman_map.end()) {
24+
int v = roman_map[m];
25+
if (i + 1 < s.size() && roman_map[s[i]] < roman_map[s[i+1]]) {
26+
t-=v;
27+
} else {
28+
t += v;
29+
}
30+
31+
cout << "m: " << m << "\n";
32+
cout << "map: " << v << "\n" << "total: " << t << "\n";
33+
}
34+
}
35+
return t;
36+
}
37+
};
38+
39+
40+
int main() {
41+
Solution solution;
42+
43+
string s1 = "III";
44+
int result1 = solution.romanToInt(s1);
45+
cout <<" Result 1: "<< result1 << "\n";
46+
string s2 = "LVIII";
47+
int result2 = solution.romanToInt(s2);
48+
cout <<" Result 2: "<< result2 << "\n";
49+
string s3 = "MCMXCIV";
50+
int result3 = solution.romanToInt(s3);
51+
cout <<" Result 3: "<< result3 << "\n";
52+
53+
return 0;
54+
}

RomantoInteger.exe

202 KB
Binary file not shown.

addTwoNumbers.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://www.geeksforgeeks.org/cpp-program-for-inserting-a-node-in-a-linked-list/
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool isPalindrome(int x) {
8+
if (x < 0) {
9+
return false;
10+
}
11+
long y = x;
12+
long s = 0;
13+
while (x != 0) {
14+
int z = x % 10;
15+
s = (s * 10) + z;
16+
x /= 10;
17+
}
18+
return y == s;
19+
}
20+
};
21+
22+
int main() {
23+
Solution solution;
24+
int x;
25+
cout << "Input number: ";
26+
cin >> x ;
27+
bool result = solution.isPalindrome(x);
28+
cout << (result ? "True" : "False") << endl;
29+
return 0;
30+
}

addTwoNumbers.exe

148 KB
Binary file not shown.

twosum.cpp

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_map>
4+
5+
std::vector<int> twoSum(const std::vector<int>& nums, int target) {
6+
// Step 1: Create an empty hash map
7+
std::unordered_map<int, int> num_map;
8+
9+
// Step 2: Iterate through the array
10+
for (int i = 0; i < nums.size(); ++i) {
11+
// Step 3: Calculate the complement
12+
int complement = target - nums[i];
13+
14+
// Step 4: Check if the complement exists in the hash map
15+
if (num_map.find(complement) != num_map.end()) {
16+
// Complement found, return the indices
17+
return {num_map[complement], i};
18+
}
19+
20+
// Step 5: Add the current number and its index to the hash map
21+
num_map[nums[i]] = i;
22+
}
23+
24+
// Return an empty vector if no solution found (though the problem guarantees a solution)
25+
return {};
26+
}
27+
28+
// Example usage
29+
int main() {
30+
std::vector<int> nums1 = {2, 7, 11, 15};
31+
int target1 = 9;
32+
std::vector<int> result1 = twoSum(nums1, target1);
33+
std::cout << "[" << result1[0] << ", " << result1[1] << "]" << std::endl; // Output: [0, 1]
34+
35+
std::vector<int> nums2 = {3, 2, 4};
36+
int target2 = 6;
37+
std::vector<int> result2 = twoSum(nums2, target2);
38+
std::cout << "[" << result2[0] << ", " << result2[1] << "]" << std::endl; // Output: [1, 2]
39+
40+
std::vector<int> nums3 = {3, 3};
41+
int target3 = 6;
42+
std::vector<int> result3 = twoSum(nums3, target3);
43+
std::cout << "[" << result3[0] << ", " << result3[1] << "]" << std::endl; // Output: [0, 1]
44+
45+
return 0;
46+
}
47+
48+
49+
50+
//what i submitted
51+
// class Solution {
52+
// public:
53+
// vector<int> twoSum(vector<int>& nums, int target) {
54+
// unordered_map<int, int> hashmap;
55+
56+
// for (int i = 0; i < nums.size(); i++) {
57+
// int complement = target - nums[i];
58+
59+
// if (hashmap.find(complement) != hashmap.end()){
60+
// return {hashmap[complement], i};
61+
// }
62+
// hashmap[nums[i]] = i;
63+
// }
64+
// return {};
65+
// }
66+
// };
67+
68+
69+
70+
// Visual Explanation
71+
// Consider the input array nums = [2, 7, 11, 15] and target = 9.
72+
73+
// Step-by-step Execution:
74+
75+
// Initialization:
76+
77+
// An empty hash map (unordered_map) num_map is created.
78+
// Iteration:
79+
80+
// First Iteration (i=0, num=2):
81+
82+
// Calculate complement: 9 - 2 = 7.
83+
// Check if 7 is in num_map: No.
84+
// Add 2 to num_map with index 0: num_map = {2: 0}.
85+
// Second Iteration (i=1, num=7):
86+
87+
// Calculate complement: 9 - 7 = 2.
88+
// Check if 2 is in num_map: Yes, it is at index 0.
89+
// Return indices: [0, 1].
90+
// Result:
91+
92+
// The function returns [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
93+
// Code Breakdown
94+
// Here is the code again for reference:
95+
96+
// cpp
97+
// Copy code
98+
// #include <iostream>
99+
// #include <vector>
100+
// #include <unordered_map>
101+
102+
// std::vector<int> twoSum(const std::vector<int>& nums, int target) {
103+
// std::unordered_map<int, int> num_map;
104+
105+
// for (int i = 0; i < nums.size(); ++i) {
106+
// int complement = target - nums[i];
107+
108+
// if (num_map.find(complement) != num_map.end()) {
109+
// return {num_map[complement], i};
110+
// }
111+
112+
// num_map[nums[i]] = i;
113+
// }
114+
115+
// return {};
116+
// }
117+
// Explanation of Syntax and Concepts
118+
// Function Signature:
119+
120+
// cpp
121+
// Copy code
122+
// std::vector<int> twoSum(const std::vector<int>& nums, int target)
123+
// std::vector<int>: The function returns a vector of integers, which are the indices of the two numbers that add up to the target.
124+
// const std::vector<int>& nums: The function takes a constant reference to a vector of integers as input.
125+
// const: Ensures that the input vector nums is not modified within the function.
126+
// &: Denotes that nums is passed by reference, which avoids copying the entire vector and improves performance.
127+
// Hash Map (unordered_map):
128+
129+
// cpp
130+
// Copy code
131+
// std::unordered_map<int, int> num_map;
132+
// This is a hash map that stores each number in nums as a key and its index as the value.
133+
// Iteration:
134+
135+
// cpp
136+
// Copy code
137+
// for (int i = 0; i < nums.size(); ++i) {
138+
// int complement = target - nums[i];
139+
140+
// if (num_map.find(complement) != num_map.end()) {
141+
// return {num_map[complement], i};
142+
// }
143+
144+
// num_map[nums[i]] = i;
145+
// }
146+
// We iterate over the nums vector.
147+
// For each element, calculate the complement (target - nums[i]).
148+
// Check if the complement exists in num_map.
149+
// If it does, return the indices of the current element and the complement.
150+
// If it doesn’t, add the current element and its index to num_map.
151+
// Why Use const and &?
152+
// Efficiency: Passing by reference (&) avoids making a copy of the vector, which is especially important for large datasets as it saves memory and processing time.
153+
// Safety: Using const ensures that the function does not modify the input vector, providing a guarantee about the immutability of nums within the function.
154+
// Visual Representation
155+
// Here is a simplified visual representation of how the function processes the array:
156+
157+
// plaintext
158+
// Copy code
159+
// nums = [2, 7, 11, 15]
160+
// target = 9
161+
162+
// Iteration 1:
163+
// num = 2, complement = 7
164+
// num_map: {}
165+
166+
// Iteration 2:
167+
// num = 7, complement = 2
168+
// num_map: {2: 0}
169+
// Found complement in num_map: 2 at index 0
170+
// Return indices: [0, 1]
171+
// Summary
172+
// const std::vector<int>& nums: Efficiently passes the input vector by reference without copying and ensures it remains unchanged.
173+
// Hash Map: Stores elements and their indices for quick lookup of complements.
174+
// Iteration and Check: Efficiently finds the pair of indices that sum up to the target.
175+
// Related Suggestions
176+
// a. Add error handling for edge cases where no solution exists, even though the problem guarantees one.
177+
178+
// b. Extend the function to handle multiple solutions and return all pairs that sum up to the target.

twosum.exe

395 KB
Binary file not shown.

0 commit comments

Comments
 (0)