# Leetcode刷题 week 2

### Leetcode刷题 week 2

Leetcode 总题单：https://blog.csdn.net/m0_46272108/article/details/109269407

# 367.有效的完全平方数

## C++代码

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代码

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.两整数之和

^异或运算有另一种说法：不进位加法

1 1 0 1
1 0 1 1
———
0 1 1 0

1 1 0 1
1 0 1 1
———
1 0 0 1

0110 + 10010 = 11000

## C++代码

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代码

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.猜数字大小

## C++代码

/**
* 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.赎金信

## C++代码

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.字符串中的第一个唯一字符

1.开map，存这个字符串，对每一个字符进行计数
2.从头到尾遍历一遍，找到字符数第一个==1的字符对应下标输出即可。

## C++代码

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代码

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.找不同

• 一个数和0做异或运算等于本身：a⊕0 = a
• 一个数和其本身做XOR运算等于 0：a⊕a = 0
• 异或运算满足交换律和结合律：a⊕b⊕a = (a⊕a)⊕b = 0⊕b = b

## C++代码

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代码

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.判断子序列

## C++代码

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代码

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();
}
}

12-25 59
04-08 146

06-06 56
04-08 35
03-23 74
04-15 89
04-19 171
06-01 49
05-05 56
04-21 40
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客