不是哥们不做代码园oj
是哥们真做不出来
思路:
1.二叉树的中序遍历存入数组
2.判断数组元素是否递增
class Solution {
public:
vector<int >ANS;
void dfs(TreeNode *r){
if(!r)return;
dfs(r->left);
ANS.push_back(r->val);
dfs(r->right);
}
bool isValidBST(TreeNode* root) {
dfs(root);
int L=ANS.size();
if(!root||L==1)return 1;
vector<int>tem=ANS;
for(int i=1;i<L;i++){
if(ANS[i-1]<ANS[i])continue;
else return 0;
}
return 1;
}
};
顺便补充三种遍历(前,中,后
//打印序列
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
void Print_(TreeNode *root){
if(!root)return;
cout<<root->val;//前序
Print_(root->left);
cout<<root->val;//中序
Print_(root->right);
cout<<root->val;//后序
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>ANS;
void dfs(TreeNode* root){
if(!root)return;
dfs(root->left);
ANS.push_back(root->val);
dfs(root->right);
}
vector<int> inorderTraversal(TreeNode* root) {
if(!root)return ANS;
dfs(root);
return ANS;
}
};
思路:
1.判断最后一位是不是“1”
2.ANS[n]表示用二进制表示n的情况下二进制中“1”的个数;
class Solution {
public:
vector<int> countBits(int n) {
vector < int >ANS(n+1,0);
for(int i=0;i<=n;i++){
ANS[i]=ANS[i>>1]+(i&1);//若i为奇数(i&1)=1,i>>1等同于去掉i二进制表示的最后一位
}
return ANS;
}
};
思路:
Brian KernighanBrian Kernighan 算法
class Solution {
public:
vector<int> countBits(int n) {
vector < int >ANS;
for(int i=0;i<=n;i++){
int tem=i,N=0;
while(tem){
N++;
tem=tem&(tem-1);
}
ANS.push_back(N);
}
return ANS;
}
};
思路:
定义双指针pre,cur,借助指针tem不断让cur指向pre,当cur为空指针时,即完成反转
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *pre=nullptr,*cur=head,*tem=head;
while(cur!=nullptr){
tem=cur->next;
cur->next=pre;
pre=cur;
cur=tem;
}
return pre;
}
};
思路:
维护一个优先队列
class KthLargest { public: priority_queue<int ,vector<int>,greater<int> >Q; int k; KthLargest(int k, vector<int>& nums) { this->k=k; for(auto a:nums){ Q.push(a); } while(Q.size()>k){ Q.pop(); } } int add(int val) { Q.push(val); while(Q.size()>k)Q.pop(); return Q.top(); } }; /** * Your KthLargest object will be instantiated and called as such: * KthLargest* obj = new KthLargest(k, nums); * int param_1 = obj->add(val); */
class Solution {
public:
bool isValid(string s) {
stack< char>S;
int L=s.size();
for(int i=0;i<L;i++){
if(s[i]=='{'||s[i]=='('||s[i]=='[')S.push(s[i]);
else{
if(S.empty())return 0;
char tem=S.top();
S.pop();
if(tem=='{'&&s[i]!='}')return 0;//判断括号是否匹配
if(tem=='('&&s[i]!=')')return 0;
if(tem=='['&&s[i]!=']')return 0;
}
}
if(!S.empty())return 0;//检查是否有未配对的左括号
else return 1;
}
};
思路:(贪心+双指针)
1.排序先;
2. 两个指针指向数组的首尾;
3.两个指针指向的数据的和<=船能承受的最大重量,两个指针同时向着中间移动。
否则移动指向数组尾的指针;
思路:二进制表示;
如果数组有n个元素,枚举[0,2^n-1];
用“1”表示选中某元素;
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int L=nums.size();
int x=1;
x=(x<<L)-1;
vector<vector<int>>ANS;
for(int i=0;i<=x;i++){
vector<int >tem;
for(int j=0;j<L;j++){
if((i>>j)&1==1)tem.push_back(nums[j]);
}
ANS.push_back(tem);
}
return ANS;
}
};
思路:模拟
class Solution {
public:
int myAtoi(string str) {
long long res = 0;
int i = 0;
int flag = 1;
while(str[i]==' '){i++;}
if(str[i]=='-'){flag=-1;}
if(str[i]=='+'||str[i]=='-'){i++;}
while(i<str.size()&&isdigit(str[i])){
int r=str[i]-'0';
res = res * 10 +r;
i++;
if (res>INT_MAX)
{
return flag >0 ? INT_MAX:INT_MIN;
}
}
return flag >0 ? res : -res;
}
};
思路:
用队列辅助遍历子节点
1.放入节点;
2.获取节点的子节点个数;
3.将该节点的子节点放入队列;
4.节点出队;
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
queue<Node*>Q;
vector<vector<int>> levelOrder(Node* r) {
vector<vector<int>>ans;
if(r==nullptr)return ans;
Q.push(r);
while(!Q.empty()){
int L=Q.size();
vector<int>ANS;
while(L-->0){
r=Q.front();
int l=r->children.size();//该节点获取子节点的数量
ANS.push_back(r->val);;
for(int i=0;i<l;i++){
auto tem=r->children[i];
Q.push(tem);
}
Q.pop();
}
ans.push_back(ANS);
}
return ans;
}
};