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.
LC 1652 Java 2line
Java:
class Solution {
    public int[] decrypt(int[] code, int k) {
        return k == 0 ? new int[code.length] : IntStream.range(0, code.length).map(i -> IntStream.rangeClosed(
            1, Math.abs(k)).map(j -> code[(i + code.length + (k > 0 ? j : -j)) % code.length]).sum()).toArray();
    }
}
 
Java:
public static int[] decrypt(int[] code, int k) {
    int[] res = new int[code.length];
    int length = code.length;
    if (k == 0) {
        Arrays.fill(code, 0);
        return code;
    } else if (k > 0) {
        int sum = 0;
        for (int i = 1; i <= k; i++) {
            sum += code[i];
        }
        res[0] = sum;
        for (int i = 2; i <= length; i++) {
            sum -= code[i - 1];
            if (i + k - 1 >= length) {
                sum += code[(i + k - 1) % length];
            } else sum += code[i + k - 1];
            res[i - 1] = sum;
        }
    } else {
        int sum = 0;
        for (int i = length - 2; i >= length + k - 1; i--) {
            sum += code[i];
        }
        res[length - 1] = sum;
        for (int i = length - 3; i >= -1; i--) {
            sum -= code[i + 1];
            if (i + k + 1 < 0) {
                sum += code[length + (i + k + 1)];
            } else sum += code[i + k + 1];
            res[i + 1] = sum;
        }
    }
    return res;
}
 
Python:
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        n = len(code)
        if k == 0:
            return [0] * n
        sign = True if k > 0 else False
        k = abs(k)
        if not sign:
            code = code[::-1]
        code = code + code[:k]
        cur = sum(code[1:1+k])
        res = []
        res.append(cur)
        l = 1
        for r in range(k+1, len(code)):
            cur += code[r]
            cur -= code[l]
            res.append(cur)
            l += 1
        if not sign:
            return res[::-1]
        return res
 
Sửa lần cuối:
Python:
class Solution:
    def decrypt(self, arr: List[int], k: int) -> List[int]:
        if k == 0:
            return [0] * len(arr)
        
        n = len(arr)
        arr = arr + arr

        res = []
        curr = 0

        if k > 0:
            for i in range(n + k):
                curr += arr[i]
                if i >= k:
                    curr -= arr[i - k]
                    res.append(curr)
        if k < 0:
            k = -k
            for i in range(n-k-1, 2*n-1):
                curr += arr[i]
                if i >= n-1:
                    curr -= arr[i - k]
                    res.append(curr)
    
        return res
 
Python:
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        n = len(code)
        if k == 0:
            return [0] * n
        if k > 0:
            l, r = 1, 1
        else:
            l, r = n - abs(k), n - abs(k)

        curr = 0
        res = []

        while len(res) < n:
            curr += code[r % n]
            if r - l + 1 == abs(k):
                res.append(curr)
                curr -= code[l % n]
                l += 1

            r += 1

        return res
 
C#:
public class Solution
{
    public int[] Decrypt(int[] code, int k)
    {
        var len = code.Length;
        var res = new int[len];
        if (k > 0)
        {
            var sum = 0;
            for (var i = 1; i <= k; i++)
            {
                sum += code[i];
            }

            for (int i = 0; i < len; i++)
            {
                res[i] = sum;
                sum += code[Normalize(i+k+1)] - code[Normalize(i+1)];
            }
        }
        else if (k < 0)
        {
            var sum = 0;
            for (var i = len - 1; i >= len + k; i--)
            {
                sum += code[i];
            }
            for (int i = 0; i < len; i++)
            {
                res[i] = sum;
                sum += code[Normalize(i)] - code[Normalize(i+k)];
            }
        }

        return res;

        int Normalize(int x)
        {
            while (x < 0)
            {
                x += len;
            }

            return x % len;
        }
    }
}
 
1731906467702.png

Python:
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        res = []

        n = len(code)
        code = code + code
        for i in range(n):
            if k < 0:
                res.append(sum(code[i + n + k:i + n + k + abs(k)]))
            else:
                res.append(sum(code[i + 1:(i + k + 1)]))
        return res
do k < n nên t nhân đôi mảng code lên cho dễ xử lí trường hợp mảng cộng quay lại đầu
 
C++:
func decrypt(code []int, k int) []int {
    if k == 0 {
        return make([]int, len(code))
    }

    arr := make([]int, 0)
    n := len(code)

    for i := 0; i < n; i++ {
        sum := 0
        if k > 0 {
            for j := 1; j <= k; j++ {
                if i+j >= n {
                    sum += code[i+j-n]
                } else {
                    sum += code[i+j]
                }
            }

        } else {
            for j := 1; j <= -k; j++ {
                if i-j < 0 {
                    num := math.Abs(float64(i - j))
                    sum += code[n-int(num)]
                } else {
                    sum += code[i-j]
                }
            }
        }

        arr = append(arr, sum)
    }

    return arr
}
 
đi phỏng vấn làm ngon lành mà ngta tuyển app tôi lại nhớ sao đó thành framework, câu cuối hỏi m ưng làm app hay framework, tôi nói t làm cả 2 đều được nhưng chọn 1 cái thì framework đi (vì nhớ nhầm)
Xong hôm nay bị reject với lí do chọn frw :shame::shame::shame:

