我在LeetCode的100题

我在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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 6. ZigZag Conversion 7. Reverse Integer 8. String to Integer (atoi) 9. Palindrome Number 10. Regular Expression Matching 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Swap Nodes in Pairs 24. Reverse Nodes in k-Group 25. Remove Duplicates from Sorted Array 26. Remove Element 27. Implement strStr() 28. Divide Two Integers 29. Substring with Concatenation of All Words 30. Next Permutation 31. Longest Valid Parentheses 32. Search in Rotated Sorted Array 33. Search for a Range 34. Find First and Last Position of Element in Sorted Array 35. Valid Sudoku 36. Sudoku Solver 37. Count and Say 38. Combination Sum 39. Combination Sum II 40. First Missing Positive 41. Trapping Rain Water 42. Jump Game 43. Merge Intervals 44. Insert Interval 45. Unique Paths 46. Minimum Path Sum 47. Climbing Stairs 48. Permutations 49. Permutations II 50. Rotate Image 51. Group Anagrams 52. Pow(x, n) 53. Maximum Subarray 54. Spiral Matrix 55. Jump Game II 56. Merge k Sorted Lists 57. Insertion Sort List 58. Sort List 59. Largest Rectangle in Histogram 60. Valid Number 61. Word Search 62. Minimum Window Substring 63. Unique Binary Search Trees 64. Unique Binary Search Trees II 65. Interleaving String 66. Maximum Product Subarray 67. Binary Tree Inorder Traversal 68. Binary Tree Preorder Traversal 69. Binary Tree Postorder Traversal 70. Flatten Binary Tree to Linked List 71. Construct Binary Tree from Preorder and Inorder Traversal 72. Construct Binary Tree from Inorder and Postorder Traversal 73. Binary Tree Level Order Traversal 74. Binary Tree Zigzag Level Order Traversal 75. Convert Sorted Array to Binary Search Tree 76. Convert Sorted List to Binary Search Tree 77. Recover Binary Search Tree 78. Sum Root to Leaf Numbers 79. Path Sum 80. Path Sum II 81. Binary Tree Maximum Path Sum 82. Populating Next Right Pointers in Each Node 83. Populating Next Right Pointers in Each Node II 84. Reverse Linked List 85. Reverse Linked List II 86. Partition List 87. Rotate List 88. Remove Duplicates from Sorted List 89. Remove Duplicates from Sorted List II 90. Intersection of Two Linked Lists 91. Linked List Cycle 92. Linked List Cycle II 93. Reorder List 94. Binary Tree Upside Down 95. Binary Tree Right Side View 96. Palindrome Linked List 97. Convert Binary Search Tree to Sorted Doubly Linked List 98. Lowest Common Ancestor of a Binary Tree 99. Lowest Common Ancestor of a Binary Search Tree 100. Binary Tree Level Order Traversal II

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值