This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使用头插入的方式,建立一个新的链表 | |
// 12 ms | |
class Solution { | |
public: | |
ListNode* reverseList(ListNode* head) { | |
ListNode *temp = nullptr,*nextNode = nullptr; | |
while(head){ | |
nextNode = head->next; | |
head->next = temp; | |
temp = head; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使用哈希法,用数组提前记录所有值,然后再进行遍历 | |
class Solution { | |
public: | |
int numJewelsInStones(string J, string S) { | |
int array[100] = {0}; | |
int temp = 0; | |
for(int i = 0;i < S.length();i++){ | |
array[S[i] - 65]++; | |
} | |
for(int i = 0;i < J.length();i++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//比较巧妙的思路 | |
class Solution { | |
public: | |
int rob(vector<int>& nums) { | |
int a = 0,b = 0; | |
for(int i = 0;i < nums.size();i++){ | |
if(i % 2 == 0) | |
a = (a + nums[i]) > b ? (a + nums[i]) : b; | |
else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int countPrimes(int n) { | |
if(n <= 2) return 0; | |
vector<bool> passed(n,false); | |
int sum = 1; | |
int upper = sqrt(n); | |
for(int i = 3;i < n;i += 2){ | |
if(!passed[i]){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//使用动态规划解答 | |
class Solution { | |
int uniquePaths(int m, int n) { | |
if (m > n) return uniquePaths(n, m); | |
vector<int> cur(m, 1); | |
for (int j = 1; j < n; j++) | |
for (int i = 1; i < m; i++) | |
cur[i] += cur[i - 1]; | |
return cur[m - 1]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<string> letterCasePermutation(string S) { | |
vector<string> vs; | |
helper(vs,S,0); | |
return vs; | |
} | |
void helper(vector<string> & vs,string &S,int p){ | |
if(p == S.size()){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int maxProfit(vector<int>& prices) { | |
int maxPro = 0; | |
int minPrice = INT_MAX; | |
for(int i = 0;i < prices.size();i++){ | |
minPrice = min(minPrice,prices[i]); | |
maxPro = max(maxPro,prices[i] - minPrice); | |
} | |
return maxPro; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
double myPow(double x, int n) { | |
double ans = 1; | |
unsigned long long p; | |
if (n < 0) { | |
p = -n; | |
x = 1 / x; | |
} | |
else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
bool Increment(char* number); | |
void PrintNumber(char* number); | |
void Print1ToMaxOfNDigits(int n) { | |
if (n <= 0) | |
return; |