Announcement

Collapse
No announcement yet.

[Lớp thuật toán hè 2015] Tài liệu và trao đổi buổi 2

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 13521005
    replied
    Mình làm thế này. Phức tạp hơn một tí, tốc độ cũng không cải thiện gì ps:
    PHP Code:

    uint32_t reverseBits
    (uint32_t n) {
        
    int a;
        
    char befaft;//before - after
        
    int jx;
        for(
    int i 016i++)
        {
            
    31 i;
            
    bef = (>> i) & 1;
            
    aft = (>> j) & 1;
            
    bef aft;
            
    = (<< i) | (<< j);
            
    ^= a;

        }
        return 
    n;

    Last edited by 13521005; 09-07-2015, 09:35.

    Leave a comment:


  • 14520423
    replied
    PHP Code:
    uint32_t reverseBits(uint32_t n) {
        
    uint32_t temp=0;
        for(
    int i=0;i<32;i++)
        {   
            
            
    temp=temp<<1;
            if(
    n&1)
                
    temp=temp+1;
            
    n=n>>1;
        }
        return 
    temp;

    https://leetcode.com/problems/reverse-bits/ có vẻ đúng :3

    Leave a comment:


  • 13521005
    replied
    cải tiến đoạn code của mình bằng phương pháp đệ quy thì ta chỉ cần một dòng

    Originally posted by 13521005 View Post

    PHP Code:
    int singleNumber(intnumsint numsSize) {
        
    int result =  nums[0];
        for(
    int i 1numsSize i++)
        {
            
    result ^= nums[i];
        }
        return 
    result;

    PHP Code:
    int singleNumber(intnumsint numsSize) {
        return (
    numsSize == -1) ? : (singleNumber(numsnumsSize-1)) ^ nums[numsSize-1];

    :happy:

    Leave a comment:


  • toannv
    replied
    Originally posted by 14520674 View Post
    thầy ơi có tài liệu chi tiết ko ạ e ở nhà học cuối tháng này mới vào SG
    Chứ em muốn chi tiết cỡ nào nữa? :funny:

    Leave a comment:


  • 14520674
    replied
    thầy ơi có tài liệu chi tiết ko ạ e ở nhà học cuối tháng này mới vào SG

    Leave a comment:


  • toannv
    replied
    Originally posted by 14520769 View Post
    nếu như vậy thì nếu mảng có 2 phần tử duy nhất sẽ ra sai kết quả? VD đối với mảng {1, 2, 3, 1} thì kết quả sẽ là 2^3 = 0010 ^ 0011 = 0001 = 1?
    Đề bài giới hạn chuyện này rồi mà em: every element appears twice except for one . Yên tâm.
    Đọc kỹ hướng dẫn sử dụng trước khi dùng.
    Last edited by toannv; 08-07-2015, 10:29.

    Leave a comment:


  • 14520769
    replied
    nếu như vậy thì nếu mảng có 2 phần tử duy nhất sẽ ra sai kết quả? VD đối với mảng {1, 2, 3, 1} thì kết quả sẽ là 2^3 = 0010 ^ 0011 = 0001 = 1?

    Leave a comment:


  • 14520979
    replied
    Originally posted by 14520674 View Post
    mỗi lần xor thì nếu có 2 số giống nhau nó sẽ ra 0 nên sau khi xor hết dãy số thì những số giống nhau sẽ bị triệt tiêu chỉ còn lại số lặp 1 lần ) nên nó ra kết quả thôi )
    cảm ơn bạn, giờ thì hiểu rồi

    Leave a comment:


  • 14520674
    replied
    Originally posted by 14520979 View Post
    tại sao xor lần lượt như thế lại ra được kết quả đề bài yêu cầu. mấy anh giải thích giúp em với???
    mỗi lần xor thì nếu có 2 số giống nhau nó sẽ ra 0 nên sau khi xor hết dãy số thì những số giống nhau sẽ bị triệt tiêu chỉ còn lại số lặp 1 lần ) nên nó ra kết quả thôi )

    Leave a comment:


  • 14520979
    replied
    tại sao xor lần lượt như thế lại ra được kết quả đề bài yêu cầu. mấy anh giải thích giúp em với???

    Leave a comment:


  • 13520280
    replied
    - C++ using STL
    - 20ms
    PHP Code:
    class Solution {
    public:
        
    int singleNumber(vector<int>& nums
        {
            
    int result nums[0];
         
            for(
    std::vector<int>::iterator it nums.begin()+1;it!=nums.end();it++)
            {
                
    result ^=*it;
            }
            return 
    result;
        }
    }; 
    Last edited by 13520280; 07-07-2015, 13:48.

    Leave a comment:


  • 14520874
    replied
    Ý tưởng: Chia làm 2 mảng, mảng chứa số âm, mảng chứa số dương...

    PHP Code:
    #define _MAX 50000

    int singleNumber(intnumsint numsSize) {
        
    inthash = (int*)calloc(_MAX,sizeof(int));
        
    inthash2 = (int*)calloc(_MAX,sizeof(int));
        for (
    int i 0i<numsSizei++) {
            if (
    nums[i]<0hash[nums[i]*(-1)]++;
            else 
    hash2[nums[i]]++;
        }
        for (
    int i 0i<_MAXi++) {
            if (
    hash[i] == 1) return i*(-1);   
            if (
    hash2[i] == 1) return i;   
        }

    :embarrassed:

    Leave a comment:


  • 13520280
    replied
    Originally posted by 14520052 View Post
    PHP Code:
    int singleNumber(intnumsint numsSize) {
        
    int x=nums[0],i;
        for (
    i=1;i<numsSize;i++)
            
    x^nums[i];
        return 
    x;

    Ý tưởng by Thiên Phúc.
    Ừa ha. Hay thiệt! XOR :kiss:

    Leave a comment:


  • 14520052
    replied
    PHP Code:
    int singleNumber(intnumsint numsSize) {
        
    int x=nums[0],i;
        for (
    i=1;i<numsSize;i++)
            
    x^nums[i];
        return 
    x;

    Ý tưởng by Thiên Phúc.

    Leave a comment:


  • 13521005
    replied
    XOR tất cả phần tử với nhau sẽ ra kết quả
    vì 2 số giống nhau XOR lại sẽ = 0

    PHP Code:
    int singleNumber(intnumsint numsSize) {
        
    int result =  nums[0];
        for(
    int i 1numsSize i++)
        {
            
    result ^= nums[i];
        }
        return 
    result;

    Leave a comment:

LHQC

Collapse
Working...
X