LeetCode #912 SortAnArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스..
LeetCode #912 SortAnArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이스, sql, query, 쿼리
Runtime: 60 ms, faster than 93.71% of C++ online submissions for Sort an Array.
Memory Usage: 12.5 MB, less than 94.44% of C++ online submissions for Sort an Array.
STL 쓰면 되고, 삽입정렬 한번 해봤는데 time limit 걸림
LeetCode #912
Q.
Given an array of integers nums, sort the array in ascending order.
주어진 정수 배열을 오름차순으로 정렬하면 됨
Example 1:
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Example 2:
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Constraints:
- 1 <= nums.length <= 50000
- -50000 <= nums[i] <= 50000
Process
// Process
// Insertion sort
//1. Input int array
//2. Iterate from 1 to the end (i)
// 2.1. Iterate from i to the 0 (j)
// 2.1.1. Check if [j] < [j-1]
// 2.1.1.1. If so -> next
// 2.1.1.2. If not -> swap [j-1] with [j]
//3. Finish
// 처리과정
// 삽입정렬
//1. 정수배열 입력받는다.
//2. 1부터 끝까지 반복한다 (i)
// 2.1. i 부터 1 까지 반복한다 (j)
// 2.1.1. [j-1] 값과 [j] 값 비교해서
// 2.1.1.1. [j-1] 이 더 크면 넘어간다.
// 2.1.1.2. [j] 값이 더 크면, 자리 교체한다.
//3. 끝낸다.
Code.. lemme see example code!!!
코드.. 예제코드를 보자!!!
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums;
// bool isDone;
// for (int i = 1; i < nums.size(); ++i) {
// isDone = false;
// for (int j = i; !isDone && j > 0; --j) {
// if (nums[j] < nums[j-1]) {
// int temp = nums[j];
// nums[j] = nums[j-1];
// nums[j-1] = temp;
// } else {
// isDone = true;
// }
// }
// }
// return nums;
}
};
Something else you might like...?
2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법
2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법