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

  • Người tạo chủ đề Người tạo chủ đề Vipluckystar
  • Ngày bắt đầu Ngày bắt đầu
hóng ngày trưởng môn phát cạc :adore:

via theNEXTvoz for iPhone
Giờ có 3 mốc chắc chắn là sẽ phát cạc.
1 là thẻ xanh, mấy năm nữa có:canny:
2 là lên 2k5, dạo này đang bận học với làm AI quá nên chưa có thời gian try hard thêm nên cái này quá xa.
3 là phá đảo 3k888 bài của Leetcode :ah:
Trong khi đó thì vẫn cứ phải chờ cạc của anh em sống qua ngày vậy, mà anh em có offer xong im ắng quá
zFNuZTA.gif


via theNEXTvoz for iPhone
 
Python:
class Solution:
    def decodeCiphertext(self, encodedText: str, rows: int) -> str:
        m = rows
        n = len(encodedText)//rows
        grid = [["" for _ in range(n)] for _ in range(m)]
        curr = 0
        for i in range(m):
            for j in range(n):
                grid[i][j] = encodedText[curr]
                curr += 1

        currentCol = 0
        ans = []
        while currentCol < n:
            col = currentCol
            for i in range(m):
                ans.append(grid[i][col])
                col += 1
                if col >= n:
                    break

            currentCol += 1

        return ("".join(ans)).rstrip()
 
C#:
using System.Text;

public class Solution
{
    public string DecodeCiphertext(string encodedText, int rows)
    {
        int size = encodedText.Length;
        int n = size / rows;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++)
            for(int j = 0; j < rows && i + j < n; j++)
                sb.Append(encodedText[n * j + i + j]);
        return sb.ToString().TrimEnd();
    }
}
 
ko doc doan xoa trailing space, 1 bug
0FFPAjM.png

JavaScript:
function decodeCiphertext(encodedText: string, m: number): string {
    const l = encodedText.length;
    const n = l / m;
    const res: string[] = [];

    for (let j = 0; j < n; j++) {
        let i = 0, k = j;

        while (i < m && k < n) {
            const cur = i * n + k;
            res.push(encodedText[cur]);
            i++, k++
        }
    }
    return res.join('').trimEnd();
};
 
ko doc doan xoa trailing space, 1 bug
0FFPAjM.png

JavaScript:
function decodeCiphertext(encodedText: string, m: number): string {
    const l = encodedText.length;
    const n = l / m;
    const res: string[] = [];

    for (let j = 0; j < n; j++) {
        let i = 0, k = j;

        while (i < m && k < n) {
            const cur = i * n + k;
            res.push(encodedText[cur]);
            i++, k++
        }
    }
    return res.join('').trimEnd();
};
non
fzjdayy.gif
1775286054700.webp

e sinh vin năm 3 nên đọc đề cẩn thận lắm
mOOOycL.gif
 
C#:
public class Solution {
    public bool JudgeCircle(string moves) {
        int x=0;
        int y=0;
        foreach(var i in moves)
            if(i=='U') y++;
            else if (i=='D') y--;
            else if (i=='L') x--;
            else if (i=='R') x++;
        return (x==0&&y==0);
    }
}
 
JavaScript:
function judgeCircle(moves: string): boolean {
    let i = 0, j = 0
    for (const m of moves) {
        if (m === 'U') i--;
        else if (m === 'D') i++
        else if (m === 'R') j++
        else j--
    }
    return i === 0 && j === 0;
};
 
Mã:
class Solution {
    public boolean judgeCircle(String s) {
        int x = 0, y = 0;

        for (int i = 0; i < s.length(); i++) {
            char val = s.charAt(i);
            if (val == 'L') x--;
            else if (val == 'R') x++;
            else if (val == 'U') y++;
            else y--;
        }

        return x == 0 && y == 0;
    }
}
 