via theNEXTvoz for iPhone
HR mới liên lạc nói mình confirm lại là muốn làm app hay làm frw, lần này nói làm app thẳng luôn. Kết quả là có return offer :feel_good::feel_good::feel_good:
Mình có hỏi tại sao lại từ chối rồi mà còn call tiếp thì bên kia nói do làm đc hết bài hackerrank nên HM muốn kéo mình về :sexy_girl::sexy_girl::sexy_girl:
Bái bai ae tôi đi đây

via theNEXTvoz for iPhone
 
HR mới liên lạc nói mình confirm lại là muốn làm app hay làm frw, lần này nói làm app thẳng luôn. Kết quả là có return offer :feel_good::feel_good::feel_good:
Mình có hỏi tại sao lại từ chối rồi mà còn call tiếp thì bên kia nói do làm đc hết bài hackerrank nên HM muốn kéo mình về :sexy_girl::sexy_girl::sexy_girl:
Bái bai ae tôi đi đây

via theNEXTvoz for iPhone
can à bác
g3ofLLW.png
phát cạt thôi
LEY1HPK.gif
uoc j cũng dc hr liên hệ lại như này
HssWxYn.png
 
Java:
class Solution {
    public int[] decrypt(int[] code, int k) {
        int n = code.length;
        int[] res = new int[n];

        if (k == 0) {
            return res;
        }

        int sum = 0;
        int start = k > 0 ? 1 : n + k;
        int end = k > 0 ? k : n - 1;

        for (int i = start; i <= end; i++) {
            sum += code[i % n];
        }

        for (int i = 0; i < n; i++) {
            res[i] = sum;

            sum -= code[(start++ % n)];
            sum += code[(++end % n)];
        }

        return res;
    }
}
 
Java:
class Solution {
    public int[] decrypt(int[] code, int k) {
        int n = code.length;
        int[] res  = new int[n];
        if( k==0 )
            return res;
        int l=0,r=0;
        if(k<0){
            l=n+k;
            r = n-1;
        }else{
            l=1;
            r = k;
        }
        for(int i = l;i<=r;i++)
            res[0]+=code[i];
        for(int i = 1;i<n;i++){
            res[i]= res[i-1] + code[(r+i)%n] - code[(l+i-1)%n];
        }
        return res;
}
}
 
HR mới liên lạc nói mình confirm lại là muốn làm app hay làm frw, lần này nói làm app thẳng luôn. Kết quả là có return offer :feel_good::feel_good::feel_good:
Mình có hỏi tại sao lại từ chối rồi mà còn call tiếp thì bên kia nói do làm đc hết bài hackerrank nên HM muốn kéo mình về :sexy_girl::sexy_girl::sexy_girl:
Bái bai ae tôi đi đây

via theNEXTvoz for iPhone
ko cần đi đâu hết, đợi Đảng đưa đất nước tiến lên XHCN là ở VN sướng nhất luôn. :ah:
 
PHP:
class Solution {

    /**
     * @param Integer[] $code
     * @param Integer $k
     * @return Integer[]
     */
    function decrypt($code, $k) {
        $length = count($code);
        $ans = array_fill(0, $length, 0);
        if ($k == 0) return $ans;

        for ($i=0; $i<$length; $i++) {
            $c = $k;
            while (($k > 0 && $c > 0) || ($k < 0 && $c < 0)) {
                $index = $c + $i;
                if ($k > 0) {
                    $ans[$i] += $index < $length ? $code[$index] : $code[abs($length - $index)];
                    $c--;
                    continue;
                }
                
                $ans[$i] += $index < 0 ? $code[$length + $index] : $code[$index];
                $c++;
            }
        }

        return $ans;
    }
}
 
C#:
public class Solution {
    public int[] Decrypt(int[] code, int k) {
        int[] result = new int[code.Length];
        int temp = 0;
        for(int i = 0; i < code.Length; i++)
        {
            for(int j = 1; j <= Math.Abs(k); j++)
            {
                temp = k > 0 ? j : -j;
                result[i] += code[(i+temp+code.Length) % code.Length];
            }
        }
        return result;
    }
}
 
HR mới liên lạc nói mình confirm lại là muốn làm app hay làm frw, lần này nói làm app thẳng luôn. Kết quả là có return offer :feel_good::feel_good::feel_good:
Mình có hỏi tại sao lại từ chối rồi mà còn call tiếp thì bên kia nói do làm đc hết bài hackerrank nên HM muốn kéo mình về :sexy_girl::sexy_girl::sexy_girl:
Bái bai ae tôi đi đây

via theNEXTvoz for iPhone
Mấy thằng Can lỏ cùi bắp có tí Hacker rank cũng ko làm đc
zFNuZTA.gif
chúc mừng fen phát cạc thôi.
Lướt xuống thì thấy Hàn, có owp Can ko đi Can đi Hàn làm gì
BdgiW7R.gif


via theNEXTvoz for iPhone
 
Sửa lần cuối:
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.215.888
Quay lại
Lên đầu trang