[TIL]20210829

박창현·2021년 8월 29일
0

TODAY I LEARNED

목록 보기
37/53

알고리즘

10828. 스택 (백준)

import sys

N = int(sys.stdin.readline())
stack = []
for _ in range(N):
    command = sys.stdin.readline().split()
    com = command[0]
    if com == "push":
        stack.append(command[1])
    elif com == "pop":
        if len(stack) == 0:
            print(-1)
        else:
            print(stack.pop())

    elif com == "size":
        print(len(stack))
    elif com == "empty":
        if len(stack) == 0:
            print(1)
        else:
            print(0)
    elif com == 'top':
        if len(stack) == 0:
            print(-1)
        else:
            print(stack[-1])

10845. 큐 (백준)

import sys

N = int(sys.stdin.readline())
stack = []
for _ in range(N):
    command = sys.stdin.readline().split()
    com = command[0]
    if com == "push":
        stack.append(command[1])
    elif com == "pop":
        if len(stack) == 0:
            print(-1)
        else:
            print(stack.pop(0))

    elif com == "size":
        print(len(stack))
    elif com == "empty":
        if len(stack) == 0:
            print(1)
        else:
            print(0)

    elif com == 'front':
      if len(stack) == 0:
        print(-1)
      else:
        print(stack[0])


    elif com == 'back':
        if len(stack) == 0:
            print(-1)
        else:
            print(stack[-1])

10866. 덱 (백준)

import sys

N = int(sys.stdin.readline())
stack = []
for _ in range(N):
    command = sys.stdin.readline().split()
    com = command[0]
    if com == "push_front":
        stack.insert(0,command[1])
    elif com=="push_back":
        stack.append(command[1])
    elif com == "pop_front":
        if len(stack) == 0:
            print(-1)
        else:
            print(stack.pop(0))
    elif com == "pop_back":
        if len(stack) == 0:
            print(-1)
        else:
            print(stack.pop())

    elif com == "size":
        print(len(stack))
    elif com == "empty":
        if len(stack) == 0:
            print(1)
        else:
            print(0)

    elif com == 'front':
      if len(stack) == 0:
        print(-1)
      else:
        print(stack[0])


    elif com == 'back':
        if len(stack) == 0:
            print(-1)
        else:
            print(stack[-1])

9012. 괄호 (백준)

import sys

N=int(sys.stdin.readline())

for _ in range(N):
    cnt1=0
    cnt2=0
    no=0
    A=sys.stdin.readline()
    for i in range(len(A)):
        if A[i]==")":
            cnt2+=1
        if A[i]=="(":
            cnt1+=1

        if cnt2>cnt1:
            no=1


    if no==1:
        print("NO")
    elif cnt1==cnt2:
        print("YES")
    else:
        print("NO")

이 문제는 단순히 (, ) 괄호 갯수를 카운트 하는 것이 아니라, 괄호의 열림과 닫힘의 순서위치가 맞아떨어져야한다. 이를 주의.

1748. 수 이어 쓰기 1 (백준)

메모리 초과.

import sys

N=int(sys.stdin.readline())

A=''.join(str(i) for i in range(1, N+1))
print(len(A))

정답.

 n = input()
n_len = len(n) - 1
c = 0
i = 0
while i < n_len:
    c += 9 * (10 ** i) * (i + 1)
    i += 1
c += ((int(n) - (10 ** n_len)) + 1) * (n_len + 1)
print(c)

## 11일 경우
# n=11, n_len =1, while문 1번 순환...
#   c= 9*1*1=9
# c=(11-10+1)*2=4, 9+4
# while의 c는 1~9까지, 밖의 c는 10~11까지의 len

## 121일 경우
# n=121, n_len=2, while문 2번 순환...
#   c= 9*1*1
#   i=1
#   c= 9*10*2 (2는 10~99일때 2자리값임으로.)
#   c= 9+ 180+ (121-100+1(=99))*3 = 255

# while문으로 1~9, 10~99,100~999 등 자릿수 꽉차는 값 계산, 밖에서 나머지 계산.

input 관련.

<input required maxlength="15" type="text" placeholder="What is your name?" />
<button>Log In</button>

여기서 required 는 내용물이 입력된 상태를 요구하는 것이다.
button을 누르면 이 required가 유효한지 유효성 검사를 진행해야하는데, 이를 위해서는 form을 이용해 내용물을 묶어줘야한다.

<form>
  <div id="login-form">
   <input required maxlength="15" type="text" placeholder="What is your name?" />
   <button>Log In</button>
 </div>
 </form>

이렇게 묶어주면, js 없이 유효성검사를 통해 required를 만족하는지 확인할 수 있다.

또한, form을 통해 묶어준다면 js에서 코딩할 필요없이 submit을 할 방법이 많아진다. input타입의 submit또는 button으로.

preventDefault()

const loginForm=document.querySelector("#login-form");
const loginInput=document.querySelector("#login-form input");

function onLoginSubmit(event) {
  event.preventDefault();
  console.log(loginInput.value);
}


loginForm.addEventListener("click", onLoginSubmit);

preventDefault() 이 코드에서는 자동으로 페이지가 새로고침되는 것을 막아준다.

실제로 preventDefault()가 하는 일은 어떠한 event의 디폴트값의 수행을 막는 것이다.

a태그를 이용한 하이퍼링크도 이를 이용하면 아무리 클릭해도 링크에 접속되지 않도록 막아준다.

profile
개강했기에 가끔씩 업로드.

0개의 댓글