본문 바로가기

Algorithm/Leet Code

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/12/19 - [Algorithm/Leet Code] - LeetCode #706 DesignHashMap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이터베이..

2019/12/11 - [Algorithm/Leet Code] - LeetCode #1047 RemoveAllAdjacentDuplicatesInString. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술..

2019/12/06 - [Algorithm/Leet Code] - LeetCode #844 BackspaceStringCompare. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, 데이..

 

 

 

2019/11/02 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #7 MostExpensive. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면접, ..

2019/10/31 - [Algorithm/Code Fights (Code Signal)] - CodeSignal Intro Databases #6 VolleyballResults. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,기술면..

 

 

2019/08/14 - [Life/Item review] - Mi Band 4 review, 미밴드4 후기, 장점, 단점, 리뷰, 한글, global review, 미밴드4 글로벌 후기, 리뷰, 구매, 사용방법, setting, 세팅, ProsNCons

 

 

2018/10/19 - [Programming/Design Pattern ] - Design pattern - Prototype (디자인패턴 - 프로토타입) / Java C++ C#

 

 

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #60 sudoku. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

2019/01/12 - [Algorithm/Code Fights (Code Signal)] - Aracade Intro #59 spiralNumbers. Algorithm,알고리즘,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive

 

 

2018/12/28 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 - (#9, Tell, Don't Ask)

2018/12/26 - [Programming/Software Architecture] - Perfecting OO's Small Classes and Short Methods. 완벽한 객체지향의 작은 클래스와 짧은 메소드, Book:ThoughtWorks Anthology, Java,cpp,자바,oop,좋은코드,객체지향프로그래밍 (1)

 

 

2019/01/14 - [Programming/Java] - 자바 메모리 누수 체크/확인/고치는 방법, Memory leak check/fix in Java application, cleanCode/좋은코드/oop/객체지향

 

 

2019/02/19 - [Life/Health care] - Lysine 라이신 usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/16 - [Life/Health care] - Finasteride 피나스테라이드,탈모약 usage/side effects/dosage 효능/부작용/효과/sexual effect/두타스테라이드/프로페시아/propecia/finpecia/카피약/copy drug/hair loss

2019/02/25 - [Life/Health care] - Folic Acid 엽산 vitaminB9,비타민M usage/side effects/dosage 효과/효능/부작용/성인,소아 용법, 복용법

2019/02/28 - [Life/Health care] - Vitamin K, 비타민 K usage/side effects/dosage 효능/부작용/성인,소아 용법

2019/03/03 - [Life/Health care] - Vitamin B1, Thiamine, 비타민 B1, 티아민 usage/side effects/dosage 효능/부작용/성인,소아 용법