diff --git a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README.md b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README.md index 7a62b4fcf7c8c..59b8e45e1dd8c 100644 --- a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README.md +++ b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README.md @@ -68,7 +68,19 @@ ```java - +class Solution { + public int numberOfSteps(int num) { + int cnt = 0; + while (num != 0) { + if (num % 2 == 1) + num--; + else + num /= 2; + cnt++; + } + return cnt; + } +} ``` ### **...** diff --git a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README_EN.md b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README_EN.md index af2a514685466..e828822776c9b 100644 --- a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README_EN.md +++ b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README_EN.md @@ -61,7 +61,19 @@ Step 4) 1 is odd; subtract 1 and obtain 0. ### **Java** ```java - +class Solution { + public int numberOfSteps(int num) { + int cnt = 0; + while (num != 0) { + if (num % 2 == 1) + num--; + else + num /= 2; + cnt++; + } + return cnt; + } +} ``` ### **...** diff --git a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.java b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.java new file mode 100644 index 0000000000000..fd6ac2dfc9993 --- /dev/null +++ b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.java @@ -0,0 +1,13 @@ +class Solution { + public int numberOfSteps(int num) { + int cnt = 0; + while (num != 0) { + if (num % 2 == 1) + num--; + else + num /= 2; + cnt++; + } + return cnt; + } +} \ No newline at end of file diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/README.md b/solution/1600-1699/1678.Goal Parser Interpretation/README.md index f297c7e47fec3..9e1be1167bd55 100644 --- a/solution/1600-1699/1678.Goal Parser Interpretation/README.md +++ b/solution/1600-1699/1678.Goal Parser Interpretation/README.md @@ -64,7 +64,29 @@ G -> G ```java - +class Solution { + public String interpret(String command) { + StringBuilder sb = new StringBuilder(); + int p = 0, q = 1; + for (; p < command.length(); p++, q++) { + char c = command.charAt(p); + if (c == 'G') + sb.append('G'); + if (c == '(') { + if (command.charAt(q) == ')') { + sb.append("o"); + p++; + q++; + } else { + sb.append("al"); + p += 2; + q += 2; + } + } + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md b/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md index 851e3617808e8..ccf493856e622 100644 --- a/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md +++ b/solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md @@ -57,7 +57,29 @@ The final concatenated result is "Goal". ### **Java** ```java - +class Solution { + public String interpret(String command) { + StringBuilder sb = new StringBuilder(); + int p = 0, q = 1; + for (; p < command.length(); p++, q++) { + char c = command.charAt(p); + if (c == 'G') + sb.append('G'); + if (c == '(') { + if (command.charAt(q) == ')') { + sb.append("o"); + p++; + q++; + } else { + sb.append("al"); + p += 2; + q += 2; + } + } + } + return sb.toString(); + } +} ``` ### **...** diff --git a/solution/1600-1699/1678.Goal Parser Interpretation/Solution.java b/solution/1600-1699/1678.Goal Parser Interpretation/Solution.java new file mode 100644 index 0000000000000..b5b6e0817b2e3 --- /dev/null +++ b/solution/1600-1699/1678.Goal Parser Interpretation/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public String interpret(String command) { + StringBuilder sb = new StringBuilder(); + int p = 0, q = 1; + for (; p < command.length(); p++, q++) { + char c = command.charAt(p); + if (c == 'G') + sb.append('G'); + if (c == '(') { + if (command.charAt(q) == ')') { + sb.append("o"); + p++; + q++; + } else { + sb.append("al"); + p += 2; + q += 2; + } + } + } + return sb.toString(); + } +} \ No newline at end of file