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.
Java:
class Solution {
    class Piece {
        public Character c;
        public Integer i;
        public Piece(Character c, Integer i) {
            this.c = c;
            this.i = i;
        }
    }

    public boolean canChange(String start, String target) {
        int n = target.length();
        Queue<Piece> sQueue = new ArrayDeque();
        Queue<Piece> tQueue = new ArrayDeque();
        for (int i = 0; i < n; i++) {
            if (start.charAt(i) != '_') {
                sQueue.offer(new Piece(start.charAt(i), i));
            }
            if (target.charAt(i) != '_') {
                tQueue.offer(new Piece(target.charAt(i), i));
            }
        }
        if (tQueue.size() != sQueue.size()) return false;
        while (!sQueue.isEmpty()) {
            Piece s = sQueue.poll();
            Piece t = tQueue.poll();
            if (s.c != t.c) return false;
            if (s.c == 'L' && s.i < t.i) return false;
            if (s.c == 'R' && s.i > t.i) return false;
        }
        return true;
    }

}
Đặt tên hơi ngoo nhưng thôi kệ :baffle:
 
Python:
class Solution:
    def canChange(self, start: str, target: str) -> bool:
        n = len(start)
        count_L_start = count_R_start = count_L_target = count_R_target = 0
        for i in range(n):
            if start[i] == 'L':
                count_L_start += 1
            elif start[i] == 'R':
                count_R_start += 1
            if target[i] == 'L':
                count_L_target += 1
            elif target[i] == 'R':
                count_R_target += 1
        if count_L_start != count_L_target or count_R_start != count_R_target:
            return False
        i = 0 #start
        j = 0 #target
        while i < n and j < n:
            if start[i] == '_':
                i += 1
                continue
            if target[j] == '_':
                j += 1
                continue
            if start[i] == target[j]:
                if start[i] == 'L' and i < j:
                    return False
                elif start[i] == 'R' and i > j:
                    return False
                else:
                    i += 1
                    j += 1
            else:
                return False
        return True
 
Java:
class Solution {
    class Piece {
        public Character c;
        public Integer i;
        public Piece(Character c, Integer i) {
            this.c = c;
            this.i = i;
        }
    }

    public boolean canChange(String start, String target) {
        int n = target.length();
        Queue<Piece> sQueue = new ArrayDeque();
        Queue<Piece> tQueue = new ArrayDeque();
        for (int i = 0; i < n; i++) {
            if (start.charAt(i) != '_') {
                sQueue.offer(new Piece(start.charAt(i), i));
            }
            if (target.charAt(i) != '_') {
                tQueue.offer(new Piece(target.charAt(i), i));
            }
        }
        if (tQueue.size() != sQueue.size()) return false;
        while (!sQueue.isEmpty()) {
            Piece s = sQueue.poll();
            Piece t = tQueue.poll();
            if (s.c != t.c) return false;
            if (s.c == 'L' && s.i < t.i) return false;
            if (s.c == 'R' && s.i > t.i) return false;
        }
        return true;
    }

}
Đặt tên hơi ngoo nhưng thôi kệ :baffle:
vãi lọ chơi cả Q luôn :eek:
 
Python:
class Solution:
    def canChange(self, start: str, target: str) -> bool:
        n = len(start)
        X = [i for i in range(n) if start[i] != '_']
        Y = [i for i in range(n) if target[i] != '_']

        for x, y in zip_longest(X, Y, fillvalue=None):
            if x == None or y == None \
            or start[x] != target[y] \
            or start[x] == 'L' and x < y \
            or start[x] == 'R' and x > y:
                return False
        
        return True
 
C++:
class Solution {
public:
    bool canChange(string s, string t) {
        int i = 0, j = 0;
        while (i < s.size() || j < t.size()) {
            while (i < s.size() && s[i] == '_') i++;
            while (j < t.size() && t[j] == '_') j++;
            if (s[i] != t[j]) return false;
            if (s[i] == 'L' && i < j) return false;
            if (s[i] == 'R' && i > j) return false;
            i++, j++;
        }
        return true;
    }
};
 
Java:
class Solution {
    public boolean canChange(String start, String target) {
        int n = target.length();
        int stack1= 0;
        int stack2=0;
        for(int i=0;i<n;i++){
            char s = start.charAt(i);
            char t = target.charAt(i);
            if(s=='R' ) {
                if(stack2>0) return false;
                stack1++;
            }
            if(t=='L') {
                if(stack1>0) return false;
                stack2++;
            }
            if(t=='R') {
                if(--stack1<0) return false;
              
            }
            if(s=='L') {
                if(--stack2<0) return false;
            }
        }
      
        return stack1+stack2==0;
    }
}
 
Swift:
class Solution {
    func canChange(_ start: String, _ target: String) -> Bool {
    var wL = 0, wR = 0
    for (cs, ct) in zip(start, target) {
        wL += ct == "L" ? 1 : 0
        wR += cs == "R" ? 1 : 0
        if cs == "L" { if wR > 0 || wL <= 0 { return false }; wL -= 1 }
        if ct == "R" { if wL > 0 || wR <= 0 { return false }; wR -= 1 }
    }
    return wL == 0 && wR == 0
    }
}

Sao thằng Swift chạy nhìn chậm thế nhỉ :beat_brick:
 
C++:
class Solution {
public:
    bool canChange(string start, string target) {
        int i = 0;
        int j = 0;
        int n = start.size();
        while(true){
            while(i != n && start[i] == '_')i++;
            while(j != n && target[j] == '_')j++;
            if(i == n && j == n) return true;
            if(i != j && ( i == n  || j == n)) return false;
            if(start[i] != target[j]){
                return false;
            }
            if(start[i] == target[j]){
                if(start[i] == 'L'){
                    if(i < j) return false;
                }
                if(target[j] == 'R'){
                    if(i > j) return false;
                }
                i++;
                j++;
            }
        }
        return true;
    }
};
 
JavaScript:
var canChange = function(start, target) {
    let startPt = 0, targetPt = 0;
    const n = start.length;
    while(startPt < n || targetPt < n){
        while(start[startPt] === "_") startPt++;
        while(target[targetPt] === "_") targetPt++;
        if(start[startPt] !== target[targetPt]) return false;
        if(start[startPt] === "L" && startPt < targetPt) return false;
        if(start[startPt] === "R" && startPt > targetPt) return false;
        startPt++;
        targetPt++;
    }
    return true;
};
 
e mà implement ko chậm vào bào rating contest r :canny: khổ cái giờ q3 cũng có khả năng giải 50/50 mà cài chậm quá
Bạn sợ à
zFNuZTA.gif



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.216.286
Quay lại
Lên đầu trang