- 博客(19)
- 收藏
- 关注
原创 在二叉树中找到两个节点的最近公共祖先
思路:深度遍历以所求节点为终止节点的路径,判断两个路径的交点/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */class Solution {public: /** * * @param root TreeNode类 * @param o1 int整型 * @param o2 int整型
2021-09-13 19:11:12
220
原创 有效的括号序列
stack<char> c_stack; for(int i = 0;i<s.length();i++){ if(s[i] == '('){ c_stack.push(')'); } else if(s[i] == '['){ c_stack.push(']'); } else if(s[..
2021-09-09 21:26:43
128
原创 5.用两个栈实现队列
class Solution{public: void push(int node) { while(!stack1.empty()){ stack2.push(stack1.top()); stack1.pop(); } stack2.push(node); while(!stack2.empty()){ stack1.push(stack2.top());
2021-08-23 21:58:21
103
原创 4.重建二叉树
注:没有中序是无法构造的/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode* reConstructBinar
2021-08-23 21:50:50
97
原创 3. 从尾到头打印链表
从头到尾打印,然后逆序 或者利用栈的先进后出vector<int> printListFromTailToHead(ListNode* head) { vector<int> res; while(head){ res.push_back(head->val); head = head->next; } reverse(res.begin(), res.end
2021-08-23 21:29:11
129
原创 2.替换空格
string replaceSpace(string s) { // write code here string res = ""; for(int i = 0;i<s.size();i++){ if(s[i] != ' '){ res += s[i]; } else{ res += "%20";
2021-08-23 21:25:52
114
原创 1. 二维数组中的查找
思路(1)每一行有序,对每一行二分查找bool Find(int target, vector<vector<int> > array) { // 每一行有序 int row = array.size(); int col = array[0].size(); for(int i = 0;i<row;i++){ int low = 0; int high = ar
2021-08-23 21:21:28
125
原创 华为机试(句子逆序)
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I” 所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符。#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){ string str; getline(cin,str); vector<string.
2021-08-14 20:23:55
177
原创 华为机试题(密码验证合格)
密码要求:1.长度超过8位2.包括大小写字母.数字.其它符号,以上四种至少三种3.不能有相同长度大于2的子串重复#include<iostream>#include<string>using namespace std;// 判断长度bool lenIsValid(string str){ if(str.length()<=8){ return false; } return true;}// 判断条件2bool
2021-08-14 20:15:25
257
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