美人为了不被恶魔吃掉,就给他讲了一个又一个的故事,一直讲了一千零一夜,后来魔鬼终于困了。姑娘 用发丝勒死了魔鬼。然后姑娘发现,每到晚上,自己的手就会长出鳞片,白天又会消失。突然有天晚上,姑娘又看见了魔鬼。她失声尖叫起来,镜子碎了。原来,美人为了大义,勒死恶魔,却给自己带来了诅咒,会强迫性的直面自己恐惧忧虑恶心的东西。她害怕什么,虽然她的面容不会改变,但她看镜子时,就会把自己看成什么。这个诅咒,作为对美人的报复,明明有姣好的面容但她自己却永远只能对着恶心的事物度日。之前的所有美人,都因此自杀了,但是她们死后,她们的肉体就会成为魔鬼新的躯壳,真正成为恶心的东西,美人笑了笑,看着镜子中恶心的面容说道,那我以后不好看啦,就少照镜子咯,这也没办法不是。既然我不好看啦,那我就多看点好看的人补偿下自己呗,咳咳,我是吃了点亏啦
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
BONUS:
在C++中点和箭头的区别,之前写过,现在再复习下
In one word:
指针就用箭头,不然就是点
不同点:点运算符( . )的左边操作数是一个结果为结构的表达式;
箭头运算符( -> )的左边的操作数是一个指向结构体的指针。
例如:
typedef struct // 定义一个结构体类型:DATA
{
char key[10]; // 结构体成员:key
char name[20]; // 结构体成员:name
int age; // 结构体成员:age
}DATA;
DATA data; // 声明一个结构体变量
DATA *pdata; // 声明一个指向结构体的指针
// 访问数据操作如下:
data.age = 24; // 结构体变量通过点运算符( . )访问
pdata->age = 24; // 指向结构体的指针通过箭头运算符( -> )访问
解法主要是主函数很难写:
首先,类里的函数输入是指针,所以必须有一个指向树的指针
但是树在构建过程中不是指针所以左右子树赋值方式在不停变换
另外结构体自身有初始化方式,要用结构体自定义的方式赋值才能建树
#include <iostream> #include<vector> #include<string> #include<iterator> # include<cstdlib> using namespace std; /** Definition for a binary tree node. */ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: bool isSymmetric(TreeNode* root) { return isMirror(root,root); } bool isMirror(TreeNode* root1,TreeNode* root2){ if(!root1&&!root2) return true; if(!root1||!root2) return false; return root1->val==root2->val&&isMirror(root1->left,root2->right)&&isMirror(root1->right,root2->left); } }; int main() { Solution s1; TreeNode T1(1); TreeNode T2(2); //T2->val=2; TreeNode T3(2); //T3->val=2; TreeNode T4(3); //T4->val=3; TreeNode T5(4); //T5->val=4; TreeNode T6(4); //T6->val=4; TreeNode T7(3); //T7->val=3; TreeNode* root=&T1; root->left=&T2; root->right=&T3; T2.left=&T4; T2.right=&T5; T3.left=&T6; T3.right=&T7; cout<<s1.isSymmetric(root)<<endl; }
Validate if a given string can be interpreted as a decimal number.
Some examples:"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
" -90e3 "
=> true
" 1e"
=> false
"e3"
=> false
" 6e-1"
=> true
" 99e2.5 "
=> false
"53.5e93"
=> true
" --6 "
=> false
"-+3"
=> false
"95a54e53"
=> false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
- Numbers 0-9
- Exponent - "e"
- Positive/negative sign - "+"/"-"
- Decimal point - "."
Of course, the context of these characters also matters in the input.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
#include <iostream> #include<string> #include<regex> using namespace std; class Solution { public: bool isNumber(string s) { cout<<s<<endl; s.erase(0,s.find_first_not_of(" ")); cout<<s.size()<<endl; s.erase(s.find_last_not_of(" ")+1); cout<<s.size()<<endl; if(s.empty()) return false; string p="[+-]?(\\d+\\.?|\\.\\d+)\\d*(e[+-]?\\d+)?"; return regex_match(s,regex(p)); } }; int main() { Solution s1; string s=" 0.1 "; cout<<s1.isNumber(s)<<endl; }
根据例子就可以看出,一个是去前面,另一个是后面,最后通过和正则表达式比较得出结果