我在LeetCode上面AC的只有94题,但是我思考的题目已经超过100题了。100题,我依然是菜鸟,不过算是入门了。
我想回忆一下,算是总结与分享。
一.我的小小经验
很幸运一开始和师兄一起训练,如果不是的话我觉得很难坚持一下,因为我自己做题的话是不可能一道题做一个小时的。我觉得一道题思考这么长时间是有它的收获的,现在我也慢慢练习在一个人的情况下思考尽可能长的时间。相信我,思考的时间与收获成正比。
模仿与学习,还是因为训练营,我有幸与很多我们学校的大牛一起,听他们讲题目的解法,我尝试把一种解法运用到多道题目上面。除此之外,网上也是有很多解题的代码的,不要因为自己做出来就不出查其它答案,大多数时候还是有更好的解法的。即使你的解法已经是最好的,一个解法代表着一种思想,还是有用的。
交流,交流也是一种模仿与学习,这是面对面的,效果自然更好。一起做一道题,共同分享自己的见解。
细心,我把它放到最后,是因为我现在也做得不好,LeetCode太过友好,它会提示我们的错误,然后我们去更正。但是我们不能太过依赖LeetCode,我现在都是想好了,写好代码了,还要想想有没有漏洞,然后才是输入自己的测试数据,正确才提交,想以此让自己更加细心。
二.实现巧妙的题目
231. Power of Two
Describe
Given an integer, write a function to determine if it is a power of two.
Solution
C Language
bool isPowerOfTwo(int n)
{
return n > 0 && (n&(n - 1)) == 0;
}
通过n&(n-1)的结果为n消去最右边的一个1,如果消去了这个一等于0,那么二进制里面只有一个1。
Similar Problems:338. Counting Bits
136. Single Number
Describe
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Solution
C Language
int singleNumber(int* nums, int numsSize)
{
int index, result = 0;
for(index = 0; index < numsSize; index++)
result ^= nums[index];
return result;
}
这道题用上了异或运算符,A^A = 0, 0^A = A.
Similar Problem:260. Single Number III
三.多想法的题目
55. Jump Game
Describe
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
Example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
Solution
C Language
Think one:在遇到0的时候回头看前面有没有某个点能跳过这个0,因为没有0的话,一次走一步就能过去,i - j为第i个点到等于0的点的距离。
bool canJump(int* nums, int numsSize)
{
int i, j;
for(i = 0; i < numsSize - 1; i++)
if(nums[i] == 0) {
for(j = i - 1; j >= 0; j--)
if(nums[j] > i - j)
break;
if(j < 0)
return 0;
}
return 1;
}
Think two:用一个max记录,到第i个点时能跳到的最远的点,if(max < i)判断能否跳到现在的第i个点,if(nums[i] + i > max)判断当前点跳的距离是不是比max大。
bool canJump(int* nums, int numsSize)
{
int max;
int i;
if(numsSize == 0)
return 1;
max = nums[0];
for(i = 1; i < numsSize - 1; i++) {
if(max < i)
return 0;
if(nums[i] + i > max)
max = nums[i] + i;
}
if(max >= numsSize - 1)
return 1;
else
return 0;
}
215. th Largest Element in an Array
Describe
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.
Solution
C Language
各种排序算法的变形。
one: quick sort partition
int top_k(int *array, int low, int high, int k)
{
int index = low;
int i, tmp, pivot = array[high];
for(i = low; i < high; i++)
if(array[i] > pivot) {
tmp = array[i];
array[i] = array[index];
array[index] = tmp;
index++;
}
array[high] = array[index];
array[index] = pivot;
if(index == k - 1)
return array[index];
else if(index > k - 1)
return top_k(array, low, index - 1, k);
else
return top_k(array, index + 1, high, k);
}
int findKthLargest(int* nums, int numsSize, int k) {
return top_k(nums, 0, numsSize - 1, k);
}
two: select sort
int findKthLargest(int* nums, int numsSize, int n)
{
int i, j, k, tmp;
for(i = 0; i < n; i++) {
k = i;
for(j = i + 1; j < numsSize; j++)
if(nums[j] > nums[k])
k = j;
if(k != i){
tmp = nums[k];
nums[k] = nums[i];
nums[i] = tmp;
}
}
return nums[n - 1];
}
My LeetCode Solution in github:https://github.com/ChenJingPiao/LeetCode