summaryrefslogtreecommitdiff
path: root/top-interview-questions/easy/others
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-02-05 18:36:31 -0800
committer3gg <3gg@shellblade.net>2025-02-05 18:36:31 -0800
commit4689e4e80b479be25f7557d05818f5caa723aafa (patch)
tree4df25811fe2a9a15b401375178da6537f4b6063f /top-interview-questions/easy/others
Initial commit.HEADmain
Diffstat (limited to 'top-interview-questions/easy/others')
-rw-r--r--top-interview-questions/easy/others/05_valid_parentheses.cc41
-rw-r--r--top-interview-questions/easy/others/06_missing_number.cc13
2 files changed, 54 insertions, 0 deletions
diff --git a/top-interview-questions/easy/others/05_valid_parentheses.cc b/top-interview-questions/easy/others/05_valid_parentheses.cc
new file mode 100644
index 0000000..3f6d020
--- /dev/null
+++ b/top-interview-questions/easy/others/05_valid_parentheses.cc
@@ -0,0 +1,41 @@
1class Solution {
2public:
3 char matchingChar(char c) {
4 switch (c) {
5 case ')': return '(';
6 case '}': return '{';
7 case ']': return '[';
8 default: throw;
9 }
10 }
11
12 bool isValid(string s) {
13 // ([]) -- valid
14 // ([)] -- invalid
15 std::stack<char> st;
16 for (char c : s) {
17 switch (c) {
18 case '(':
19 case '{':
20 case '[': {
21 st.push(c);
22 break;
23 }
24 case ')':
25 case '}':
26 case ']': {
27 const char expected = matchingChar(c);
28 if (st.empty() ||
29 (st.top() != expected)) {
30 return false;
31 }
32 st.pop();
33 break;
34 }
35 default:
36 throw;
37 }
38 }
39 return st.empty();
40 }
41};
diff --git a/top-interview-questions/easy/others/06_missing_number.cc b/top-interview-questions/easy/others/06_missing_number.cc
new file mode 100644
index 0000000..0487bcc
--- /dev/null
+++ b/top-interview-questions/easy/others/06_missing_number.cc
@@ -0,0 +1,13 @@
1class Solution {
2public:
3 int missingNumber(vector<int>& nums) {
4 const size_t expectedSum = nums.size() * (nums.size()+1) / 2;
5
6 size_t sum = 0;
7 for (int x : nums) {
8 sum += x;
9 }
10
11 return expectedSum - sum;
12 }
13};