Skip to content

Commit 281debe

Browse files
committed
[Create] LeetCdoe_1805 정수 추출
1 parent 13a5686 commit 281debe

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

alphanumeric.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// LeetCode_1805
2+
// 2021.04.21
3+
// Easy
4+
5+
6+
import java.util.ArrayList;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
10+
public class alphanumeric {
11+
public static void main(String[] args) {
12+
// System.out.println(new alphanumeric().numDifferentIntegers("a123bc34d8ef34"));
13+
System.out.println(new alphanumeric().numDifferentIntegers("167278959591294"));
14+
}
15+
public int numDifferentIntegers(String word) {
16+
StringBuilder s = new StringBuilder(word);
17+
List<Integer> list = new ArrayList<>();
18+
List<String> sublist = new ArrayList<>();
19+
HashSet<List> set1,set2;
20+
String tmp ="";
21+
int len = s.length();
22+
23+
for(int i = 0; i<s.length(); i++){
24+
if(i == len-1)
25+
s.append("e");
26+
if(!(s.charAt(i) >= 'a' && s.charAt(i) <= 'z'))
27+
tmp += s.charAt(i);
28+
else
29+
if(!tmp.equals("")) {
30+
// if(word.substring(0,i-tmp.length()).contains(tmp))
31+
// continue;
32+
try {
33+
list.add(Integer.valueOf(tmp));
34+
}catch (NumberFormatException e){
35+
sublist.add(tmp);
36+
}
37+
tmp ="";
38+
}
39+
}
40+
41+
set1 = new HashSet(list);
42+
set2 = new HashSet(sublist);
43+
return set1.size()+ set2.size();
44+
}
45+
}
46+
47+
/* SimpleSolution_출처: leetcode
48+
class Solution {
49+
public int numDifferentIntegers(String word) {
50+
String[] arr = word.replaceAll("[a-zA-Z]", " ").split("\\s+");
51+
Set<String> set = new HashSet<String>();
52+
53+
for (String str : arr) {
54+
if (!str.isEmpty())
55+
set.add(String.valueOf(str.replaceAll("^0*","")));
56+
}
57+
58+
return set.size();
59+
}
60+
}
61+
*/
62+

0 commit comments

Comments
 (0)