자료구조 & 알고리즘

가장 긴 팰린드롬 부분 문자열을 출력하라input: dcbabcddoutput: dcbabcd  /** * 팰린드롬은 짝수 / 홀수 케이스 구분하는 것 주의 */class Solution { public String longestPalindrome(String s){ if(s.length() == 1){ return s; } String result = s.substring(0, 1); //제일 작은 팰린드롬은 글자 하나 (한글자도 팰린드롬이라고 보는 경우) for (int i = 0; i oddStr.length() ? evenStr : oddStr; result = result.length() > lon..
// 정수형 배열 nums 가 주어졌다.// 연속된 부분 배열의 합 중 가장 큰 값을 출력하세요.// 입출력 예시// nums: -5, 0, -3, 4, -1, 3, 1, -5, 8// 출력: 10// nums: 5, 4, 0, 7, 8// 출력: 24public class Main { public static int solution(int[] nums) { return divideSubArray(nums, 0, nums.length - 1); } private static int divideSubArray(int[] nums, int left, int right) { if (left == right) { return nums[left]; ..
// 정수 num 이 주어졌을 때,// num 숫자에서 두 자리를 최대 한번만 교체하여 얻을 수 있는 최대값을 출력하세요.// 입출력 예시// num: 2736// 출력: 7236// 입력: 7116// 출력: 7611public class Main { public static int solution(int num) { char[] chars = String.valueOf(num).toCharArray(); int max = Integer.MIN_VALUE; int[] maxArr = new int[chars.length]; for (int i = chars.length - 1; i >= 0; i--) { max = Math.ma..
// 원형 루트 상에 n 개의 주유소가 있다.// 각 주유소의 가스 보유량은 gas 배열에 주어지고,// 해당 주유소에서 다음 주유소로의 이동 비용은 cost 배열에 주어진다.// 어떤 위치에서 차량이 가스를 채워 출발하면 모든 주유소를 방문하고 원래의 위치로 돌아올 수 있는지// 계산하는 프로그램을 작성하세요.// 입출력 예시// gas: 1, 2, 3, 4, 5// cost: 3, 4, 5, 1, 2// 출력: 3// gas: 2, 3, 4// cost: 3, 4, 3// 출력: -1public class Main { public static int solution(int[] gas, int[] cost) { int totalGas = 0; int resultIdx = ..
// 양의 정수 배열 prices 가 주어졌다.// 각 원소의 의미는 주식 가격을 의미한다.// 한 번에 한 주만 보유할 수 있다고 할 때 prices 를 보고 사고 팔기를 반복해서// 얻을 수 있는 최대 수익을 반환하는 프로그램을 작성하세요.// 입출력 예시 // 1 3 4 5 5 6// prices: 5, 1, 6, 4, 3, 5// 출력: 7// prices: 1, 2, 3, 4, 5// 출력: 4public class Main { public static int solution(int[] prices) { if(prices == null || prices.length
// 정수형 nums 배열이 주어졌다.// 각 원소의 값은 해당 위치에서 오른쪽으로 이동할 수 있는 최대 값이다.// 첫 번째 위치에서 시작해서 가장 끝까지 이동이 가능한지 판별하는 프로그램을 작성하세요.// 이동이 가능하면 true, 불가능하면 false 를 반환하세요.// 입출력 예시// nums: 2, 3, 0, 1, 4// 출력: true// nums: 3, 0, 0, 1, 1// 출력: true// nums: 3, 2, 1, 0, 4// 출력: falsepublic class Main { public static boolean solution(int[] nums) { int pos = 0; // 갈 수 있는 최대 위치 for (int i = 0; i = nums.l..
// 주어진 nums 배열에서 3 개의 합이 0이 되는 모든 숫자들을 출력하세요.// 중복된 숫자 셋은 제외하고 출력하세요.// 입출력 예시// nums: {-1, 0, 1, 2, -1, -4}; // 출력: [[-1, -1, 2], [-1, 0, 1]]// nums: {1, -7, 6, 3, 5, 2}// 출력: [[-7, 1, 6], [-7, 2, 5]] /** * 어려움 ! */public class Main { public static ArrayList> solution(int[] nums) { Arrays.sort(nums); ArrayList> list = new ArrayList(); for (int i = 0; i (Arrays.asList(num..
// 문자열 s 를 거꾸로 출력하는 프로그램을 작성하세요.// 단, 각 단어의 알파벳 순서는 그대로 출력합니다.// 문장에 공백이 여러개일 시, 단어와 단어 사이 하나의 공백을 제외한 나머지 공백은 제거하세요.// 입출력 예시// 문자열 s: "the sky is blue"// 출력: "blue is sky the"// 문자열 s: " hello java "// 출력: "java hello"public class Main { public static String solution(String s) { s = removeWhiteSpace(s); String reversed = reverseString(s.toCharArray(), 0, s.length() - ..
기수정렬public class Main { // 기수정렬 -> 큐를 이용하여 낮은 자리수부터 비교.. private static void radixSort(int[] arr) { Queue[] queueArr = new Queue[10]; //0부터 9까지 for (int i = 0; i (); } int maxLen = getMaxLen(arr); int div = 1; int idx = 0; for (int i = 0; i  계수정렬public class Main2 { public static void countingSort(int[] arr) { int maxNumber = Array..
public class Main { public static void quickSort(int[] arr, int left, int right) { if (left >= right) { return; } int pivot = partition(arr, left, right); quickSort(arr, left, pivot - 1); quickSort(arr, pivot + 1, right); } public static int partition(int[] arr, int left, int right) { int pivot = left; int lo = left; //arr[lo]가..
꾸준함의 미더덕
'자료구조 & 알고리즘' 카테고리의 글 목록 (2 Page)