官网地址:leetcode-cn.comphp
在官网注册后,点击题库页面就能够看到全部能够刷的题目了 html
问题描述 java
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
bool resolve = false;
for (int i = 0; i < nums.size() - 1; i ++) {
for (int j = i + 1; j < nums.size(); j ++){
if (nums[i] + nums[j] == target) {
res.push_back(i);
res.push_back(j);
resolve = true;
break;
}
}
if (resolve) {
break;
}
}
return res;
}
};
复制代码
而后提交就能够看到统计信息了 c++
leetcode题的答案不止一种,能够根据cpu和内存使用的反馈进行调优。编程
class Solution {
public:
bool isPowerOfTwo(long n) {
return n != 0 && (n & (n -1)) == 0;
}
};
复制代码
能够在题解界面查看其它同窗的解题思路 api
class Solution {
public:
bool isPowerOfFour(long n) {
return n != 0 && (n & 0xAAAAAAAA) == 0 && (n & (n -1)) == 0;
}
};
复制代码
class Solution {
public:
int fib(int n) {
if (n == 0) {
return 0;
}
if (n <= 2) {
return 1;
}
int a = 1, b = 1, i = 3, res = 0;
while (i <= n) {
res = a + b;
a = b;
b = res;
i ++;
}
return res;
}
};
复制代码
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int find = 0;
int j = nums.size();
for (int i = 0; i < j; i ++) {
cout << "i: " << i << " num:" << nums[i] << endl;
if (nums[i] == val) {
find ++;
swap(nums[i], nums[j - 1]);
j --;
i --;
}
}
return nums.size() - find;
}
};
复制代码
leetcode已经支持c, c++, java, php等主流编程语言编写答案了,能够选择本身擅长的语言 编程语言
能够在后台看到我的的答题报表 ui