1. 程式人生 > >leetcode 443. String Compression(字串壓縮)

leetcode 443. String Compression(字串壓縮)

問題描述:

  Given an array of characters, compress it in-place.

  The length after compression must always be smaller than or equal to the original array.

  Every element of the array should be a character (not int) of length 1.

  After you are done modifying the input array in-place, return the new length of the array.

Follow up:
  Could you solve it using only O(1) extra space?

這裡寫圖片描述

Note:

  All characters have an ASCII value in [35, 126].
  1 <= len(chars) <= 1000.

程式碼:

class Solution {
    public int compress(char[] chars) {
        if(chars.length == 0) return 0; 

        int mark = 0;       
        int write
= 0; for(int i = 0; i < chars.length; i++){ if(i + 1 == chars.length || chars[i] != chars[i+1]){ chars[write] = chars[mark]; write++; if(mark < i){ for(char a : (""+(i-mark+1)).toCharArray()) chars[write
++] = a; } mark = i + 1; } } return write; } }

相關推薦

leetcode 443. String Compression字串壓縮

問題描述:   Given an array of characters, compress it in-place.   The length after compression must always be smaller than or equal t

443. String Compressionpython+cpp

題目: Given an array of characters, compress it in-place. The length after compression must always be

LeetCode 557. Reverse Words in a String III 字串翻轉

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initia

LeetCode 541. Reverse String II 字串翻轉

Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string

LeetCode演算法題-String CompressionJava實現

這是悅樂書的第230次更新,第242篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第97題(順位題號是443)。給定一組字元,就地壓縮它。壓縮後的長度必須始終小於或等於原始陣列。陣列的每個元素都應該是長度為1的字元(不是int)。在就地修改輸入陣列後,返回陣列的新長度。例

[LeetCode] 415. Add Strings字串 加法

題:https://leetcode.com/problems/add-strings/description/ 題目 Given two non-negative integers num1 and num2 represented as string, return the su

LeetCode 415. Add Strings字串相加python3版本

程式碼轉載自:點選這裡可以檢視 我給加了很詳細的註解,這樣看起來就方便多了,尤其是像我一樣的新手 class Solution(object): def addStrings(self, num1, num2): """

Leetcode 443 String Compression

int code elif 用兩個 字符 etc self 兩個 left 基礎的字符串操作,用兩個指針掃一遍就行. class Solution(object): def compress(self,chars): """

C++實現string.replace字串替換

#include "stdafx.h" #include <iostream> #include <string

asp.net core 系列之Performance的 Response compression響應壓縮

本文,幫助瞭解響應壓縮的一些知識及用法(大部分翻譯於官網,英文水平有限,不準確之處,歡迎指正)。 什麼是響應壓縮?響應壓縮簡單的說就是為了減少網路頻寬,而把返回的響應壓縮,使之體積縮小,從而加快響應的一種技術(個人理解) 網路頻寬是有限的資源。減少響應(response)的大小通常可以增加應用的響應性(即

443. String Compression 字符串壓縮

you 2.6 bottom none var lac ++ enum sin Given an array of characters, compress it in-place.The length after compression must always be sm

443 String Compression 壓縮字符串

str 壓縮 amp r++ compress 所有 .com etc char 給定一組字符,使用原地算法將其壓縮。壓縮後的長度必須始終小於或等於原數組長度。數組的每個元素應該是長度為1 的字符(不是 int 整數類型)。在完成原地修改輸入數組後,返回數組的新長度。進階:

E - String of CCPC 字串處理str.substr()的運用)

滴答滴答---題目連結  BaoBao has just found a string  of length  consisting of 'C' and 'P' in his pocket. As a big fan of the China

每日一題--LeetCode 415 字串相加java

題目描述: 解題思路:直接將兩個字串每一位提出來,相加,並且判斷是否有進位 insert方法和append方法的區別:insert可以在指定位置插入,append只能在最後位置插入 程式碼如下: class Solution { public String addStr

PAT乙級——1078字串壓縮和解壓 判斷邊界Java實現

題目:字串壓縮與解壓 (20 分) 文字壓縮有很多種方法,這裡我們只考慮最簡單的一種:把由相同字元組成的一個連續的片段用這個字元和片段中含有這個字元的個數來表示。例如 ccccc 就用 5c 來表示。如果字元沒有重複,就原樣輸出。例如 aba 壓縮後仍然是 aba。 解壓方法就是

ZOJ-3985 String of CCPC字串處理

傳送門: 題意: 給你一個字串,其中只有'C',‘P’兩種字元。現在你可以從商店中購買‘C’,'P'兩種字元,但需要支付費用,第一次購買需要支付0,第二次支付1,第n次支付n-1。現在你可以通過組成CCPC字元來賺取1,每組成一個CCPC字元賺1。其中CCPCCPC這

LeetCode初級演算法問題字串

反轉字串class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str """ retur

牛客多校第三場 E Sort String字串hash

題目描述 Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and won

LeetCode 223 Rectangle Area矩形面積

lee return common 分享 span spa class tracking mod 翻譯 找到在二維平面中兩個相交矩形的總面積。 每一個矩形都定義了其左下角和右上角的坐標。 (矩形例如以下圖) 如果,總占地面積永遠不會超過int

LeetCode 54. Spiral Matrix螺旋矩陣

gin 每一個 owin code -1 new 完成 length tco Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral