본문 바로가기

반응형

old

(225)
파이썬의 자료구조-리스트 List 리스트 List 정의 파이썬의 자료구조 중 하나 다른 언어의 어레이array와 매우 비슷함. 파이썬 자료구조의 종류 리스트 List 튜플 Tuple 딕션어리 Dictionary 셋 set 선언 temp=[] temp=list() temp=[5,9,44] 인덱싱 특징 list안에 list 넣기 가능 - 이 경우 이는 다른 언어의 2D array와 같음 string 역시 기본적으로 array이므로, 똑같은 현상이 발생 슬라이싱 [시작 인덱스 : 마지막 인덱스-1 : 조건 인덱스] [0:8:3] = index 0 에서 7 까지 3을 더해가며 선택하라 [:8:] = index 0 에서 7 까지 1을 더해가며 선택하라 [7::] = index 7 에서 -1 까지 1을 더해가며 선택하라 [::-1] = index ..
Add Strings 문제풀이 문제 Problem_Link 내 답 1시간 제한 인터넷 사용 안함 내 코드 class Solution { public String addStrings(String num1, String num2) { StringBuilder sb = new StringBuilder(); int num1Leng = num1.length() - 1; int num2Leng = num2.length() - 1; int round = 0; for (int i = Math.max(num1.length(), num2.length()) - 1; i > -1; --i) { if (num1Leng < 0) { sb.append(String.valueOf(((num2.charAt(num2Leng) - 48) + round)%10)); i..
Remove Duplicates from Sorted Array 문제풀이 문제 Problem_Link 내 답 1시간 제한 인터넷 사용 안함 내 코드 class Solution { public int removeDuplicates(int[] nums) { if(nums==null ||nums.length==0) return 0; int temp = -101; int count=0; int pos=0; for(int i=0;i
선택정렬 Selection Sort 정의 오름차순으로 정렬하는 기초적인 알고리즘 중 하나 기법 무차별 대입 Brute Force 알고리즘 순서 순회해서 최소값을 찾는다 첫번째 값과 최소값을 스왑한다 두번쨰 값부터 순회해서 최소값을 찾는다 두번쨰 값과 최소값을 스왑한다 마지막 직전까지 반복한다. 자바 코드 public static int[] selectionSort(int[] input) { for (int i = 0; i i; --j) { if (input[j] < input[i]) { int temp = input[i]; input[i] = input[j]; input[j] = temp; } } } return input; } 평균..
버블정렬 Bubble Sort 정의 오름차순으로 정렬하는 기초적인 알고리즘 중 하나 기법 무차별 대입 Brute Force 알고리즘 순서 두 숫자(첫번째와 두번째)를 비교하며 순회한다 두 숫자를 비교하여, 작은수를 왼쪽, 큰수를 오른쪽으로 이동시킨다. 첫번째 선회를 하면, 맨 오른쪽에 가장 큰 수가 위치하게 된다 마찬가지로 하나씩 줄여가며 순회하면 완성 자바 코드 public static int[] bubbleSort(int[] input) { for (int i = 0; i input[j + 1]) { int temp = input[j]; input[j] = input[j..
Two Sum문제 풀이 문제 Problem_Link 내 답 1시간 제한 인터넷 사용 안함 Overview input : int[] nums, int target output : int[] My code class Solution { public int[] twoSum(int[] nums, int target) { int temp[] = new int[nums.length]; int rtn[] = new int[2]; for (int i = 0; i < nums.length; i++) { temp[i] = target - nums[i]; for (int j = 0; j < i; j++) { if (temp[j] == nums[i]) { rtn[0] = j; rtn[1] = i; return rtn; } } } rtn[0] = 0..
Count Binary Substrings문제 풀이 문제 Problem_Link 내 답 1시간 제한 인터넷 사용 안함 내 코드 Compile Error 최선의 답 인터넷과 책에서 찾아본 답 class Solution { public int countBinarySubstrings(String s) { int curRun = 1; int preRun = 0; int count = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == s.charAt(i - 1)) ++curRun; else { count += Math.min(curRun, preRun); preRun = curRun; curRun = 1; } } return count + Math.min(curRun, preRun); } } Time co..
[Java코드]알파벳 오더로 정렬하기 알파벳 오더로 정렬하기 아스키 코드 룰에 따라서 [숫자][대문자][소문자]로 정렬됩니다 0 ~ 9, A ~ Z, a ~ z Time complexity : O( log n) Space complexity : O(1) List Collections.sort(List_values); Array Arrays.sort(String[]array); Time complexity : O(n^2) Space complexity : O(1) public static String[] LexicalOrder(String[] words) { int n = words.length; for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { if (words[i].c..

반응형