X*4
2 楼
给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
不太会写
a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
不太会写
T*u
3 楼
南瓜的茎被白虫子吃空,整棵都死了。求助!
r*c
4 楼
油价率先奔5了
p*g
5 楼
p*9
7 楼
如果保证input都是valid的,用一个recursive应该很容易的。因为任何根节点一定从x
?开始。
?开始。
F*Q
8 楼
the organic solution to kill caterpillars:
http://www.lowes.com/pd_368920-316-HG-93190_0__?productId=34998
you can use it up to the day of harvest.
【在 T*****u 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 南瓜的茎被白虫子吃空,整棵都死了。求助!
M*A
10 楼
晒黑~
g*e
13 楼
这对南瓜的虫一点用都没有。
【在 F***Q 的大作中提到】![](/moin_static193/solenoid/img/up.png)
:
: the organic solution to kill caterpillars:
: http://www.lowes.com/pd_368920-316-HG-93190_0__?productId=34998
: you can use it up to the day of harvest.
【在 F***Q 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
:
: the organic solution to kill caterpillars:
: http://www.lowes.com/pd_368920-316-HG-93190_0__?productId=34998
: you can use it up to the day of harvest.
H*g
15 楼
戴个礼帽,挎个盒子炮
a*1
16 楼
serta那个原价贵哦。
大家是自己去拿还是让送呀?
大家是自己去拿还是让送呀?
c*w
17 楼
第一个?前面的部分是root。
扫描过去找到对应第一个?的:,第一个?与这个:之间的substring是左子树,:的
右边的substring是右子树。
把左右子树对应的substring用recursion。
扫描过去找到对应第一个?的:,第一个?与这个:之间的substring是左子树,:的
右边的substring是右子树。
把左右子树对应的substring用recursion。
K*2
18 楼
二战时皇军就有自行车,在山地和岛屿地区行动方便
c*w
20 楼
好像有点问题
a?b:c?d:e
应该是a是跟,左子树是b,右子树是c?d:e吧?
a?b:c?d:e
应该是a是跟,左子树是b,右子树是c?d:e吧?
c*w
23 楼
我这个跟题目里的例子不一样。
我这个难道不是一个nested ternary?
还有更复杂的形式例如 a?b?c:d:e?f:g
我这个难道不是一个nested ternary?
还有更复杂的形式例如 a?b?c:d:e?f:g
a*n
26 楼
右孩子也有孩子怎么办?
a?b?c:d:e?f:g
这样呢?
a?b?c:d:e?f:g
这样呢?
S*e
30 楼
zan
p*2
32 楼
这个行吗?
Node dfs(String str, int s, int e){
int i = s;
while(i
if(i==e){
return new Node(str.substring(s, e+1));
}
int j=i+1;
int count=0;
while(j
count++;
if(str.charAt(j) == ':')
count--;
j++;
}
Node node = new Node(str.substring(s, i));
node.left = dfs(str, i+1, j-1);
node.right= dfs(str, j+1, e);
return node;
}
【在 c******w 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我这个跟题目里的例子不一样。
: 我这个难道不是一个nested ternary?
: 还有更复杂的形式例如 a?b?c:d:e?f:g
x*1
34 楼
G的题目真是千奇百怪,我一个都不会做。
p*2
45 楼
这个行吗?
int curr = 0;
Node dfs(String str){
if(curr >= str.length())
return null;
int i = curr;
while(i
Node node = new Node(str.substring(curr, i));
curr = i+1;
if(i
node.right = dfs(str);
}
return node;
}
【在 x*********3 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 貌似没有必要这么复杂, 这个应该是O(n^2),下面是O(n)的。
: 简单起见,每个节点只有一个字符,str的话处理起来也很简单,直接读到:或?就
: 行了:
: Node * dfs(char *& p)
: {
: if (!p || (!*p)|| *p == ':') return NULL;
: Node * n = new Node(*p);
: if (*(p+1) == '?')
: {
: p = p+2;
c*r
47 楼
这个思路不错,我给转成了个c++代码如下。只是觉得你的code里面,最后一个if语句
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i 我转的C++如下:
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i i++;
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i node->left = tree_dfs(str);
}
else if (i node->right = tree_dfs(str);
}
return node;
}
【在 p*****2 的大作中提到】![](/moin_static193/solenoid/img/up.png)
:
: 这个行吗?
: int curr = 0;
:
: Node dfs(String str){
: if(curr >= str.length())
: return null;
:
: int i = curr;
:
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i
}
else if (i
}
return node;
}
【在 p*****2 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
:
: 这个行吗?
: int curr = 0;
:
: Node dfs(String str){
: if(curr >= str.length())
: return null;
:
: int i = curr;
:
x*3
50 楼
x*3
52 楼
这种recursive用if else的话,左右子树就搞不到一个root上去
了,是吧。
了,是吧。
X*4
53 楼
给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
不太会写
a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
不太会写
p*9
54 楼
如果保证input都是valid的,用一个recursive应该很容易的。因为任何根节点一定从x
?开始。
?开始。
c*w
56 楼
第一个?前面的部分是root。
扫描过去找到对应第一个?的:,第一个?与这个:之间的substring是左子树,:的
右边的substring是右子树。
把左右子树对应的substring用recursion。
扫描过去找到对应第一个?的:,第一个?与这个:之间的substring是左子树,:的
右边的substring是右子树。
把左右子树对应的substring用recursion。
p*2
61 楼
这个行吗?
Node dfs(String str, int s, int e){
int i = s;
while(i
if(i==e){
return new Node(str.substring(s, e+1));
}
int j=i+1;
int count=0;
while(j
count++;
if(str.charAt(j) == ':')
count--;
j++;
}
Node node = new Node(str.substring(s, i));
node.left = dfs(str, i+1, j-1);
node.right= dfs(str, j+1, e);
return node;
}
【在 c******w 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 我这个跟题目里的例子不一样。
: 我这个难道不是一个nested ternary?
: 还有更复杂的形式例如 a?b?c:d:e?f:g
x*1
62 楼
G的题目真是千奇百怪,我一个都不会做。
p*2
72 楼
这个行吗?
int curr = 0;
Node dfs(String str){
if(curr >= str.length())
return null;
int i = curr;
while(i
Node node = new Node(str.substring(curr, i));
curr = i+1;
if(i
node.right = dfs(str);
}
return node;
}
【在 x*********3 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 貌似没有必要这么复杂, 这个应该是O(n^2),下面是O(n)的。
: 简单起见,每个节点只有一个字符,str的话处理起来也很简单,直接读到:或?就
: 行了:
: Node * dfs(char *& p)
: {
: if (!p || (!*p)|| *p == ':') return NULL;
: Node * n = new Node(*p);
: if (*(p+1) == '?')
: {
: p = p+2;
c*r
74 楼
这个思路不错,我给转成了个c++代码如下。只是觉得你的code里面,最后一个if语句
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i 我转的C++如下:
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i i++;
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i node->left = tree_dfs(str);
}
else if (i node->right = tree_dfs(str);
}
return node;
}
【在 p*****2 的大作中提到】![](/moin_static193/solenoid/img/up.png)
:
: 这个行吗?
: int curr = 0;
:
: Node dfs(String str){
: if(curr >= str.length())
: return null;
:
: int i = curr;
:
,是否node->right = dfs(str);该移到if外,在一个else if 里面, 这个else if条件
是 else if (i
int curr = 0;
Node* tree_dfs(string str ) {
if(curr >= str.length())
return NULL;
int i = curr;
while(i
Node* node = new Node(str.substr(curr, i-curr));
curr = i+1;
if(i
}
else if (i
}
return node;
}
【在 p*****2 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
:
: 这个行吗?
: int curr = 0;
:
: Node dfs(String str){
: if(curr >= str.length())
: return null;
:
: int i = curr;
:
x*3
77 楼
x*3
79 楼
这种recursive用if else的话,左右子树就搞不到一个root上去
了,是吧。
了,是吧。
k*L
80 楼
Let me try:
Node *ternary(string &s) {
if (s.size() == 0) return NULL;
stack st;
bool isLeft = true;
int prev = 0;
for (int i = 0; i < s.size(); ++i) {
if (s.at(i) == ‘?’ || s.at(i) == ‘:’) {
Node *newNode = new Node(s.substr(pref, i - pref));
if (!st.empty()) {
Node *n;
if (isLeft) {
n = st.top();
n->left = newNode;
} else {
n = st.pop();
n->right = newNode;
}
}
if (s.at(i) == ‘?’)
st.push(newNode);
isLeft = true;
pev = i + 1;
} else if (s.at(i) == ‘:’) {
isLeft = false;
prev = i + 1;
}
} else {
continue;
}
}
Node *root = NULL;
while (!st.empty())
root = st.pop();
return root;
}
【在 X*4 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
: a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
: 不太会写
Node *ternary(string &s) {
if (s.size() == 0) return NULL;
stack
bool isLeft = true;
int prev = 0;
for (int i = 0; i < s.size(); ++i) {
if (s.at(i) == ‘?’ || s.at(i) == ‘:’) {
Node *newNode = new Node(s.substr(pref, i - pref));
if (!st.empty()) {
Node *n;
if (isLeft) {
n = st.top();
n->left = newNode;
} else {
n = st.pop();
n->right = newNode;
}
}
if (s.at(i) == ‘?’)
st.push(newNode);
isLeft = true;
pev = i + 1;
} else if (s.at(i) == ‘:’) {
isLeft = false;
prev = i + 1;
}
} else {
continue;
}
}
Node *root = NULL;
while (!st.empty())
root = st.pop();
return root;
}
【在 X*4 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
: a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
: 不太会写
l*s
82 楼
//Recursive
public static TreeNode buildTree(string express)
{
int i = 0;
while (i < express.Length && express[i] != '?') i++;
TreeNode root = new TreeNode(express.Substring(0, i).Trim());
int index = i, count = 0;
while (index < express.Length && !(count == 0 && express[index] == ':'))
{
if (express[index] == '?') count++;
else if (express[index] == ':') count--;
if(count != 0) index++;
}
if (index >= express.Length) return root;
root.Left = buildTree(express.Substring(i + 1, index - i - 1).Trim());
root.Right = buildTree(express.Substring(index + 1).Trim());
return root;
}
//Iterative
public static TreeNode buildTree_Iterative(string express)
{
Stack stack = new Stack();
StringBuilder subExpress = new StringBuilder();
char lastOp = '\0';
for(int i = 0; i <= express.Length; i++)
if (i == express.Length || express[i] == '?' || express[i] == ':')
{
TreeNode newNode = new TreeNode(subExpress.ToString().Trim());
if(lastOp == '?') stack.Peek().Left = newNode;
else if (lastOp == ':')
{
stack.Pop();
stack.Peek().Right = newNode;
if(stack.Count > 1) stack.Pop();
}
stack.Push(newNode);
if(i < express.Length) lastOp = express[i];
subExpress.Clear();
}
else subExpress.Append(express[i]);
while (stack.Count > 1) stack.Pop();
return stack.Pop();
}
public static TreeNode buildTree(string express)
{
int i = 0;
while (i < express.Length && express[i] != '?') i++;
TreeNode root = new TreeNode(express.Substring(0, i).Trim());
int index = i, count = 0;
while (index < express.Length && !(count == 0 && express[index] == ':'))
{
if (express[index] == '?') count++;
else if (express[index] == ':') count--;
if(count != 0) index++;
}
if (index >= express.Length) return root;
root.Left = buildTree(express.Substring(i + 1, index - i - 1).Trim());
root.Right = buildTree(express.Substring(index + 1).Trim());
return root;
}
//Iterative
public static TreeNode buildTree_Iterative(string express)
{
Stack
StringBuilder subExpress = new StringBuilder();
char lastOp = '\0';
for(int i = 0; i <= express.Length; i++)
if (i == express.Length || express[i] == '?' || express[i] == ':')
{
TreeNode newNode = new TreeNode(subExpress.ToString().Trim());
if(lastOp == '?') stack.Peek().Left = newNode;
else if (lastOp == ':')
{
stack.Pop();
stack.Peek().Right = newNode;
if(stack.Count > 1) stack.Pop();
}
stack.Push(newNode);
if(i < express.Length) lastOp = express[i];
subExpress.Clear();
}
else subExpress.Append(express[i]);
while (stack.Count > 1) stack.Pop();
return stack.Pop();
}
r*n
85 楼
case class TreeNode(value: Char, var left: Option[TreeNode] = None, var
right: Option[TreeNode] = None)
object TreeBuilder {
//recursive version
def recursive(input: String, start: Int): (Int, Option[TreeNode]) ={
if (start >= input.length) {
(start, None)
} else {
val cur = Some(TreeNode(input(start)))
if (start == input.length - 1) {
(start + 1, cur)
} else {
input(start + 1) match {
case '?' =>
val (loffset, l) = recursive(input, start + 2)
val (roffset, r) = recursive(input, loffset)
cur.get.left = l
cur.get.right = r
(roffset, cur)
case ':' =>
(start + 2, cur)
}
}
}
}
// iterative version
def buildTree(input:String): Option[TreeNode] = {
if (input.length == 0) {
return None
}
val stack = new mutable.Stack[TreeNode]()
val root = Some(TreeNode(input(0)))
var cur: TreeNode = root.get
var left = false
(1 until input.length).foreach { x=>
input(x) match {
case '?' =>
left = true
stack.push(cur)
case ':' =>
case _ =>
if (left) {
cur.left = Some(TreeNode(input(x)))
left = false
cur = cur.left.get
} else {
cur = stack.pop()
cur.right = Some(TreeNode(input(x)))
cur = cur.right.get
}
}
}
root
}
}
right: Option[TreeNode] = None)
object TreeBuilder {
//recursive version
def recursive(input: String, start: Int): (Int, Option[TreeNode]) ={
if (start >= input.length) {
(start, None)
} else {
val cur = Some(TreeNode(input(start)))
if (start == input.length - 1) {
(start + 1, cur)
} else {
input(start + 1) match {
case '?' =>
val (loffset, l) = recursive(input, start + 2)
val (roffset, r) = recursive(input, loffset)
cur.get.left = l
cur.get.right = r
(roffset, cur)
case ':' =>
(start + 2, cur)
}
}
}
}
// iterative version
def buildTree(input:String): Option[TreeNode] = {
if (input.length == 0) {
return None
}
val stack = new mutable.Stack[TreeNode]()
val root = Some(TreeNode(input(0)))
var cur: TreeNode = root.get
var left = false
(1 until input.length).foreach { x=>
input(x) match {
case '?' =>
left = true
stack.push(cur)
case ':' =>
case _ =>
if (left) {
cur.left = Some(TreeNode(input(x)))
left = false
cur = cur.left.get
} else {
cur = stack.pop()
cur.right = Some(TreeNode(input(x)))
cur = cur.right.get
}
}
}
root
}
}
m*t
86 楼
seems recursive is the easiest. I am still trying to figure out a stack-
based approach:-)
class TreeNode{
public:
string str;
TreeNode* left;
TreeNode* right;
TreeNode(string s) {
str = s;
left = NULL;
right = NULL;
}
};
class Solution {
public:
TreeNode* buildTree(string &A, int& index) {
int index_next = A.find_first_of("?:",index);
if (index_next==string::npos) {
TreeNode* root = new TreeNode(A.substr(index));
index = A.size();
return root;
}
TreeNode* root = new TreeNode(A.substr(index, index_next-index));
if (A[index_next]=='?') {
index = index_next+1;
root->left = buildTree(A, index);
root->right = buildTree(A, index);
} else {
index = index_next+1;
}
return root;
}
TreeNode* buildTree(string &A) {
int index = 0;
return buildTree(A, index);
}
};
【在 X*4 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
: a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
: 不太会写
based approach:-)
class TreeNode{
public:
string str;
TreeNode* left;
TreeNode* right;
TreeNode(string s) {
str = s;
left = NULL;
right = NULL;
}
};
class Solution {
public:
TreeNode* buildTree(string &A, int& index) {
int index_next = A.find_first_of("?:",index);
if (index_next==string::npos) {
TreeNode* root = new TreeNode(A.substr(index));
index = A.size();
return root;
}
TreeNode* root = new TreeNode(A.substr(index, index_next-index));
if (A[index_next]=='?') {
index = index_next+1;
root->left = buildTree(A, index);
root->right = buildTree(A, index);
} else {
index = index_next+1;
}
return root;
}
TreeNode* buildTree(string &A) {
int index = 0;
return buildTree(A, index);
}
};
【在 X*4 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 给一个string of nested ternary operations例如a?b?c:d:e,build a tree:root是
: a,左子树是b?c:d对应的tree ,右子树是e。保证input都是valid的。
: 不太会写
m*t
87 楼
stack based approach
TreeNode* buildTreeStack(string &A) {
stack s_nodes;
stack s_operators;
int index = 0;
while (index if (A[index]=='?') {
s_operators.push(A[index]);
index++;
} else if (A[index]==':') {
if (s_operators.top()=='?') {
s_operators.push(A[index]);
} else {
// pop three things from the s_nodes, and form the
subtree
TreeNode* new_right = s_nodes.top(); s_nodes.pop();
TreeNode* new_left = s_nodes.top(); s_nodes.pop();
TreeNode* new_root = s_nodes.top(); s_nodes.pop();
s_operators.pop(); s_operators.pop();
new_root->left = new_left;
new_root->right = new_right;
s_nodes.push(new_root);
s_operators.push(A[index]);
}
index++;
} else { // regular node
int index_next = A.find_first_of("?:", index);
if (index_next==string::npos) {
TreeNode* new_node = new TreeNode(A.substr(index));
index = A.size();
s_nodes.push(new_node);
} else {
TreeNode* new_node = new TreeNode(A.substr(index, index_
next-index));
index = index_next;
s_nodes.push(new_node);
}
}
}
while (!s_operators.empty()) {
TreeNode* new_right = s_nodes.top(); s_nodes.pop();
TreeNode* new_left = s_nodes.top(); s_nodes.pop();
TreeNode* new_root = s_nodes.top(); s_nodes.pop();
s_operators.pop(); s_operators.pop();
new_root->left = new_left;
new_root->right = new_right;
s_nodes.push(new_root);
}
return s_nodes.top();
}
【在 m****t 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: seems recursive is the easiest. I am still trying to figure out a stack-
: based approach:-)
: class TreeNode{
: public:
: string str;
: TreeNode* left;
: TreeNode* right;
: TreeNode(string s) {
: str = s;
: left = NULL;
TreeNode* buildTreeStack(string &A) {
stack
stack
int index = 0;
while (index
s_operators.push(A[index]);
index++;
} else if (A[index]==':') {
if (s_operators.top()=='?') {
s_operators.push(A[index]);
} else {
// pop three things from the s_nodes, and form the
subtree
TreeNode* new_right = s_nodes.top(); s_nodes.pop();
TreeNode* new_left = s_nodes.top(); s_nodes.pop();
TreeNode* new_root = s_nodes.top(); s_nodes.pop();
s_operators.pop(); s_operators.pop();
new_root->left = new_left;
new_root->right = new_right;
s_nodes.push(new_root);
s_operators.push(A[index]);
}
index++;
} else { // regular node
int index_next = A.find_first_of("?:", index);
if (index_next==string::npos) {
TreeNode* new_node = new TreeNode(A.substr(index));
index = A.size();
s_nodes.push(new_node);
} else {
TreeNode* new_node = new TreeNode(A.substr(index, index_
next-index));
index = index_next;
s_nodes.push(new_node);
}
}
}
while (!s_operators.empty()) {
TreeNode* new_right = s_nodes.top(); s_nodes.pop();
TreeNode* new_left = s_nodes.top(); s_nodes.pop();
TreeNode* new_root = s_nodes.top(); s_nodes.pop();
s_operators.pop(); s_operators.pop();
new_root->left = new_left;
new_root->right = new_right;
s_nodes.push(new_root);
}
return s_nodes.top();
}
【在 m****t 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: seems recursive is the easiest. I am still trying to figure out a stack-
: based approach:-)
: class TreeNode{
: public:
: string str;
: TreeNode* left;
: TreeNode* right;
: TreeNode(string s) {
: str = s;
: left = NULL;
相关阅读