|
@@ -1,30 +1,79 @@
|
|
|
-import java.util.HashMap;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
public class tmp {
|
|
|
- private static String solution(String value) {
|
|
|
- String[] arr = value.split(",");
|
|
|
- //valueArray是题目中的整型数组
|
|
|
- int[] valueArray = new int[arr.length];
|
|
|
- for (int i = 0; i < arr.length; i++) {
|
|
|
- valueArray[i] = Integer.parseInt(arr[i]);
|
|
|
- }
|
|
|
-
|
|
|
- //Coding here...
|
|
|
- int minValue = Integer.MAX_VALUE;
|
|
|
- int maxD = 0;
|
|
|
- for (int i = 0; i < valueArray.length; i++) {
|
|
|
- if (valueArray[i] < minValue) {
|
|
|
- minValue = valueArray[i];
|
|
|
- } else if (valueArray[i] - minValue > maxD) {
|
|
|
- maxD = valueArray[i] - minValue;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //在处理完问题之后,请将你的结果转换为字符串,并return
|
|
|
- return String.valueOf(maxD);
|
|
|
+
|
|
|
+ public static JSONObject convert(JSONObject inputJson) {
|
|
|
+ JSONObject outputJson = new JSONObject();
|
|
|
+ JSONObject conditions = new JSONObject();
|
|
|
+ JSONArray children = new JSONArray();
|
|
|
+ String conjunction = inputJson.getString("relationship");
|
|
|
+
|
|
|
+ inputJson.getJSONArray("list").forEach(item -> {
|
|
|
+ JSONObject child = new JSONObject();
|
|
|
+ String id = UUID.randomUUID().toString();
|
|
|
+
|
|
|
+ child.put("id", id);
|
|
|
+
|
|
|
+ JSONObject left = new JSONObject();
|
|
|
+ left.put("type", "field");
|
|
|
+ left.put("field", ((JSONObject) item).getString("key"));
|
|
|
+
|
|
|
+ String op = ((JSONObject) item).getString("op");
|
|
|
+
|
|
|
+ Object right = ((JSONObject) item).getString("value");
|
|
|
+
|
|
|
+ child.put("left", left);
|
|
|
+ child.put("op", op);
|
|
|
+ child.put("right", right);
|
|
|
+
|
|
|
+ children.add(child);
|
|
|
+ });
|
|
|
+
|
|
|
+ conditions.put("id", UUID.randomUUID().toString());
|
|
|
+ conditions.put("conjunction", conjunction);
|
|
|
+ conditions.put("children", children);
|
|
|
+
|
|
|
+ outputJson.put("conditions", conditions);
|
|
|
+ return outputJson;
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- System.out.println(solution("7,1,5,3,6,4"));
|
|
|
+ String inputStr = "{\n" +
|
|
|
+ " \"relationship\":\"and\",\n" +
|
|
|
+ " \"list\":[\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \n" +
|
|
|
+ " \"key\": \"tag1\",\n" +
|
|
|
+ " \"op\": \"greater\",\n" +
|
|
|
+ " \"value\": 0\n" +
|
|
|
+ " },{\n" +
|
|
|
+ " \n" +
|
|
|
+ " \"key\": \"tag2\",\n" +
|
|
|
+ " \"op\": \"equals\",\n" +
|
|
|
+ " \"value\": \"value\"\n" +
|
|
|
+ " },{\n" +
|
|
|
+ " \"relationship\":\"and\",\n" +
|
|
|
+ " \"list\":[\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \n" +
|
|
|
+ " \"key\": \"tag1\",\n" +
|
|
|
+ " \"op\": \"greater\",\n" +
|
|
|
+ " \"value\": 0\n" +
|
|
|
+ " },{\n" +
|
|
|
+ " \n" +
|
|
|
+ " \"key\": \"tag2\",\n" +
|
|
|
+ " \"op\": \"equals\",\n" +
|
|
|
+ " \"value\": \"value\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}";
|
|
|
+
|
|
|
+ JSONObject inputJson = JSONObject.parseObject(inputStr);
|
|
|
+ JSONObject outputJson = tmp.convert(inputJson);
|
|
|
+ System.out.println(outputJson.toJSONString());
|
|
|
}
|
|
|
-}
|
|
|
+}
|