Skip to content

Commit 657ba5f

Browse files
authored
feat: add java solution to lc problems: No.1342 and No.1678 (doocs#537)
* feat: add java solution to lc problem: No.1342.Number of Steps to Reduce a Number to Zero * feat: add java solution to lc problem: No. 1678.Goal Parser Interpretation * feat: add java solution to lc problem: No.1342.Number of Steps to Reduce a Number to Zero * add java solution to lc problem: No.1678.Goal Parser Interpretation * feat: code format
1 parent 573b62b commit 657ba5f

File tree

6 files changed

+108
-4
lines changed

6 files changed

+108
-4
lines changed

solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@
6868
<!-- 这里可写当前语言的特殊实现逻辑 -->
6969

7070
```java
71-
71+
class Solution {
72+
public int numberOfSteps(int num) {
73+
int cnt = 0;
74+
while (num != 0) {
75+
if (num % 2 == 1)
76+
num--;
77+
else
78+
num /= 2;
79+
cnt++;
80+
}
81+
return cnt;
82+
}
83+
}
7284
```
7385

7486
### **...**

solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ Step 4) 1 is odd; subtract 1 and obtain 0.
6161
### **Java**
6262

6363
```java
64-
64+
class Solution {
65+
public int numberOfSteps(int num) {
66+
int cnt = 0;
67+
while (num != 0) {
68+
if (num % 2 == 1)
69+
num--;
70+
else
71+
num /= 2;
72+
cnt++;
73+
}
74+
return cnt;
75+
}
76+
}
6577
```
6678

6779
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int numberOfSteps(int num) {
3+
int cnt = 0;
4+
while (num != 0) {
5+
if (num % 2 == 1)
6+
num--;
7+
else
8+
num /= 2;
9+
cnt++;
10+
}
11+
return cnt;
12+
}
13+
}

solution/1600-1699/1678.Goal Parser Interpretation/README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,29 @@ G -&gt; G
6464
<!-- 这里可写当前语言的特殊实现逻辑 -->
6565

6666
```java
67-
67+
class Solution {
68+
public String interpret(String command) {
69+
StringBuilder sb = new StringBuilder();
70+
int p = 0, q = 1;
71+
for (; p < command.length(); p++, q++) {
72+
char c = command.charAt(p);
73+
if (c == 'G')
74+
sb.append('G');
75+
if (c == '(') {
76+
if (command.charAt(q) == ')') {
77+
sb.append("o");
78+
p++;
79+
q++;
80+
} else {
81+
sb.append("al");
82+
p += 2;
83+
q += 2;
84+
}
85+
}
86+
}
87+
return sb.toString();
88+
}
89+
}
6890
```
6991

7092
### **...**

solution/1600-1699/1678.Goal Parser Interpretation/README_EN.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,29 @@ The final concatenated result is &quot;Goal&quot;.
5757
### **Java**
5858

5959
```java
60-
60+
class Solution {
61+
public String interpret(String command) {
62+
StringBuilder sb = new StringBuilder();
63+
int p = 0, q = 1;
64+
for (; p < command.length(); p++, q++) {
65+
char c = command.charAt(p);
66+
if (c == 'G')
67+
sb.append('G');
68+
if (c == '(') {
69+
if (command.charAt(q) == ')') {
70+
sb.append("o");
71+
p++;
72+
q++;
73+
} else {
74+
sb.append("al");
75+
p += 2;
76+
q += 2;
77+
}
78+
}
79+
}
80+
return sb.toString();
81+
}
82+
}
6183
```
6284

6385
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String interpret(String command) {
3+
StringBuilder sb = new StringBuilder();
4+
int p = 0, q = 1;
5+
for (; p < command.length(); p++, q++) {
6+
char c = command.charAt(p);
7+
if (c == 'G')
8+
sb.append('G');
9+
if (c == '(') {
10+
if (command.charAt(q) == ')') {
11+
sb.append("o");
12+
p++;
13+
q++;
14+
} else {
15+
sb.append("al");
16+
p += 2;
17+
q += 2;
18+
}
19+
}
20+
}
21+
return sb.toString();
22+
}
23+
}

0 commit comments

Comments
 (0)