b; 参考技术B三元表达式如: String str = (a == null? "":a.toString()); //当a为null时str = ""; 当a不为null时str = a.toString();参考技术C你好: 这个是基础,举个例子: int i = a>b?0:1; 意思就是:如果a>b,那么i=0,否则i=1 谢谢参考技术D三元表达式我见过的只有一个:(布尔表达式 ? 值真:值假),首先计算前边的表达式的值为真为假,若为真则执行问好后边冒号前边的表达式或语句,为假则执行冒号后边的表达式或语句
java 439.三元表达式Parser.java
/*
In order to pick out useful "?" and ":", we can always begin with the last "?" and the first ":" after the chosen "?".
Therefore, directly seek for the last "?" (or you can simply put all "?" into a stack) and update the string depending on T or F until no more "?"s.
*/
public class Solution {
public String parseTernary(String expression) {
int len = expression.length();
Deque<Character> stack = new ArrayDeque<Character>();
for(int i = len - 1; i >=0; i--) {
char temp = expression.charAt(i);
if(!stack.isEmpty() && stack.peek() == '?') {
stack.pop(); // ?
char first = stack.pop();
stack.pop();// :
char second = stack.pop();
if(temp == 'T') {
stack.push(first);
} else {
stack.push(second);
}
} else {
stack.push(temp);
}
}
return String.valueOf(stack.peek());
}
}