bài làm lâu rồi
JavaScript:
function robotSim(commands: number[], obstacles: number[][]): number {
    let res = 0;
    let x = 0, y = 0;
    const dirs = [[0,1], [1, 0], [0, -1], [-1, 0]];
    let i = 0;
    const set = new Set<string>()
    for (const [a,b] of obstacles) set.add(`${a},${b}`)
    for (const c of commands) {
        if (c === -1) i = (i + 1) % 4;
        else if (c === -2) i = (i + 3) % 4;
        else {
            const [dx, dy] = dirs[i];
            for (let i = 0; i < c; i++) {
                if (set.has(`${x + dx},${y + dy}`)) break;
                x = x + dx, y = y + dy
            }
        }
        res = Math.max(res, x * x + y * y)
    }

    return res;
};
 
C#:
public class Solution
{
    public int RobotSim(int[] commands, int[][] obstacles)
    {
        HashSet<(int, int)> m = new();
        foreach(var o in obstacles)
            m.Add((o[0], o[1]));
        int rtn = 0;
        int[,] dir = new int[,] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
        int cur_dir = 0;
        int x = 0;
        int y = 0;
        foreach(var i in commands)
        {
            if(i==-1)
                cur_dir = (cur_dir + 1) % 4;
            if (i == -2)
                cur_dir = (cur_dir - 1 + 4) % 4;
            for(int j = 1; j <= i; j++)
            {
                int nx = dir[cur_dir, 0] + x;
                int ny = dir[cur_dir, 1] + y;
                if (m.Contains((nx, ny)))
                    break;
                x=nx; y=ny;
            }
            rtn = Math.Max(x * x + y * y,rtn);
        }
        return rtn;
    }
}
Bác pentagon bị KIA r, thảo nào lâu r ko thấy
ME1tJB0.png
 
Sửa lần cuối:
JavaScript:
class Robot {
    w: number;
    h: number;
    x: number;
    y: number;
    idx: number;
    dirs: string[] = ["East", "North", "West", "South"];

    constructor(width: number, height: number) {
        this.w = width;
        this.h = height;
        this.x = 0;
        this.y = 0;
        this.idx = 0;
    }

    step(num: number): void {
        const m = 2 * (this.w + this.h - 2);
        num %= m;
        if (num === 0 && this.x === 0 && this.y === 0) {
            this.idx = 3;
        }

        while (num > 0) {
            if (this.idx === 0) {
                let move = Math.min(num, (this.w - 1) - this.x);
                this.x += move;
                num -= move;
            } else if (this.idx === 1) {
                let move = Math.min(num, (this.h - 1) - this.y);
                this.y += move;
                num -= move;
            } else if (this.idx === 2) {
                let move = Math.min(num, this.x);
                this.x -= move;
                num -= move;
            } else if (this.idx === 3) {
                let move = Math.min(num, this.y);
                this.y -= move;
                num -= move;
            }

            if (num > 0) {
                this.idx = (this.idx + 1) % 4;
            }
        }
    }

    getPos(): number[] {
        return [this.x, this.y];
    }

    getDir(): string {
        return this.dirs[this.idx];
    }
}
Edit: bài này có cách dùng O(1) cơ bản thế mà đi traverse, chịu rồi...
 
JavaScript:
class Robot {
    w: number;
    h: number;
    x: number;
    y: number;
    idx: number;
    dirs: string[] = ["East", "North", "West", "South"];

    constructor(width: number, height: number) {
        this.w = width;
        this.h = height;
        this.x = 0;
        this.y = 0;
        this.idx = 0;
    }

