Given a set of characters, compress them using the in place algorithm.
The compressed length must always be less than or equal to the original array length.
Each element of the array should be a character of length 1 (not an int integer type).
After modifying the input array in place, returns the new length of the array.
Example 1:
Input: ["a","a","b","b","c","c","c"]
Output: return 6. The first 6 characters of the input array should be: ["a","2","b","2","c","3"]
Explain:
'aa' is replaced by 'a2'. "bb" is replaced by "b2". "ccc" is replaced by "c3".
Traverse list:
If the next element is the same as the current element, and ans is the same, the counter adds one;
If the next element is different from the current element, the ans and counter are spliced after the transformation to the result string; then the ans is reset and the counter is reset;
1 class Solution(object):
2 def compress3(self, chars):
3 # Set pointer and write pointer of record element
4 anchor = write = 0
5 # Traversing primitive list
6 for read, c in enumerate(chars):
7 # If you read the original list End, or the current character is different from the next character, indicating that the end of the same character is read
8 if read + 1 == len(chars) or chars[read + 1] != c:
9 # Write the elements of the record to the new list in
10 chars[write] = chars[anchor]
11 # Write pointer backward
12 write += 1
13 # If the read pointer goes further than the record pointer
14 if read > anchor:
15 # Character and splice counter to new list after
16 for digit in str(read - anchor + 1):
17 chars[write] = digit
18 write += 1
19 # Assign new value to record pointer
20 anchor = read + 1
21 res = [0]*write
22 for k in range(write):
23 if k<write:
24 res[k] = chars[k]
25 print(res)
26 # Returns the position of the write pointer, which is new list Length
27 return write
28
29 def compress2(self, chars):
30 """
31 :type chars: List[str]
32 :rtype: int
33 """
34 # Writing pointer
35 ans = 0
36 # Record pointer
37 index = 0
38 # Traversing primitive list
39 for i, ch in enumerate(chars):
40 # If it's traversed list If the last or current character of is different from the next character, the reading of the same character ends
41 if i == len(chars) - 1 or chars[i + 1] != ch:
42 # Assign the element pointed by the record pointer to the new list
43 chars[ans] = chars[index]
44 # Write pointer backward
45 ans += 1
46 # If the reading pointer is in front of the recording pointer
47 if i > index:
48 # Calculate the length of the same character
49 length = i - index + 1
50 for j in str(length):
51 chars[ans] = j
52 ans += 1
53 index = i + 1
54 result = [0] * ans
55 for k in range(ans):
56 if k < ans:
57 result[k] = chars[k]
58 print(result)
59 return ans
60
61 """
62 Integers index Bitwise stitching to the back of the collection
63 """
64
65 def intToList(self, result, index):
66 """
67 :type result: List[str]
68 :type index: int
69 :rtype: List[str]
70 """
71 temp = str(index)
72 for i in range(len(temp)):
73 result.append(temp[i])
74 return result
75
76 def compress(self, chars):
77 """
78 :type chars: List[str]
79 :rtype: int
80 """
81 print("Length of set:", len(chars))
82 # Counter initialization
83 index = 1
84 # Result list Length
85 listLen = 1
86 for i in range(1, len(chars)):
87 # print(chars[i])
88 if chars[i] == chars[i - 1]:
89 index += 1
90 elif chars[i] != chars[i - 1]:
91 chars[listLen] = str(index)
92 listLen += 1
93 chars[listLen] = chars[i]
94 listLen += 1
95 index = 1
96 print(chars)
97 return listLen
98
99
100 if __name__ == '__main__':
101 solution = Solution()
102 print(solution.compress2(["a"]))
103 print(solution.compress2(["a", "a", "b", "b", "c", "c", "c"]))
104 print(solution.compress2(["a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]))