Leetcode Brush Questions Week 2

Leetcode general question sheet: https://blog.csdn.net/m0_46272108/article/details/109269407

367. Effective perfect square number

The idea of ​​the ontology:
Find 1~num, then the time complexity is: O (n) O(n)O ( n )

The time complexity of binary search is: O (logn) O(logn)O ( l o g n )

C++ code

class Solution {
    
    
public:
    bool isPerfectSquare(int num) {
    
    
        int l = 1, r = num;
        while (l < r) {
    
    
            int mid = l + 1ll + r >> 1;//有可能爆int,1要改成longlong
            if (mid <= num / mid) {
    
    
                l = mid;//l = mid,上面必须要+1,否则可能会进入死循环
            } else {
    
    
                r = mid - 1;
            }
        }
        return r * r == num;
    }
};

Java code

class Solution {
    
    
    public boolean isPerfectSquare(int num) {
    
    
        int l = 1, r = num;
        while (l < r) {
    
    
            int mid = l + 1 + r >> 1;
            if (mid <= num / mid) {
    
    
                l = mid;//l = mid,上面必须要+1,否则可能会进入死循环
            } else {
    
    
                r = mid - 1;
            }
        }
        return r * r == num;
    }
}

371. Sum of Two Integers

This question requires no + -.
There is another way to calculate a+b ^ XOR operation: addition without carry
For example:

1 1 0 1
1 0 1 1
———
0 1 1 0
means no carry .
So we can bitwise AND operation to "carry" operations.
Example:
1 1 0 1
1 0 1 1
———
1 0 0 1
Carry again is << 1 shift one bit to the left.
The result is the
original result of 1 0 0 1 0 : 1101 + 1011 = 11000
0110 + 10010 = 11000 The
result is consistent.

So a+b is equivalent to [(a & b) << 1] + (a ^ b)
so there will still be a plus sign.
Because the title requires int, and int has only 32 bits.
Every time a is executed, there will be an additional 0 at the end,
so it is executed at most 32 times, the value of a is 0, and the value of b is the final result.

C++ code

class Solution {
    
    
public:
    int getSum(int a, int b) {
    
    
        if (!a) return b;
        int sum1 = a ^ b;
        int sum2 = (unsigned)(a & b) << 1;
        return getSum(sum2, sum1);
    }
};

Java code

class Solution {
    
    
    public int getSum(int a, int b) {
    
    
        if (a == 0) return b;
        int sum1 = a ^ b;
        int sum2 = (a & b) << 1;
        return getSum(sum2, sum1);
    }
}

374. Guess the size of the number

The ontology idea is also binary search:
cut half directly, search is faster than linear:
the code:

C++ code

/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is lower than the guess number
 *			      1 if num is higher than the guess number
 *               otherwise return 0
 * int guess(int num);
 */
class Solution {
    
    
public:
    int guessNumber(int n) {
    
    
        int l = 1, r = n;
        while (l < r) {
    
    
            int mid = (long long)l + r >> 1;//爆int
            if (guess(mid) <= 0) 
                r = mid;
            else 
                l = mid + 1;
        }
        return r;
    }
};

383. Ransom letter

Question: Determine whether the first string ransom can be composed of strings in the second string magazines.
Idea:
Open the map to save the string magazine, and count the number of characters in the string magazine,
then open the map to save the string ransom, and traverse the string ransom. If there is no string in the string ransom in the string magazine, then directly Return false. If there is, the corresponding character in the string magazine is -1. If the string ransom is traversed, and no false is returned, then the first string ransom can be changed from the characters in the second string magazine constitute. Return true;

Above code:

C++ code

class Solution {
    
    
public:
    bool canConstruct(string ransomNote, string magazine) {
    
    
        unordered_map<char, int> hash;
        for(auto x : magazine) hash[x]++;
        for(auto x : ransomNote)
            if(!hash[x])
                return false;
            else
                hash[x]--;
        return true;
    }
};

387. The first unique character in a string

Ideas:
1. Open map, save this string, count each character
2. Traverse it from beginning to end, find the first character with the number of characters == and output the corresponding subscript.

C++ code

class Solution {
    
    
public:
    int firstUniqChar(string s) {
    
    
        unordered_map<char, int> hash;
        for (auto x : s) hash[x]++;
        for (int i = 0; i < s.size(); ++i) {
    
    
            if (hash[s[i]] == 1) {
    
    
                return i;
            }
        }
        return -1;
    }
};

Java code

class Solution {
    
    
    public int firstUniqChar(String s) {
    
    
        int[] a = new int[26];
        // Arrays.fill(a, 0);
        for (int i = 0; i < s.length(); ++i) {
    
    
            a[s.charAt(i) - 'a']++;
        }
        for (int i = 0; i < s.length(); ++i) {
    
    
            if (a[s.charAt(i) - 'a'] == 1) 
                return i;
        }
        return -1;
    }
}

389. Find the difference

The use of XOR.
XOR all all at once.
The problem can be solved by using XOR operation mainly because XOR operation has the following characteristics:

  • A number and 0 do the XOR operation equal to itself: a⊕0 = a
  • XOR operation of a number and itself is equal to 0: a⊕a = 0
  • The exclusive OR operation satisfies the commutative and associative laws: a⊕b⊕a = (a⊕a)⊕b = 0⊕b = b

Therefore, based on the above basic conditions, all numbers are ORed in order, and the final result is the only number

C++ code

class Solution {
    
    
public:
    char findTheDifference(string s, string t) {
    
    
        char ch = 0;
        for (int i = 0; i < s.size(); ++i) {
    
    
            ch ^= s[i] ^ t[i];
        }
        return ch ^ t[t.size() - 1];
    }
};

Java code

class Solution {
    
    
    public char findTheDifference(String s, String t) {
    
    
        char ch = 0;
        for (int i = 0; i < s.length(); ++i) {
    
    
            ch ^= s.charAt(i) ^ t.charAt(i);
        }
        ch ^= t.charAt(t.length() - 1);
        return ch;
    }   
}

392. Judge subsequence

Idea: Dual pointer thinking

C++ code

class Solution {
    
    
public:
    bool isSubsequence(string s, string t) {
    
    
        int i = 0;
        for (int j = 0; i < s.size() && j < t.size(); ++j) {
    
    
            if (s[i] == t[j]) {
    
    
                i++;
            }
        }
        return i == s.size();
    }
};

Java code

class Solution {
    
    
    public boolean isSubsequence(String s, String t) {
    
    
        int i = 0;
        for (int j = 0; i < s.length() && j < t.length(); ++j) {
    
    
            if (s.charAt(i) == t.charAt(j)) {
    
    
                i++;
            }
        }
        return i == s.length();
    }
}

Guess you like

Origin blog.csdn.net/m0_46272108/article/details/109301316