官網地址: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