saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • c++
  • 개발자 취업준비
  • BFS
  • 백준
  • 파이썬
  • 개발자
  • 딥러닝
  • 온라인저지
  • DFS
  • 릿코드
  • 알고리즘
  • two pointer
  • 취준
  • 알고리즘문제해결
  • 문제해결능력
  • LeetCode
  • 취업준비
  • Python
  • 딕셔너리
  • 리트코드

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 557. Reverse Words in a String III

2022. 9. 23. 01:52

557. Reverse Words in a String III

Easy

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

 

Example 1:

Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"

Example 2:

Input: s = "God Ding"
Output: "doG gniD"

 

Constraints:

  • 1 <= s.length <= 5 * 104
  • s contains printable ASCII characters.
  • s does not contain any leading or trailing spaces.
  • There is at least one word in s.
  • All the words in s are separated by a single space.

문제 풀이

  1. 문자열이 주어진다. 띄어쓰기로 나누어져 있다. 띄어쓰기마다 존재하는 단어들을 뒤집어야한다.
  2. 스트링의 제한을 보면 5 * 10^4 이기 때문에, N^2 으로 풀어도 문제없을 것 같다. (브루트 포스)
  3. 파이썬의 스플릿과 슬라이스를 사용하거나 투포인터를 사용해서 풀 수 있다. 

소스 코드

투포인터

class Solution:
    def reverseWords(self, s: str) -> str:
        s_size = len(s)
        space_idx = -1
        s = list(s)
        for i in range(s_size + 1):
            if i == s_size or s[i] == ' ':
                start_idx = space_idx + 1
                end_idx = i - 1
                while start_idx < end_idx:
                    s[start_idx], s[end_idx] = s[end_idx], s[start_idx]
                    start_idx += 1
                    end_idx -= 1
                space_idx = i
        s = ''.join(s)
        return s

스플릿 & 슬라이스

class Solution:
    def reverseWords(self, s: str) -> str:
        result = split(' ', s)
        result = ' '.join([word[::-1] for word in result])
        return result
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 1680. Concatenation of Consecutive Binary Numbers  (0) 2022.09.24
[LeetCode] 1272. Remove Interval  (1) 2022.09.23
[LeetCode]1338. Reduce Array Size to The Half  (0) 2022.08.20
[LeetCode] 1570. Dot Product of Two Sparse Vectors  (0) 2022.08.18
[LeetCode] 28. Implement strStr()  (0) 2022.08.17
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 1680. Concatenation of Consecutive Binary Numbers
    • [LeetCode] 1272. Remove Interval
    • [LeetCode]1338. Reduce Array Size to The Half
    • [LeetCode] 1570. Dot Product of Two Sparse Vectors
    saurus2
    saurus2
    Simple is Best

    티스토리툴바