不知道为什么编程题这么简单😂
1.四个数计算24
class Solution { public: /** * * @param arr int整型一维数组 * @param arrLen int arr数组长度 * @return bool布尔型 */ bool process(int index,int i,int* arr,int arrLen,int path) { if (index == 4 && path == 24) return true; if (index == 4) return false; if (index == i) return process(index + 1, i, arr, arrLen, path); return process(index + 1, i, arr, arrLen, path + arr[index])|| process(index + 1, i, arr, arrLen, path - arr[index])|| process(index + 1, i, arr, arrLen, path * arr[index])|| arr[index]==0||process(index + 1, i, arr, arrLen, path / arr[index]); } bool Game24Points(int* arr, int arrLen) { for (int i = 0; i < 4; i++) { if (process(0,i, arr, arrLen,arr[i])) return true; } return false; } };
2.括号匹配
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isvalid(stack<char> &st, char ch) { if (st.top() == '('&&ch == ')') return true; if (st.top() == '['&&ch == ']') return true; if (st.top() == '{'&&ch == '}') return true; return false; } bool IsValidExp(string s) { if (s.size() == 0) return true; stack<char> st; for (int i = 0; i < s.size(); i++) { if (st.empty() && (s[i] == ')' || s[i] == ']' || s[i] == '}')) return false; if (isvalid(st, s[i])) st.pop(); else { st.push(s[i]); } } return st.empty(); } };
3.找零问题
因为就是个单纯的计算题,代码比较简单就不贴出来了
全部评论
(1) 回帖