[ Swift 알고리즘 입문 ] 기본적으로 알아둬야 할 사항들!!

2022. 5. 19. 16:48Algorithm & DataStructure

1. String 타입 처리에 신경쓴다.
  • Swift 문자열에서 String 은 일반적인 정수형 인덱스로 첨자접근이 가능한 배열(랜덤 접근 컬렉션) 과 다른 구조인 양방향컬렉션으로 되어있음
  • String.Index 타입으로 첨자접근 시 그 시간 복잡도는 O(N) 가 됨. 또한 String 타입의 길이를 체크하는 count 프로퍼티의 경우도 그 시간 복잡도는 O(N)이 됨
2.강제 언래핑 활용 

 스위프트에서 String?  타입과 같은 옵셔널 타입을 강제로 언래핑해 사용 

-> ! 강제 언래핑을 통해 코드 양을 줄이고, 시간관리 할 수 있음, 시간적 여유가 된다면 옵셔널 바인딩을 통한 안전한 접근방식을 고려 가능 

  • 실무에서는 해당 값이 없을 시 (nil) 런타임 에러를 야기해 지양하는 문법, 알고리즘 문제에서 보통 입력값은 구체적인 값이 주어지고 그에 따른 결과값을 반환하도록 요구하는 경우가 많음
3. Array, Set, Dictionary … 등 자료구조 다루는 연습
  • 스위프트 단점 : Heap / Stack / Queue 등을 지원하지 않는다.

추가적으로 검색하거나 정리해야 할 부분..

시간복잡도에 관한 것 따로 찾아보기 

readLine() 이 반환하는 것 찾아보기 


readLine(strippingNewline:)

Returns a string read from standard input through the end of the current line or until EOF(End of File: 끝에 도달했을 때 끝낼 수 있어야 한다는 것) is reached.

func readLine(strippingNewline: Bool = true) -> String?

func readLine(strippingNewline: Bool = true) -> String?

Parameters

strippingNewline

If true, newline characters and character combinations are stripped from the result; otherwise, newline characters or character combinations are preserved. The default is true.

Return Value

The string of characters read from standard input. If EOF  has already been reached when readLine() is called, the result is nil.

Discussion

Standard input is interpreted as UTF-8. Invalid bytes are replaced by Unicode replacement characters.

OR Command Line Input 

enum CommandLine 

 

Reference - 참고 문서 및 블로그

https://developer.apple.com/documentation/swift/1641199-readline/

https://0urtrees.tistory.com/167