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.
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
à chỉ là mỗi làm algo thì ko xài, e tưởng bác bảo h ko còn cty nào xài dead lang mới sợ
CwKLdbR.png
, e làm OA vẫn quất java bth. nam tính cộc cằn xôi thịt mạnh mẽ
Vim7wci.png
 
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
mấy chỗ khác ko biết chứ top 10 weekly contest leetcode kiểu j cũng có 1 ông java
BdgiW7R.png
 
C-like:
impl Solution {
    pub fn rotate_string(s: String, goal: String) -> bool {
        if s.len() != goal.len() {
            return false;
        }

        format!("{goal}{goal}").contains(&s)
    }
}

C-like:
impl Solution {
    pub fn compressed_string(word: String) -> String {
        let (word_bytes, n) = (word.as_bytes(), word.len());
        let (mut bytes, mut i) = (vec![], 0);

        while i < n {
            let (bc, mut count) = (word_bytes[i], 0);

            while i < n && count < 9 && word_bytes[i] == bc {
                (count, i) = (count + 1, i + 1);
            }

            bytes.push(b'0' + count);
            bytes.push(bc);
        }

        unsafe { String::from_utf8_unchecked(bytes) }
    }
}

dog_ningen.jpg
 
lướt cmt thấy chê C#, tủi thân ghê :too_sad:
ơ mà bài này ra hồi tháng 5 rồi, thảo nào thấy quen quen :byebye:
C#:
public class Solution {
    public string CompressedString(string word) {
        var result = new StringBuilder();
        int count = 1;
        char temp = word[0];;
        for(int i = 1; i<word.Length; i++)
        {
            if(count == 9 || temp != word[i])
            {
                result.Append(count);
                result.Append(temp);
                temp = word[i];
                count = 0;
            }
            count++;
        }
        result.Append(count);
        result.Append(temp);
        return result.ToString();
    }
}
 
Java:
class Solution {
    public static final char INIT_CHAR = ' ';
    public String compressedString(String word) {
        StringBuilder compressed = new StringBuilder();
        char prev = INIT_CHAR;
        int counter = 1;
        for (char c : word.toCharArray()) {
            if (c != prev) {
                if (prev != INIT_CHAR) {
                    compressed.append(counter);
                    compressed.append(prev);
                }
                counter = 1;
            } else {
                counter++;
            }

            if (counter == 10) {
                compressed.append('9');
                compressed.append(prev);
                counter = 1;
            }

            prev = c;
        }

        compressed.append(counter);
        compressed.append(prev);

        return compressed.toString();
    }
}
Nay Medium giả cầy rồi :burn_joss_stick: :burn_joss_stick: :burn_joss_stick:
 
C++:
int Solution::minChanges(string s) {
    int count = 0;
    for (size_t i = 0; i < s.length() - 1; i = i + 2) {
        if (s[i] != s[i+1]) {
            count++;
        }
    }
    return count;
}
 
Python:
class Solution:
    def minChanges(self, s: str) -> int:
        changes = 0
        for i in range(0, len(s), 2):
            if s[i] != s[i + 1]:
                changes += 1
        return changes
 
Lâu lắm mới có thời gian rỗi buổi sáng để làm 1 cốc cafe, gõ một bài leetcode
C++:
int minChanges(string s) {
    int ret = 0;
    int n = s.size();
    for(int i = 0; i < n; i+=2) {
        if (s[i] != s[i+1]) ret++;
    }
    return ret;
}
 
Python:
class Solution:
    def minChanges(self, s: str) -> int:
        return sum(c1 != c2 for c1, c2 in zip(s[::2], s[1::2]))
 
C++:
class Solution {
public:
    int minChanges(string s) {
        int ans = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i % 2 == 1) {
                if (s[i] != s[i - 1]) ans++;
            }
        }
        return ans;
    }
};
 
bài hôm nay lại troll à. Dãy chẵn ngắn nhất là 2 thì cứ check 2 thằng liên tục là đc :ops:
JavaScript:
function minChanges(s: string): number {
    let n = s.length, res = 0;
    for (let i = 1; i < n; i+=2) {
        if (s[i] !== s[i-1]) res++
    }
    return res;
};
 
Python:
class Solution(object):
    def minChanges(self, s):
        """
        :type s: str
        :rtype: int
        """
        i = 0
        changes = 0
        while i < len(s) - 1:
            if s[i] != s[i+1]:
                changes += 1
            i += 2   
        return changes
?, chạy bừa hóa ra lại được:ah:
 
JavaScript:
var minChanges = function (s) {
    const lens = Array.from(s.match(/(0+)|(1+)/g)).map(it => it.length);
    let hasOdd = false, ans = 0;
    for (const len of lens) {
        ans += hasOdd ? 1 : 0;
        if (len & 1) {
            hasOdd = !hasOdd;
        }
    }
    return ans;
};
 
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.214.224
Quay lại
Lên đầu trang