    step(num: number): void {
        const m = 2 * (this.w + this.h - 2);
        num %= m;
        if (num === 0 && this.x === 0 && this.y === 0) {
            this.idx = 3;
        }

        while (num > 0) {
            if (this.idx === 0) {
                let move = Math.min(num, (this.w - 1) - this.x);
                this.x += move;
                num -= move;
            } else if (this.idx === 1) {
                let move = Math.min(num, (this.h - 1) - this.y);
                this.y += move;
                num -= move;
            } else if (this.idx === 2) {
                let move = Math.min(num, this.x);
                this.x -= move;
                num -= move;
            } else if (this.idx === 3) {
                let move = Math.min(num, this.y);
                this.y -= move;
                num -= move;
            }

            if (num > 0) {
                this.idx = (this.idx + 1) % 4;
            }
        }
    }

    getPos(): number[] {
        return [this.x, this.y];
    }

    getDir(): string {
        return this.dirs[this.idx];
    }
}
Edit: bài này có cách dùng O(1) cơ bản thế mà đi traverse, chịu rồi...
Adn issue
zFNuZTA.gif


via theNEXTvoz for iPhone
 
Java:
class Robot {
    int[][] grid;
    int[] pos = {0, 0};
    String[] directs = {"East", "North", "West", "South"};
    int dir;
    int C;
    public Robot(int width, int height) {
        grid = new int[height][width];
        dir = 0;
        C = 2 * (width + height) - 4;//Chu vi
    }
  
    public void step(int num) {
        if (C == 0) return;
        num %= C;
        if (num == 0) {
            if (pos[0] == 0 && pos[1] == 0) dir = 3;
            return;
        }
        while (num > 0) {
            switch(dir) {
                case 0: {
                    int move = Math.min(num, grid[0].length - 1 - pos[0]);
                    pos[0] += move;
                    num -= move;
                    if (num > 0) dir = 1;
                    break;
                }
                case 1: {
                    int move = Math.min(num, grid.length - 1 - pos[1]);
                    pos[1] += move;
                    num -= move;
                    if (num > 0) dir = 2;
                    break;
                }
                case 2: {
                    int move = Math.min(num, pos[0]);
                    pos[0] -= move;
                    num -= move;
                    if (num > 0) dir = 3;
                    break;
                }
                case 3: {
                    int move = Math.min(num, pos[1]);
                    pos[1] -= move;
                    num -= move;
                    if (num > 0) dir = 0;
                    break;
                }
            }
        }
    }
  
    public int[] getPos() {
        return pos;
    }
  
    public String getDir() {
        return directs[dir];
    }
}

/**
 * Your Robot object will be instantiated and called as such:
 * Robot obj = new Robot(width, height);
 * obj.step(num);
 * int[] param_2 = obj.getPos();
 * String param_3 = obj.getDir();
 */
Trốn hơi lâu
IKHGHNs.jpeg
 
Python:
class Robot:

    def __init__(self, width: int, height: int):
        self.dirs = ["East", "North", "West", "South"]
        self.dir = 0
        self.pos = [0, 0]
        self.width, self.height = width, height
        self.size = width * 2 + height * 2 - 4

    def step(self, num: int) -> None:
        loop = num % (self.size)
        if loop == 0:
            if self.pos == [0, 0]:
                self.dir = 3
                return
        for _ in range(loop):
            if self.dir == 0:
                if self.pos[0] + 1 < self.width:
                    self.pos[0] += 1
                else:
                    self.pos[1] += 1
                    self.dir = (self.dir + 1) % 4
            elif self.dir == 1:
                if self.pos[1] + 1 < self.height:
                    self.pos[1] += 1
                else:
                    self.pos[0] -= 1
                    self.dir = (self.dir + 1) % 4
            elif self.dir == 2:
                if self.pos[0] - 1 >= 0:
                    self.pos[0] -= 1
                else:
                    self.pos[1] -= 1
                    self.dir = (self.dir + 1) % 4
            else:
                if self.pos[1] - 1 >= 0:
                    self.pos[1] -= 1
                else:
                    self.pos[0] += 1
                    self.dir = (self.dir + 1) % 4

    def getPos(self) -> List[int]:
        return self.pos

    def getDir(self) -> str:
        return self.dirs[self.dir]
 

Thống kê chủ đề

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