thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
code của bác bị chậm rồi, các đoạn dụng ans += là cộng string nó không như append của array nên độ phức tạp sẽ là O(len ans), e vừa check nhưng ko hiểu sao cách làm của e vẫn bị chậm hơn bọn nó:

Python:
class Solution:
    def compressedString(self, word: str) -> str:
        count = 1
        ans = []
        curr = word[0]

        for c in word[1:]:
            if c == curr:
                count += 1
                if count == 10:
                    ans.append(str("9"))
                    ans.append(c)
                    count = 1
            else:
                ans.append(str(count))
                ans.append(curr)
                curr = c
                count = 1
       
        ans.append(str(count))
        ans.append(curr)
       
        return "".join(ans)

Python:
class Solution:
    def compressedString(self, word: str) -> str:
        count = 1
        ans = ""

        for i in range(1, len(word)):
            if word[i] == word[i-1]:
                count += 1
                if count == 10:
                    ans += str("9")
                    ans += word[i]
                    count = 1
            else:
                ans += str(count)
                ans += word[i-1]
                count = 1
       
        return ans + str(count) + word[-1]
Ừ thường dùng array append vào rồi join nó nhanh hơn mà, do mình lười nên dùng luôn cộng cho nhanh

via theNEXTvoz for iPhone
 
Java:
class Solution {
    public String compressedString(String word) {
        StringBuilder sb = new StringBuilder();
        char pre = word.charAt(0);
        int count = 1;
        for(int i = 1;i<word.length();i++){
            if(word.charAt(i) == pre){
                count++;
                if(count == 9){
                    sb.append("9");
                    sb.append(pre);
                    count = 0;
                    if(i < word.length() - 1){
                        pre = word.charAt(i + 1);
                    }
                }
            }else{
                sb.append(count);
                sb.append(pre);
                count = 1;
                pre = word.charAt(i);
            }
        }
        if(count > 0){
            sb.append(count);
            sb.append(pre);
        }
        return sb.toString();
    }
}
 
1730703319862.png

PHP:
<?php
class StringComporession
{
    function compressedString($word)
    {
        $comp = '';
        $i = 0;
        $length = strlen($word);
        while ($i < $length) {
            $current = $word[$i];
            $counter = 0;
            do {
                $counter++;
                if ($i + 1 == $length) {
                    break;
                }
            } while ($current == $word[$i + $counter] && $counter < 9);
            $comp .= "$counter$word[$i]";
            $i += $counter;
        }
        return $comp;
    }
}
 
Bài này ez nhỉ, thế mà cũng dán nhãn Medium
JavaScript:
function compressedString(w: string): string {
    let prev = w[0], count = 1;
    let res = '';
    for (let i = 1; i < w.length; i++) {
        if (w[i] !== prev) res+= count + prev, count = 1, prev = w[i]
        else {
            count++;
            if (count > 9) res+= 9  + prev, count = 1
        }
    }
    res+= count + prev
    return res;
};
 
Python:
class Solution:
    def compressedString(self, word: str) -> str:
        comp = ''
        count = 0
        char = word[0]

        for i in range(len(word)):
            if word[i] == char and count != 9:
                count += 1
            else:
                comp = comp + str(count) + char
                char = word[i]
                count = 1
        comp = comp + str(count) + char
        return comp
như easy vậy
Python:
class Solution:
    def compressedString(self, word: str) -> str:
        if not word:
            return ""

        comp = []
        count = 0
        char = word[0]

        for i in range(len(word)):
            if word[i] == char and count != 9:
                count += 1
            else:
                comp.append(f"{count}{char}")
                char = word[i]
                count = 1
        comp.append(f"{count}{char}")
        
        return ''.join(comp)

1730706205904.png
 
Sửa lần cuối:
Java:
public String compressedString(String word) {
    StringBuilder comp = new StringBuilder();
    Stack<Character> stack = new Stack<>();

    for (char c : word.toCharArray()) {
        if (!stack.isEmpty() && c != stack.peek()) {
            comp.append(stack.size()).append(stack.peek());
            stack.clear();
        } else if (stack.size() == 9) {
            comp.append(9).append(c);
            stack.clear();
        }
        stack.push(c);
    }

    if (!stack.isEmpty()) {
        comp.append(stack.size()).append(stack.peek());
    }

    return comp.toString();
}
 
Mã:
func compressedString(word string) string {
    n := len(word)
    res := []byte{}
    i, j := 0, 0

    for i < n {
        c := word[i]
        for j = 1; j < 9; j++ {
            if i+j == n || c != word[i+j] {
                break
            }
        }
        res = append(res, byte(j)+'0')
        res = append(res, c)
        i += j
    }
    return string(res)
}
 
