Algorithm Study - 003
·
Algorithm/JAVA
Problem https://app.codility.com/programmers/lessons/6-sorting/distinct/ Write a function class Solution { public int solution(int[] A); } that, given an array A consisting of N integers, returns the number of distinct values in array A. For example, given array A consisting of six elements such that: A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1 the function should return 3, because the..
Algorithm Study - 002
·
Algorithm/JAVA
Problem https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Example 1: singly-linked list에 관한 문제이다. 단순 연결 리스트 두 개를 오름차순으로 merge하는 것인데 재귀함수가 떠오른다면 성공. Solution /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *..
Algorithm Study - 001
·
Algorithm/JAVA
Problem https://leetcode.com/problems/valid-parentheses/ Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. 먼저 적힌 괄호는 나중에 닫히는 이른바 FILO 방식의 알고리즘을 요구하는 문제. Stack이 제일 먼저 떠오른다면 반은 먹고 들어갔다고 할 수 ..
숫자 반전 알고리즘
·
Algorithm/JAVA
//핵심 주어진 수의 맨 뒷자리부터 %로 하나씩 가져오고, 자릿수 0을 붙여주면서 기존 수를 / 을 통해 뒷자리부터 삭제 시켜 나간다. class Solution { public boolean isPalindrome(int x) { if(x < 0) { return false; } int originX = x; int result = 0; while(x != 0){ result = (result * 10) + x%10; x /= 10; } if(result == originX){ return true; } return false; } }