Skip to content

Commit

Permalink
#9 443번 String Compression
Browse files Browse the repository at this point in the history
  • Loading branch information
leejw-lu committed Feb 18, 2024
1 parent 7c2e2a2 commit 76a200c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions leetcode/443_String Compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#After you are done modifying the input array ~

from collections import deque
class Solution:
def compress(self, chars: List[str]) -> int:
result=0
i=0

while i<len(chars):
cur=chars[i]
count=0
while i<len(chars) and chars[i]==cur:
count+=1
i+=1
chars[result]=cur
result+=1
if count>1:
for c in str(count): #10이상인 경우 split 할 수 있게.
chars[result]=c
result+=1

return result

# if len(chars)==1:
# return 1

# result=[]
# count=1

# for i in range(1,len(chars)):
# if chars[i-1]!=chars[i]:
# if count==1: #중복원소 없을 경우 숫자X
# result.append(chars[i-1])
# else:
# result.append(chars[i-1])
# #count 10이상이면 split
# if count>=10:
# result.append(str(count//10))
# result.append(str(count%10))
# else:
# result.append(str(count))
# count=1 # 초기화
# else:
# count+=1
# if i==len(chars)-1:
# result.append(chars[i-1])
# if count>=10:
# result.append(str(count//10))
# result.append(str(count%10))
# else:
# result.append(str(count))

# #print(result)
# idx=len(result)
# return idx

1 comment on commit 76a200c

@leejw-lu
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After you are done modifying the input array~

문제 조건을 잘 읽어야 함!!!! 따로 배열 공간 쓰는게 아니라 입력으로 주어진 배열을 수정하라는 것이었음.

Please sign in to comment.