본문 바로가기

코딩테스트 54

[LeetCode] 자바 448. Find All Numbers Disappeared in an Array 문제는 '배열에 없는 숫자 찾기, 없는 숫자란 배열 크기의 정수중에 없는 숫자를 뜻함' 첫번째. class Solution { public List findDisappearedNumbers(int[] nums) { Arrays.sort(nums); List result = new ArrayList(); for(int i =0; i< nums.length; i++){ if(nums[i] != i+1) { if(Arrays.binarySearch(nums, i+1) 2020. 8. 10.
[LeetCode] 자바 169. Majority Element 문제는 '주어진 배열에서 출현빈도가 절반 이상인 값을 찾아라' 입니다. 첫번째. class Solution { public int majorityElement(int[] nums) { int majorityElementNum = nums.length; HashMap hash_table = new HashMap(); //전체 케이스 세고 for (int i : nums) { hash_table.put(i, hash_table.getOrDefault(i, 0) + 1); } int maxNum = 0; int resultIndex = 0; //가장 많이 나온거로 for (int i : nums) { if (hash_table.get(i) > maxNum) { maxNum = hash_table.get(i);.. 2020. 8. 9.
[LeetCode] 자바, 283. Move Zeroes 283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Note: You must do this in-place without making a copy of the array. Minimize the total number of operations. 문제는 'int Array를 하나 주는데, 거기서 0을 모두 맨 뒤로 보내라' 입니다. 첫번째 시도는 class Solution { public void mo.. 2020. 8. 9.
[LeetCode] 자바 136. Single Number 136. Single Number Easy Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 문제는 간단합니다. int array 가 들어오는데 거기서 홀로 있는 number를 찾아서 return 하시오. class Solution { pu.. 2020. 8. 8.