leetcode
292 Nim 游戏
题目 :
你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。你作为先手。
你们是聪明人,每一步都是最优解。 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏。
代码:
class Solution {
public:
bool canWinNim(int n) {
return n%4;
}
};
class Solution {
public:
bool canWinNim(int n) {
int k=n>>2;
return k<<2!=n;
}
};
303 区域和检索 - 数组不可变
题目:
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。
代码:
class NumArray {
public:
vector<int> sum;
NumArray(vector<int>& nums) {
int temp=0;
for(int i=0;i<nums.size();i++){
temp += nums[i];
sum.push_back(temp);
}
}
int sumRange(int i, int j) {
if(i==0)
return sum[j];
return sum[j] - sum[i-1];
}
};
326 3的幂
题目:判断是不是3的幂
代码:
class Solution {
public:
bool isPowerOfThree(int n) {
if(n<3&&n!=1)
return false;
while(n%3==0){
n/=3;
}
if(n==1)
return true;
else
return false;
}
};
342 4的幂
题目:
如上题。
代码:
class Solution {
public:
bool isPowerOfFour(int num) {
if(num<1)
return false;
while(num%4==0)
num/=4;
if(num==1)
return true;
else
return false;
}
};
class Solution {
public:
bool isPowerOfFour(int num) {
if(num < 1)
return false;
// Is it a power of 2
if((num & (num-1)) != 0)
return false;
// What is the power
int power = 0;
while(num > 1) {
num >>= 1;
++power;
}
// If it is even power of 2 then it is a power of 4
if(power % 2 == 0)
return true;
return false;
}
};
344 反转字符串
题目:
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。
代码:
public void reverseString(char[] s) {
int length = s.length;
int i;
for (i = 0; i < length / 2; i++){
char tmp = s[i];
s[i] = s[length - i - 1];
s[length - i - 1]= tmp;
}
}
}
class Solution(object):
@staticmethod
def reverseString(s):
"""
:type s: List[str]
:rtype: None Do not return anything, modify s in-place instead.
"""
left,right = 0,len(s) - 1
while left<right:
s[left],s[right] = s[right],s[left]
left+=1
right-=1
345 反转字符串中的元音字母
题目:
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
代码:
class Solution {
public String reverseVowels(String s) {
int left = 0;
int right = s.length();
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (isVowel(chars[i])) {
left = i;
right = replaceVowel(left, right, chars);
}
}
return new String(chars);
}
int replaceVowel(int left, int right, char[] chars) {
for (int j = right - 1; j > left; j--) {
if (isVowel(chars[j])) {
char ch = chars[left];
chars[left] = chars[j];
chars[j] = ch;
return j;
}
}
return 0;
}
boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
}
}
class Solution:
def reverseVowels(self, s: str) -> str:
s = list(s)
vowels = set(['a','e','i','o','u','A','E','I','O','U'])
i = 0
j = len(s) -1;
while i<j:
if s[i] not in vowels:
i += 1
continue
if s[j] not in vowels:
j -= 1
continue
s[i],s[j] = s[j],s[i]
i += 1
j -= 1
return "".join(s)
349 Intersection of Two Arrays
题目:
给定两个数组,编写一个函数来计算它们的交集。
代码:
最蠢的方法:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> result;
for(int i=0;i<nums1.size();i++){
for(int j=0;j<nums2.size();j++){
if(nums1[i]==nums2[j])
result.push_back(nums1[i]);
}
}
sort(result.begin(),result.end());
result.erase(unique(result.begin(),result.end()),result.end());
return result;
}
};
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set1 = new HashSet<Integer>();
for(int i:nums1) set1.add(i);
HashSet<Integer> set2 = new HashSet<Integer>();
for(int i:nums2) set2.add(i);
set1.retainAll(set2);
int[] out = new int[set1.size()];
int a=0;
for(int i:set1){
out[a++] = i;
}
return out;
}
}
350. 两个数组的交集 II
题目:
给定两个数组,编写一个函数来计算它们的交集。
注意:输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。
我们可以不考虑输出结果的顺序。
代码:
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
sort(nums1.begin(),nums1.end());
sort(nums2.begin(),nums2.end());
int n = nums1.size();
int m = nums2.size();
int i=0,j=0;
vector<int> opt;
while(i<n&&j<m)
{
if(nums1[i]==nums2[j])
{
opt.push_back(nums1[i]);
i++;
j++;
}
else if(nums1[i]<nums2[j]){
i++;
}else{
j++;
}
}
return opt;
}
};
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
int m = nums1.length;
int n = nums2.length;
int i=0,j=0;
int[] opt = new int[m];
int k=0;
while(i<m && j<n){
if(nums1[i]==nums2[j]){
opt[k++]=nums1[i];
i++;
j++;
}
else if(nums1[i]<nums2[j]){
i++;
}
else{
j++;
}
}
int[] out = new int[k];
for(int p=0;p<k;p++)
out[p] = opt[p];
return out;
}
}
367 有效的完全平方数
题目:
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
说明:不要使用任何内置的库函数,如 sqrt。
代码:
class Solution {
public boolean isPerfectSquare(int num) {
long long i = 2;
for(;i*i<=num;i++);
return (i-1)*(i-1)==num;
}
}
374 猜数字大小
题目:
我们正在玩一个猜数字游戏。 游戏规则如下:
我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。
每次你猜错了,我会告诉你这个数字是大了还是小了。
你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0):
-1 : 我的数字比较小
1 : 我的数字比较大
0 : 恭喜!你猜对了!
思路:
典型的二分查找问题。
代码:
int guess(int num);
class Solution {
public:
int guessNumber(int n) {
int low = 1;
int high = n;
while(low < high){
int temp = low + (high - low)/2;
if(guess(temp) == 0)
return temp;
else if(guess(temp) == 1)
low = temp + 1;
else
high = temp -1;
}
return low;
}
};