본문 바로가기

배열16

[leetcode 33] Search in Rotated Sorted Array You are given an integer array nums sorted in ascending order, and an integer target. Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). If target is found in the array return its index, otherwise, return -1. 본래 오름차순인 배열이 특정 위치를 중심으로 회전되어 있다. 주어진 target의 위치를 찾아라. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4.. 2020. 10. 17.
[leetcode 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] 문제: 0을 전부 뒤로 미루는 문제 풀이 : 0이 아닌 숫자를 앞에서 부터 채우고 나머지 배열에 0을 채운다. class Solution { public: void moveZeroes(vector& nums) { int last = 0; //0이 아닌 숫자를 앞으로 밀어넣기 for(int i=0; i 2020. 10. 12.
[leetcode 448] Find All Numbers Disappeared in an Array Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space. Example: Input: [4,3,2,7,8,2,3,1] Output: [5,6] 문제 설명: 1부터 n까지 숫자로 구성되어 있는 배열에서 없는 .. 2020. 10. 12.
[leetcode 581] Shortest Unsorted Continuous Subarray Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order. Return the shortest such subarray and output its length. Example 1: Input: nums = [2,6,4,8,10,9,15] Output: 5 Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascend.. 2020. 10. 11.