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.
beat 100% cực ảo :waaaht:
C#:
public class Solution {
    public int[] GetMaximumXor(int[] nums, int maximumBit) {
        var result = new int[nums.Length];
        var maxK = (int)Math.Pow(2, maximumBit) - 1;
        result[nums.Length - 1] = nums[0];
        for (int i = 1; i < nums.Length; i++)
        {
            result[nums.Length - 1 - i] = result[nums.Length - i] ^ nums[i];
        }

        for (int i = 0; i < nums.Length; i++)
        {
            result[i] ^= maxK;
        }

        return result;
    }
}
 
Bài này AC hơi cao hư cấu nhỉ, nhưng mà làm quen bit manipulation cũng không khó lắm
Python:
class Solution:
    def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
        n = len(nums)
        xOr = 0
        for num in nums:
            xOr = xOr ^ num
    
        maxK = 2**maximumBit
        ans = []
        for i in range(n - 1, -1, -1):
            current = 0
            currentK = maxK
            for j in range(32, -1, -1):
                if xOr >> j & 1 == 0 and 1 << j < currentK:
                    currentK -= 1 << j
                    current |= 1 << j

            ans.append(current)
            xOr ^= nums[i]
        return ans

Python:
class Solution:
    def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
        n = len(nums)
        xOr = 0
        for num in nums:
            xOr = xOr ^ num
     
        maxK = (1 << maximumBit) - 1
        ans = [0]*n
        for i in range(n - 1, -1, -1):
            ans[n - i - 1] = maxK ^ xOr
            xOr ^= nums[i]
         
        return ans
=> Đã hiểu lí do, thế mà ko nghĩ ra cách đơn giản thế nghĩ hơi phức tạp
bài hnay AC cao là do ko có edge case đó bác, cứ submit auto pass
 
nhãn M ảo vl
PHP:
class Solution
{

    /**
     * @param Integer[] $nums
     * @param Integer $maximumBit
     * @return Integer[]
     */
    function getMaximumXor($nums, $maximumBit)
    {
        $max = (2 ** $maximumBit) - 1;
        $output = [];
        for ($i = 0; $i < count($nums); $i++) {
            $value = $max ^ $nums[$i];
            $output[] = $value;
            $max = $value;
        }
        return array_reverse($output);
    }
}
 
C++:
class Solution {
public:
    vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
        auto maxs = vector<int>(); maxs.reserve(nums.size());
        auto xorall = accumulate(nums.begin(), nums.end(), ((1 << maximumBit) - 1), [](int a, int b) { return a ^ b; });
        for_each(nums.rbegin(), nums.rend(), [&](int num) { maxs.emplace_back(xorall); xorall = xorall ^ num; });
        return maxs;
    }
};
 
Java:
class Solution {
    public int[] getMaximumXor(int[] nums, int maximumBit) {
        int max = (1 << maximumBit) - 1;
        int[] result = new int[nums.length];
        int[] xorlist = new int[nums.length];
        xorlist[0] = nums[0];
        for(int i = 1;i<nums.length;i++){
            xorlist[i] = xorlist[i - 1] ^ nums[i];
        }

        for(int i = xorlist.length - 1;i>=0;i--){
            result[xorlist.length - i  - 1] =  max ^ xorlist[i];
        }

        return result;
    }
}
 
Java:
class Solution {
    private boolean find(TreeNode n, int val, StringBuilder sb) {
        if (n.val == val)
            return true;
        if (n.left != null && find(n.left, val, sb))
            sb.append("L");
        else if (n.right != null && find(n.right, val, sb))
            sb.append("R");
        return sb.length() > 0;
    }

    public String getDirections(TreeNode root, int startValue, int destValue) {
        StringBuilder s = new StringBuilder(), d = new StringBuilder();
        find(root, startValue, s);
        find(root, destValue, d);
        int i = 0, max_i = Math.min(d.length(), s.length());
        while (i < max_i && s.charAt(s.length() - i - 1) == d.charAt(d.length() - i - 1))
            ++i;
        return "U".repeat(s.length() - i) + d.reverse().toString().substring(i);
    }
}
Lâu quá không chơi lụi nghề rồi :too_sad:
 
