본문 바로가기

Stacks: Balanced Brackets A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and ().A matching pair of brackets is not balanced if the set of brackets .. 더보기
Level 2 - 콜라츠 추측 1937년 Collatz란 사람에 의해 제기된 이 추측은, 입력된 수가 짝수라면 2로 나누고, 홀수라면 3을 곱하고 1을 더한 다음, 결과로 나온 수에 같은 작업을 1이 될 때까지 반복할 경우 모든 수가 1이 된다는 추측입니다. 예를 들어, 입력된 수가 6이라면 6→3→10→5→16→8→4→2→1 이 되어 총 8번 만에 1이 됩니다. collatz 함수를 만들어 입력된 수가 몇 번 만에 1이 되는지 반환해 주세요. 단, 500번을 반복해도 1이 되지 않는다면 –1을 반환해 주세요. 풀이 class Collatz { public int collatz(int num) { int answer = 0; while (true){ if (num % 2 == 0){ num = num / 2; } else { num .. 더보기
Linked Lists: Detect a Cycle A linked list is said to contain a cycle if any node is visited more than once while traversing the list.Complete the function provided in the editor below. It has one parameter: a pointer to a Node object named that points to the head of a linked list. Your function must return a boolean denoting whether or not there is a cycle in the list. If there is a cycle, return true; otherwise, return fa.. 더보기