본문 바로가기

Portpolio/codingtest33

Leetcode 234번 팰린드롬 연결 리스트 import java.util.Deque; import java.util.LinkedList; public class P13 extends ListNode{ P13(int val) { super(val); } public static boolean isPalindrome(ListNode head) { // 데크 선언 Deque deque = new LinkedList(); // 연결 리스트의 값을 데크에 저장 ListNode current = head; while (current != null) { deque.addLast(current.val); current = current.next; } // 양쪽에서 값을 하나씩 비교 while (deque.size() > 1) { if (!deque.pollFi.. 2023. 12. 31.
Leetcode 121번 주식을 사고팔기 가장 좋은 시점 import java.util.Arrays; public class P12 { public static void main(String args[]) { int[] prices1 = {7,1,5,3,6,4}; int profit1 = maxProfit(prices1); System.out.println(profit1); } public static int maxProfit(int[] prices) { if (prices == null || prices.length 2023. 12. 31.
Leetcode 561번 배열 파티션 I import java.util.Arrays; public class P10 { public static void main(String[] args) { int[] nums1 = {1, 4, 3, 2}; int result1 = arrayPairSum(nums1); System.out.println(result1); int[] nums2 = {6, 2, 6, 5, 1, 2}; int result2 = arrayPairSum(nums2); System.out.println(result2); } public static int arrayPairSum(int[] nums) { Arrays.sort(nums); int sum = 0; for(int i = 0; i 2023. 12. 31.
Leetcode 1번 두 수의 합 import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class P7 { public static void main(String args[]) { int[] nums = {2, 7, 11, 15}; //int[] 배열 int target = 9; //합으로 나와야 하는 수 System.out.println(Arrays.toString(twoSum(nums, target))); } //brute force, hashmap을 이용하기 public static int[] twoSum(int[] nums, int target) { Map map = new HashMap(); for(int i = 0; i < nums.len.. 2023. 12. 31.
Leetcode 344번 문자열 뒤집기 import java.util.Arrays; public class P2 { public static void main(String args[]) { P2 p2Instance = new P2(); char[] input = {'h', 'e', 'l', 'l', 'o'}; // reverseString 메소드 호출 p2Instance.reverseString(input); // 뒤집힌 문자열 출력 System.out.println("Reversed String: " + Arrays.toString(input)); } public void reverseString(char[] s) { for (int i = 0, j = s.length - 1; i < j; i++, j--) { char tmp = s[i];.. 2023. 12. 31.
Leetcode 125번 유효한 팰린드롬 - 대괄호와 캐럿 기호, 범위를 나타내는 하이픈 기호를 통해 정규 표현식의 개념을 사용함 public class P1 { public static void main(String args[]) { String input = "AMAIIAMA"; P1 obj = new P1(); boolean isPalindrome = obj.isPalindrome(input); System.out.println(isPalindrome); } public boolean isPalindrome(String s) { String sLower = s.toLowerCase(); String sRemoved = sLower.replaceAll("[^a-zA-Z0-9]", ""); //정규표현식 String revStr = ""; /.. 2023. 12. 31.
프로그래머스 lv0. 문자열 겹쳐쓰기 java https://school.programmers.co.kr/learn/courses/30/lessons/181943 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public String solution(String my_string, String overwrite_string, int s) { String answer = ""; answer = my_string.substring(0, s) + overwrite_string + my_string.substring(s + overwrite_string.length(), my_.. 2023. 12. 19.
프로그래머스 lv3. 단속카메라 java https://school.programmers.co.kr/learn/courses/30/lessons/12982 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr import java.util.Arrays; import java.util.Comparator; public class Solution { public int solution(int[][] routes) { // routes 배열을 진출 지점을 기준으로 오름차순 정렬 Arrays.sort(routes, Comparator.comparingInt(o -> o[1])); // 처음 카메라 위치는 맨 .. 2023. 12. 19.
프로그래머스 lv0. QR Code java https://school.programmers.co.kr/learn/courses/30/lessons/181903 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public String solution(int q, int r, String code) { String answer = ""; for (int i = r; i < code.length() ; i += q) { answer += String.valueOf(code.charAt(i)); } return answer; } } class Solution { public .. 2023. 12. 19.
프로그래머스 lv0. 두 수의 합 java https://school.programmers.co.kr/learn/courses/30/lessons/120802 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr class Solution { public int solution(int num1, int num2) { int answer = -1; answer = num1 + num2; return answer; } } class Solution { public static void main(String[] args) { int solution1 = solution(3, 2); int solution2 =.. 2023. 12. 18.