전체 글

전체 글

    x만큼 간격이 있는 n개의 숫자

    나의 풀이 def solution(x, n): answer = [] for i in range(1, n+1): answer.append(x*i) return answer 다른 사람의 풀이 def number_generator(x, n): return [i for i in range(x, x*n+1, x)]

    문자열 내 p와 y의 개수-자바

    나의 풀이 public class Solution { boolean solution(String s) { int pcount1=0; int ycount2=0; for(int i=0; i 'P'== e).count() == s.chars().filter( e -> 'Y'== e).count(); } } ▶ 일괄적으로 대문자로 동일한 다음에 갯수를 count하는 것이 point

    문자열 내 p와 y의 개수→Counter() 함수!!★

    나의 풀이 def solution(s): answer = True cnt_p=0 cnt_y=0 for i in s: if i == 'p' or i =='P': cnt_p+=1 if i == 'y' or i =='Y': cnt_y+=1 print(cnt_p) print(cnt_y) if cnt_p != cnt_y: return False else: return True 다른 사람의 풀이 from collections import Counter def solution(s): # 함수를 완성하세요 c = Counter(s.lower()) return c['y'] == c['p'] ▶ Counter() 는 각 해당 요소가 몇개 씩 있는지 dictionary 형태로 리턴값을 반환해 준다. ▶ 예를 들면 s매개변수..

    정수 제곱근 판별

    나의 풀이 class Solution { public long solution(long n) { double a=Math.sqrt(n); int a1=(int)a; return a==a1? (long)Math.pow(a1+1, 2): -1; } } ▶ if-else 구문 대신에 삼항 연사자를 사용 ▶ (long) 캐스팅 연산자를 써서 형변환 ▶ math.sqrt() 메서드를 사용해서 제곱근을 구함 ▶ math.pow() 메서드를 이용해서 거듭제곱을 구함