How to design priority for operational expressions in big data
This article mainly shows you "how to design priorities for operational expressions in big data", which is easy to understand and well-organized. I hope it can help you solve your doubts. Now let the editor lead you to study and learn "how to design priorities for operational expressions in big data".
Given a string containing numbers and operators, add parentheses to the expression and change its priority to get different results. You need to give the results of all possible combinations. Valid operation symbols include +, -, and *.
Example 1:
Input: output: explanation: (2-1)-1) = 0 (2-(1-1)) = 2
Example 2:
Input: "2: 3-4: 5" output: [- 34,-14,-10,-10] explain: (2 * (3-(4-5) =-34
Answer:
1public List diffWaysToCompute (String input) {
2 List res = new ArrayList ()
3 for (int I = 0; I < input.length (); iTunes +) {
4 char c = input.charAt (I)
5 if (c = ='-'| | c = ='+'| | c = ='*') {
6 String a = input.substring (0, I)
7 String b = input.substring (I + 1)
8 List al = diffWaysToCompute (a)
9 List bl = diffWaysToCompute (b)
10 for (int x: al) {
11 for (int y: bl) {
12 if (c = ='-') {
13 res.add (x-y)
14} else if (c ='+') {
15 res.add (x + y)
16} else if (c ='*') {
17 res.add (x * y)
18}
19}
20}
21}
22}
23 if (res.size () = = 0)
24 res.add (Integer.valueOf (input))
25 return res
26}
Parsing:
First, the string is intercepted, and then the calculated value is added, subtracted and multiplied by recursion. Notice lines 23 through 24. When truncating the head and tail numbers respectively, res may be empty, so you need to add the input value to the res collection.
The above is all the contents of the article "how to design priorities for operational expressions in big data". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!