HackMD 課堂連結
容器
Zerojudge 題目
- c123: 00514 - Rails
這是之前寫的,我知道用 goto
很醜,但…好爽:)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream> #include <stack> using namespace std;
int main() { ios_base::sync_with_stdio(false); cin.tie(0);
int n; ouo: while (cin >> n) { if (n == 0) { break; } while (true) { stack<int> st; int train; int point = 1; int high = 0;
for (int i = 0; i < n; i++) { cin >> train; if (train == 0) { goto ouo; } for (int i = point; i <= train; i++) { st.push(i); point++; high++; } if (st.top() == train) { st.pop(); high--; } } if (st.empty()) { cout << "Yes\n"; } else { cout << "No\n"; } } } }
|
- e447: queue 練習
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <queue> using namespace std;
queue<int> a;
int main() { int times; cin >> times; for (int i = 0; i < times; i++) { int word; cin >> word; switch (word) { case 1: int num; cin >> num; a.push(num); break; case 2: if (a.empty()) { cout << "-1\n"; break; } cout << a.front() << "\n"; break; case 3: if (a.empty()) { break; } a.pop(); break; }; } }
|
Leetcode 題目
- 20. Valid Parentheses
好。。。我直接暴力解。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public: bool isValid(string s) { stack<char> check; for (auto i : s) { if (check.empty() && (i == ')' || i == ']' || i == '}')) return false; if (i == ')' && check.top() != '(') { return false; } else if (i == ']' && check.top() != '[') { return false; } else if (i == '}' && check.top() != '{') { return false; } else if (i == '(' || i == '[' || i == '{') { check.push(i); } else { check.pop(); } } if (check.empty()) { return true; } return false; } };
|
- 1700. Number of Students Unable to Eat Lunch
比較沒有那麼暴力的暴力解 XD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: int countStudents(vector<int>& students, vector<int>& sandwiches) { int j; for (int i = 0; i < sandwiches.size(); i++) { for (j = 0; j < students.size(); j++) { if (sandwiches[i] == students[j]) { students[j] = 9; break; } } if (j == students.size()) { return students.size() - i; } } return 0; } };
|