TIL 23.01.13

쓰옹·2023년 1월 13일
0

개발자를 향해~~TIL✍

목록 보기
65/87

TODAY

  • 알고리즘 문제 풀이
  • sql 강의
  • sql 문제풀이
    • 스파르타 sql강의 중 퀴즈
    • 프로그래머스 문제풀이

알고리즘

프로그래머스 세균증식, 직삼각형 출력하기 문제풀이

세균증식

마리수(n) * 2경과시간(t)2^{ 경과시간(t)} 을 하면 됨.
최근에 제곱수 내장함수를 알게되서 그걸 사용해서 문제를 풀었다.

return n * (int)Math.pow(2, t);

다른 사람 풀이 중에 for문으로 푼 것도 있어서 갖고와봤다.

for(int i = 0; i < t; i++) {
		n *= 2;
}
return n;

직삼각형 출력하기

위에랑 아주 반대 경우
이중for문 써서 돌렸는데 내장함수가 있었음

for(int i = 1; i <= n; i++) {
	for(int j = 1; j <= i; j++) {
    	System.out.print("*");
    }
    System.out.println();
}

내장함수 쓴거

for(int i = 1; i <= n; i++) {
        System.out.println("*".repeat(i)); 
}

repeat() 문자열을 반복해준다. 근데 자바 11에 추가된거라 자바 8엔 없지롱

sql

  • 인덱스
  • with
# with 안쓴거
select b.cnt_checkins, a.cnt_total
from
(
	select course_id, count(*) as cnt_total from orders
	group by course_id
) a
inner join (
	select course_id, count(distinct(user_id)) as cnt_checkins from checkins
	group by course_id
) b
on a.course_id = b.course_id

# with 쓰면
with table1 as (
	select course_id, count(distinct(user_id)) as cnt_checkins from checkins
	group by course_id
), table2 as (
	select course_id, count(*) as cnt_total from orders
	group by course_id
)

select b.cnt_checkins, a.cnt_total
from table2 a 
inner join table1 b on a.course_id = b.course_id;

보기에 깔끔해짐

  • substring
SUBSTRING_INDEX(field, '기준', index_num)

select user_id, email, SUBSTRING_INDEX(email, '@', 1) from users;
# email을 @기준으로 쪼개고 그 중 첫번 째 조각을 가져와라

SUBSTRING(field, 첫 글자 위치, 개수)

select SUBSTRING(created_at, 1, 10) as date, count(*) from orders
# 생성시간의 첫번째 글자부터 10개 출력해라)

문자열 자르기

profile
기록하자기록해!

0개의 댓글