Python:
class Solution:
    def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
        xorAll = 0
        for num in nums:
            xorAll = xorAll ^ num
        n, mask = len(nums), (1 << maximumBit) - 1
        result = [0] * n
        for i in range(n):
            result[i] = xorAll ^ mask
            xorAll ^= nums[n - 1 - i]

        return result
 
mấy ngày ko vào thớt chơi, thím @Cố Trường Ca lại bay màu rồi à
6gCweAP.png
Cố bận chứ Cố không bay :sweat:
 
Python:
class Solution:
    def minEnd(self, n: int, x: int) -> int:
        firstElement = x
        n -= 1
        bits = 0
        for i in range(64):
            if firstElement >> i & 1 == 0:
                if n >> bits & 1 == 1:
                    firstElement |= 1 << i

                bits += 1

        return firstElement
 
Python:
class Solution:
    def minEnd(self, n: int, x: int) -> int:
        result, pos = x, 0
        n -= 1
        while n > 0:
            mask = n & 1
            while result & (1 << pos) > 0:
                pos += 1
            result = result ^ (mask << pos)
            pos += 1
            n >>= 1
        return result
 
JavaScript:
var minEnd = function (n, x) {
    const k = (n - 1).toString(2).split('');
    return Number.parseInt(
        x.toString(2)
            .padStart(52, '0')
            .split('')
            .reverse()
            .map(it => it === '1' ? it : k.pop() ?? '0')
            .reverse()
            .join(''),
        2
    );
};
 
Java:
class Solution {
    public long minEnd(int n, int x) {
        if (n == 1)
            return x;
        long res = x;
        int len = 1;
        long N = n-1;
        long X = x;
        while (N >> len > 0) {
            len++;
        }
    
        int i=0;
        for(int j=0 ;j<len;j++){
            while(((X>>i)&1)!=0){
                i++;     
            }
            res |= ((N >> j) & 1) << i;
            i++;
        }

        return res;
    }
}
debug do long với int lỗi cham kam vl
uwooUzw.gif
 
C++:
class Solution {
public:
    long long minEnd(int n, int x) {
        int max = n - 1;
        int i = 0;
        long long res = x;
        while (max != 0) {
            while (res & (1ULL<<i)) i++;
            if (max & 1) {
                res |= (1ULL<<i);
            }
            i++;
            max >>= 1;
        }
        return res;
    }
};
 
Java:
class Solution {
    public long minEnd(int n, int x) {
        long res = 0;
        long[] binX = new long[64];
        long[] binN = new long[64];
        long X = x;
        long N = n - 1;
        for (int i = 0; i < 64; i++) {
            binX[i] = (X >> i) & 1;
            binN[i] = (N >> i) & 1;
        }
        for (int i = 0, j = 0; i < 64; i++) {
            while (i < 64 && binX[i] != 0) {
                i++;
            }
            binX[i] = binN[j];
            j++;
        }
        for (int i = 0; i < 64; i++) {
            if (binX[i] == 1) {
                res += (1l << i);
            }
        }
        return res;
    }
}
:what:
 
Python:
class Solution:
    def minEnd(self, n: int, x: int) -> int:
        n -= 1
        res = 0

        for i in range(45): # loop 45 bits of final result
            curr = x & 1 # get last bit of x
            x >>= 1
            if not curr and n:
                curr = n & 1 # replace last bit of x with bit of n
                n >>= 1
            res += curr * 1 << i # get base 10 of final result

            if not n and not x: # break loop if no more replace or end of x
                break
            
        return res
 
Java:
public long minEnd(int n, int x) {
        int k = n-1;
        long res = 0;
        int rank = 0;
        int len = Integer.toBinaryString(x).length();
        while(k>0 || len>0){
            if((x&1)==0){
                res+=(long) (k&1)<<rank;
                k>>=1;
            }else
                res+=1<<rank;
            rank++;
            len--;
            x>>=1;
        }
        return res;
    }
Ngày thứ 9 rồi :ah:
1731140996353.png
 
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.313
Quay lại
Lên đầu trang