37. 反轉一個3位整數
Note
這一題反覆的測試後我發現了,運算符之間減少空格能很明顯地降低運行時間,且同樣的數字,拆開運算比整個運算更加省時,比如100就拆爲10*10。
class Solution {
public:
/**
* @param number: A 3-digit number.
* @return: Reversed number.
*/
int reverseInteger(int number) {
return number%10*10*10+number/10%10*10+number/10/10%10;
}
};
145. 大小寫轉換
Note
我現在算是知道爲什麼LintCode必刷了。返回形參和直接返回結果,其用時是完全不一樣的。直接返回character - 32,用時50ms,而return m只需10ms,差距太大了吧。。。
class Solution {
public:
/**
* @param character: a character
* @return: a character
*/
char lowercaseToUppercase(char character) {
// write your code here
char m = character - 32;
return m;
}
};
283. 三數之中的最大值
Note
無。
class Solution {
public:
/**
* @param num1: An integer
* @param num2: An integer
* @param num3: An integer
* @return: an interger
*/
int maxOfThreeNumbers(int num1, int num2, int num3) {
int max = num1 > num2? num1:num2;
max = max > num3? max:num3;
return max;
}
};
366. 斐波納契數列
Note
無。
最開始的代碼。
class Solution {
public:
/**
* @param n: an integer
* @return: an ineger f(n)
*/
int fibonacci(int n) {
vector<int> vec;
vec.push_back(0);
vec.push_back(1);
for (int i = 2; i < 50; ++i)
{
vec.push_back(vec[i - 1] + vec[i - 2]);
}
return vec[n - 1];
}
};
改進後的代碼,直接只計算到n - 1即可。
class Solution {
public:
/**
* @param n: an integer
* @return: an ineger f(n)
*/
int fibonacci(int n) {
vector<int> vec = {0, 1};
while (n > vec.size())
vec.push_back(vec.back() + vec[vec.size() - 2]);
return vec[n - 1];
}
};
463. 整數排序
Note
用的插入排序,用隨機快排應該可以用時更少。
class Solution {
public:
/**
* @param A: an integer array
* @return: nothing
*/
void sortIntegers(vector<int> &A) {
for (int i = 0; i < A.size(); ++i)
{
int temp = A[i], j = i;
while (j > 0 && temp < A[j - 1])
{
A[j] = A[j - 1];
--j;
}
A[j] = temp;
}
}
};
466. 鏈表節點計數
Note
很意外的,事實證明,堆中的形參確實參與運算要快許多。
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: the first node of linked list.
* @return: An integer
*/
int countNodes(ListNode * head) {
ListNode *p = head;
int count = 0;
while (p != nullptr)
{
++count;
p = p->next;
}
return count;
}
};
484. 交換數組兩個元素
Note
真不明白那些42ms通過的怎麼做到的。。。居然還有代碼長度只有一百多的。。。
class Solution {
public:
/**
* @param A: An integer array
* @param index1: the first index
* @param index2: the second index
* @return: nothing
*/
void swapIntegers(vector<int> &A, int index1, int index2) {
int temp = A[index1];
A[index1] = A[index2];
A[index2] = temp;
}
};
632. 二叉樹的最大節點
Note
要考慮到這是一顆二叉樹,所以存在沒有子結點或只有一個子結點的情況。採用遞歸,自上而下的遞歸,自下而上的返回結點。左右孩子都有時,返回左孩子和右孩子中值較大的結點;只有一個孩子時返回孩子結點;沒有孩子時返回根結點。將返回的結點與根結點比較,再向上返回值較大的一個。以此實現自下而上的返回出值最大的結點。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: the root of tree
* @return: the max node
*/
TreeNode * maxNode(TreeNode * root) {
if (root == NULL)
return NULL;
if (root->left == NULL && root->right == NULL)
return root;
TreeNode *a, *b, *max;
a = maxNode(root->left);
b = maxNode(root->right);
if (a == NULL && b != NULL)
max = b;
else if (a != NULL && b == NULL)
max = a;
else
max = a->val > b->val? a:b;
if (root->val < max->val)
return max;
else
return root;
}
};
一定要自己寫一遍哦~~~