楼主: love1point
跳转到指定楼层
上一主题 下一主题
收起左侧

Leetcode 刷题记录!!!不信自己今年找不到工作!!!

 
🔗
 楼主| love1point 2015-5-30 20:00:29 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-15 18:03 编辑

Linked List cycle: https://leetcode.com/problems/linked-list-cycle/

idea: 网上大部分解法都是这种,快慢指针,快的每次走两步,慢的每次走两步,如果有环,二者肯定会相遇,就像在800米跑道,俩同学一起跑步,二者速度不同,最终他们肯定会相遇,因操场是圆的
  1. public class Solution {
  2.     public boolean hasCycle(ListNode head) {
  3.         ListNode fast = head;
  4.         ListNode slow = head;
  5.         
  6.         if(head == null)
  7.         {
  8.             return false;   
  9.         }
  10.         
  11.         while(fast != null && fast.next != null)
  12.         {
  13.             slow = slow.next;
  14.             fast = fast.next.next;
  15.             if(fast == slow)
  16.             {
  17.                 return true;
  18.             }
  19.             
  20.         }
  21.         return false;
  22.     }
  23. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-30 20:58:30 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-15 18:08 编辑

Binary tree preorder traversal: https://leetcode.com/problems/binary-tree-preorder-traversal/
idea: 递归代码比较简单。前序顺序就是根,左,右。理解了此题,inorder和postorder就改一行代码的顺序即可。
  1. public class Solution {
  2.     public List<Integer> preorderTraversal(TreeNode root) {
  3.         List<Integer> result = new ArrayList<Integer>();
  4.         
  5.          if(root == null)
  6.         {
  7.             return result;
  8.         }
  9.         
  10.         helper(root, result);
  11.         
  12.         return result;
  13.     }
  14.    
  15.     public List<Integer> helper(TreeNode root, List<Integer> result)
  16.     {
  17.         if(root == null)
  18.         {
  19.             return result;
  20.         }
  21.         else
  22.         {
  23.             result.add(root.val);
  24.         }
  25.         
  26.         helper(root.left, result);
  27.         
  28.         helper(root.right, result);
  29.         
  30.         return result;
  31.     }
  32. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-30 21:57:10 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-15 18:10 编辑

Reverse Linked List:  https://leetcode.com/problems/reverse-linked-list/
idea: 此代码应该是网上能找到的最短的代码了。可以用俩个节点为例子进行过代码。用到了递归。
  1. public class Solution {
  2.     public ListNode reverseList(ListNode head) {
  3.         if(head == null || head.next == null)
  4.         {
  5.             return head;
  6.         }
  7.         
  8.         ListNode second = head.next;
  9.         head.next = null;
  10.         
  11.         ListNode rest = reverseList(second);
  12.         second.next = head;
  13.         
  14.         return rest;
  15.         
  16.     }
  17. }
复制代码
回复

使用道具 举报

🔗
calalia 2015-5-30 22:13:04 | 只看该作者
本楼:
全局:
楼楼加油~~
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-31 08:40:21 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-15 18:16 编辑

Climbing stairs:  https://leetcode.com/problems/climbing-stairs/
idea: 这题用动归。 代码走一遍,很容易记下了。每次能爬一格或者两格,比如在第三格,它只能从第一格或第二格来的。所以。f(n) = f(n-1) + f(n-2),f(n) 为 爬到第n 格的可能性组合。初始状态f(0) =1,因为第一格只有一种可能性,就是从地面到第一格爬一步。f(1) = 2, 两种,从地面直接爬两格,或者一格一格爬,从地面到一楼,一楼到二楼。
  1. public class Solution {
  2.     public int climbStairs(int n) {
  3.         if(n == 0)
  4.         {
  5.             return 0;
  6.         }
  7.         
  8.         int[] f = new int[3];
  9.         f[0] = 1;
  10.         f[1] = 2;
  11.         
  12.         if(n == 1)
  13.         {
  14.             return f[0];
  15.         }
  16.         if(n == 2)
  17.         {
  18.             return f[1];
  19.         }
  20.         
  21.         for(int i = 2; i < n; i++)
  22.         {
  23.             f[2] = f[0] + f[1];
  24.             f[0] = f[1];
  25.             f[1] = f[2];
  26.         }
  27.         return f[2];
  28.     }
  29. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-31 11:57:47 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-22 15:30 编辑

Count primes:  https://leetcode.com/problems/count-primes/

idea: 目前我还没找到比较容易记忆和理解的解法,暂且跟着代码走一遍好了,感觉好像也不太会考这种题。以后有看到好的代码再取代了。

        更新: 已找到很好解释改代码的算法思想,再次体会到算法思想的牛逼。 先假设大于等于2的数且小于n的数为Prime, 如果该数为prime,在循环范围内,它的2倍数就不是prime。


  1. public class Solution {
  2.     public int countPrimes(int n) {
  3.      if(n <= 2)
  4.      {
  5.          return 0;
  6.      }
  7.      boolean[] result = new boolean[n];
  8.      for(int i = 2; i < n; i++)
  9.      {
  10.          result[i] = true;
  11.      }
  12.      
  13.      for(int i = 2; i < n; i++)
  14.      {
  15.          if(result[i])
  16.          {
  17.              for(int j = 2 * i; j < n; j += i)
  18.              {
  19.                  result[j] = false;
  20.              }
  21.          }
  22.      }
  23.      
  24.      int count = 0;
  25.       for(int i = 2; i < n; i++)
  26.      {
  27.          if(result[i])
  28.          {
  29.              count++;
  30.          }
  31.      }
  32.      return count;
  33.     }
  34. }
复制代码

回复

使用道具 举报

🔗
PeaceinUESTC 2015-5-31 12:27:58 | 只看该作者
全局:
我想问下你怎么知道自己刷题的时候答案是正确的或者最优解?
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-31 15:13:08 | 只看该作者
全局:
PeaceinUESTC 发表于 2015-5-31 12:27
我想问下你怎么知道自己刷题的时候答案是正确的或者最优解?

我这些代码都是在leetcode accepted之后贴上来的。但不一定是最优解啊
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-31 15:13:27 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-15 18:21 编辑

Sort colors:  https://leetcode.com/problems/sort-colors/

idea: 这题暂且记住如下代码,代码还是比较好记得。思路就是我们先把0排在一起,1排在一起,剩下的就是2了。
  1. public class Solution {
  2.     public void sortColors(int[] nums) {
  3.         int index0 = 0;
  4.         int index1 =0;
  5.         for(int i = 0; i < nums.length; i++)
  6.         {
  7.             if(nums == 0)
  8.             {
  9.                 nums = 2;
  10.                 nums[index1++] = 1;
  11.                 nums[index0++] = 0;
  12.             }
  13.             else
  14.             {
  15.                 if(nums == 1)
  16.                 {
  17.                     nums = 2;
  18.                     nums[index1++] = 1;
  19.                 }
  20.             }
  21.                
  22.             }
  23.     }
  24. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2015-5-31 16:55:47 | 只看该作者
全局:
本帖最后由 love1point 于 2015-6-20 16:44 编辑

implement strStr(): https://leetcode.com/problems/implement-strstr/

idea: 记住要用 if(flag) 来控制输出。用个具体例子来过代码,比如 haystack 为 apple,  needle 为 ap.

  1. public class Solution {
  2.     public int strStr(String haystack, String needle) {
  3.         int m = haystack.length();
  4.         int n = needle.length();
  5.         for(int i = 0; i < m - n + 1; i++)
  6.         {
  7.             boolean check = true;
  8.             for(int j = 0; j < n; j++)
  9.             {
  10.                
  11.                 if(haystack.charAt(i+j) != needle.charAt(j))
  12.                 {
  13.                     check = false;
  14.                     break;
  15.                 }
  16.             }
  17.             if(check)
  18.             {
  19.                 return i;
  20.             }
  21.         }
  22.       
  23.         return -1;

  24.     }
  25. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表