Đang tập viết C# =((
C#:
public class Solution
{
    public string CompressedString(string word)
    {
        List<string> list = new List<string>();
        var strBuilder = new StringBuilder();
        char prev = ' ';
        int repeatTimes = 1;
        foreach (char c in word)
        {
            if (c == prev)
            {
                repeatTimes++;
                if (repeatTimes > 9)
                {
                    strBuilder.Append((repeatTimes - 1).ToString());
                    strBuilder.Append(prev);
                    repeatTimes = 1;
                }
            }
            else if (prev != ' ')
            {
                strBuilder.Append((repeatTimes).ToString());
                strBuilder.Append(prev);
                repeatTimes = 1;
            }
            prev = c;
        }
        strBuilder.Append((repeatTimes).ToString());
        strBuilder.Append(prev);
        return strBuilder.ToString();
    }
}
 
Đang tập viết C# =((
C#:
public class Solution
{
    public string CompressedString(string word)
    {
        List<string> list = new List<string>();
        var strBuilder = new StringBuilder();
        char prev = ' ';
        int repeatTimes = 1;
        foreach (char c in word)
        {
            if (c == prev)
            {
                repeatTimes++;
                if (repeatTimes > 9)
                {
                    strBuilder.Append((repeatTimes - 1).ToString());
                    strBuilder.Append(prev);
                    repeatTimes = 1;
                }
            }
            else if (prev != ' ')
            {
                strBuilder.Append((repeatTimes).ToString());
                strBuilder.Append(prev);
                repeatTimes = 1;
            }
            prev = c;
        }
        strBuilder.Append((repeatTimes).ToString());
        strBuilder.Append(prev);
        return strBuilder.ToString();
    }
}
Làm LC thì đừng làm bằng C#, 1 nhà hiền triết nào đó đã từng nói :beauty:

via theNEXTvoz for iPhone
 
Đang tập viết C# =((
C#:
public class Solution
{
    public string CompressedString(string word)
    {
        List<string> list = new List<string>();
        var strBuilder = new StringBuilder();
        char prev = ' ';
        int repeatTimes = 1;
        foreach (char c in word)
        {
            if (c == prev)
            {
                repeatTimes++;
                if (repeatTimes > 9)
                {
                    strBuilder.Append((repeatTimes - 1).ToString());
                    strBuilder.Append(prev);
                    repeatTimes = 1;
                }
            }
            else if (prev != ' ')
            {
                strBuilder.Append((repeatTimes).ToString());
                strBuilder.Append(prev);
                repeatTimes = 1;
            }
            prev = c;
        }
        strBuilder.Append((repeatTimes).ToString());
        strBuilder.Append(prev);
        return strBuilder.ToString();
    }
}
Bỏ ngay cái thứ ngôn ngữ dead này
osCpCsi.gif


via theNEXTvoz for iPhone
 
sao e lướt sơ sơ thớt c# thấy nhiều đồ chơi lắm mà sao dead
bgsmpbs.png
Thay vì tập trung vô algorithm thì lúc giải sẽ ăn lỗi data type, thiếu library để giải nhanh, syntax phức tạp, gõ chậm, gõ kiểu đúng mệt mỏi, nói chung chỉ có chân ái C++ và Python.
Xưa mình hay complain Python nó chậm, lâu lâu ăn TLE nhưng đủ trình optimize code là ổn đủ để đánh đổi những thứ khác
uq1dgnk.gif


via theNEXTvoz for iPhone
 
Thay vì tập trung vô algorithm thì lúc giải sẽ ăn lỗi data type, thiếu library để giải nhanh, syntax phức tạp, gõ chậm, gõ kiểu đúng mệt mỏi, nói chung chỉ có chân ái C++ và Python.
Xưa mình hay complain Python nó chậm, lâu lâu ăn TLE nhưng đủ trình optimize code là ổn đủ để đánh đổi những thứ khác
uq1dgnk.gif


via theNEXTvoz for iPhone
nghe có vẻ giống java vậy ta
V092S5K.gif
 
nghe có vẻ giống java vậy ta
V092S5K.gif
Thì fence để ý thấy mấy thằng top trên có thằng nào xài Java C# đi giải algorithm đâu.
Mình cũng chỉ biết xài Python cơ bản thôi, chỉ sợ interview nó ra low level design bắt code thì bỏ mẹ. Mà thôi mấy cái đấy học sau :sweat: Mà xài Python nhiều đâm ra quên hết C# cmnr


via theNEXTvoz for iPhone
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.213.602
Quay lại
Lên đầu trang