Loading the catalog…
Loading the catalog…
문제 내가 생각했을때 문제에서 원하는부분 문자열 my_string이 매개변수로 주어집니다. my_string은 소문자, 대문자, 자연수로만 구성되어있습니다. my_string안의 자연수들의 합을 return하도록 solution 함수를 완성해주세요. 내가 이 문제를 보고 생각해본 부분 정규식 패턴 생성하기: Pattern.compile("\\d+")는 하나 이상의 숫자가 연속되는 규칙을 정의하다. pattern.matcher(my_string)은 전달받은 문자열에서 해당 규칙을 검색할 준비를 하다. 숫자 추출 및 누적하기: while (matcher.find()) 반복문은 문자열 전체를 탐색하며 규칙에 일치하는 숫자를 추출하다. Integer.parseInt(matcher.group())은 찾아낸 문자열 숫자를 정수형으로 변환하다. 변환된 정수 값을 answer 변수에 계속해서 합산하다. 결과 반환하기: 반복이 완료되면 모든 숫자의 합이 담긴 answer를 리턴하다. 테스트 실행하기: main 메서드에서 예시 데이터를 활용해 결과가 잘 출력되는지 확인하다. 코드로 구현 import java.util.regex.Pattern; import java.util.regex.Matcher; class Solution { public int solution(String my_string) { int answer = 0; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(my_string); while (matcher.find()) { answer += Integer.parseInt(matcher.group()); } return answer; } } 프로그래머스 코드 package programmers.programmers2; import java.util.regex.Pattern; import java.util.regex.Matcher; // 프로그래머스 숨어있는 숫자의 덧셈 (2) public class Main158 { public static int solution(String my_string) { int answer = 0; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(my_string); while (matcher.find()) { answer += Integer.parseInt(matcher.group()); } return answer; } public static void main(String[] args) { // 테스트 케이스 확인용 코드 String testStr1 = "aAb1B2cC34oOp"; String testStr2 = "1a2b3c4d123Z"; System.out.println(solution(testStr1)); // 예상 결과: 37 System.out.println(solution(testStr2)); // 예상 결과: 133 } } 위에 있는 코드를 변경한 코드 마무리 코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.
What RADAR observed and classified to build this opportunity. It is what the source published, not a verification that the offer is still active.
프로그래머스 숨어있는 숫자의 덧셈 (2). 문제 내가 생각했을때 문제에서 원하는부분 문자열 my_string이 매개변수로 주어집니다. my_string은 소문자, 대문자, 자연수로만 구성되어있습니다. my_string안의 자연수들의 합을 return하도록 solution 함수를 완성해주세요. 내가 이 문제를 보고 생각해본 부분 정규식 패턴 생성하기: Pattern.compile("\\d+")는 하나 이상의 숫자가 연속되는 규칙을 정의하다. pattern.matcher(my_string)은 전달받은 문자열에서 해당 규칙을 검색할 준비를 하다. 숫자 추출 및 누적하기: while (matcher.find()) 반복문은 문자열 전체를 탐색하며 규칙에 일치하는 숫자를 추출하다.…