123
返回列表 发新帖
楼主: love1point
跳转到指定楼层
上一主题 下一主题
收起左侧

三周刷题记录,和同学们讨论,欢迎大家指正我的代码需要优化的地方

🔗
 楼主| love1point 2017-2-13 01:35:04 | 只看该作者
全局:
15) 405. Convert a Number to Hexadecimal

Do not know how to do use bit manipulation. Refer solution. How to use >>> and &     !!!!!!!!!

  1. public class Solution {
  2.      char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  3.    
  4.     public String toHex(int num) {
  5.         if(num == 0) return "0";
  6.         String result = "";
  7.         while(num != 0){
  8.             result = map[(num & 15)] + result;
  9.             num = (num >>> 4);
  10.         }
  11.         return result;
  12.     }
  13. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2017-2-13 02:18:24 | 只看该作者
全局:
15) 459. Repeated Substring Pattern

Times out, anyone improves my code?

  1. public class Solution {
  2.     public boolean repeatedSubstringPattern(String str) {
  3.         for(int i = 0; i < str.length(); i++)
  4.         {
  5.             for(int j = 0; j < str.length(); j++)
  6.             {
  7.                 String result = "";
  8.                 String temp = str.substring(i, j + 1);
  9.                 int tempLength = temp.length();
  10.                 if(tempLength == str.length())
  11.                 {
  12.                     return false;
  13.                 }
  14.                 int counter = str.length() / tempLength;
  15.                 for(int k = 0; k < counter; k++)
  16.                 {
  17.                     result += temp;
  18.                 }
  19.                 if(result.equals(str))
  20.                 {
  21.                     return true;
  22.                 }
  23.             }
  24.         }
  25.         return false;
  26.     }
  27. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2017-2-13 06:54:43 | 只看该作者
全局:
15) 437. Path Sum III

DFS

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11.     public int pathSum(TreeNode root, int sum) {
  12.         if(root == null)
  13.         {
  14.             return 0;
  15.         }
  16.         return findPath(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
  17.     }
  18.    
  19.     public int findPath(TreeNode root, int sum)
  20.     {
  21.         int result = 0;
  22.         if(root == null)
  23.         {
  24.             return result;
  25.         }
  26.         if(root.val == sum)
  27.         {
  28.             result++;
  29.         }
  30.         result += findPath(root.left, sum - root.val);
  31.         result += findPath(root.right, sum - root.val);
  32.         return result;
  33.     }
  34. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2017-2-15 10:35:16 | 只看该作者
全局:
17) Battleships in a Board

Do not know how to do at first. I did not pay attention to last assumption.

  1. public class Solution {
  2.     public int countBattleships(char[][] board) {
  3.         int result = 0;
  4.         for(int i = 0; i < board.length; i++)
  5.         {
  6.             for(int j = 0; j < board[0].length; j++)
  7.             {
  8.                 if(board[i][j] == '.')
  9.                 {
  10.                     continue;
  11.                 }
  12.                 if(i > 0 && board[i - 1][j] == 'X')
  13.                 {
  14.                     continue;
  15.                 }
  16.                 if(j > 0 && board[i][j - 1] == 'X')
  17.                 {
  18.                     continue;
  19.                 }
  20.                 result++;
  21.             }
  22.         }
  23.         return result;
  24.     }
  25. }
复制代码


回复

使用道具 举报

🔗
 楼主| love1point 2017-2-15 11:13:40 | 只看该作者
全局:
18) 413. Arithmetic Slices

If there are only three elements to form arithmetic slice, it will be very easy, but it said at least element, so I think this is the reason it is medium level.

  1. public class Solution {
  2.     public int numberOfArithmeticSlices(int[] A) {
  3.         if(A.length < 3)
  4.         {
  5.             return 0;
  6.         }
  7.         int current = 0;
  8.         int result = 0;
  9.         for(int i = 0; i < A.length - 2; i++)
  10.         {
  11.             if(A[i + 1] - A[i] == A[i + 2] - A[i + 1])
  12.             {
  13.                 current += 1;
  14.                 result += current;
  15.             }
  16.             else
  17.             {
  18.                 current = 0;
  19.             }
  20.         }
  21.         return result;
  22.     }
  23. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2017-2-15 12:14:42 | 只看该作者
全局:
19 ) Queue Reconstruction by Height

Have no idea how to do.

  1. public class Solution {
  2.    public int[][] reconstructQueue(int[][] people) {
  3.         Arrays.sort(people,new Comparator<int[]>(){
  4.            @Override
  5.            public int compare(int[] o1, int[] o2){
  6.                return o1[0]!=o2[0]?-o1[0]+o2[0]:o1[1]-o2[1];
  7.            }
  8.         });
  9.         List<int[]> res = new LinkedList<>();
  10.         for(int[] cur : people)
  11.         {
  12.             res.add(cur[1],cur);      
  13.         }
  14.         return res.toArray(new int[people.length][]);
  15.     }
  16. }
复制代码
回复

使用道具 举报

🔗
 楼主| love1point 2017-2-16 13:56:15 | 只看该作者
全局:
20) 384. Shuffle an Array

OOP. Difficult is how to random shuffle.

  1. public class Solution {
  2.     private int[] nums;
  3.     private Random random;

  4.     public Solution(int[] nums) {
  5.         this.nums = nums;
  6.         random = new Random();
  7.     }
  8.    
  9.     /** Resets the array to its original configuration and return it. */
  10.     public int[] reset() {
  11.         return nums;
  12.     }
  13.    
  14.     /** Returns a random shuffling of the array. */
  15.     public int[] shuffle() {
  16.         if(nums == null) return null;
  17.         int[] a = nums.clone();
  18.         for(int j = 1; j < a.length; j++) {
  19.             int i = random.nextInt(j + 1);
  20.             swap(a, i, j);
  21.         }
  22.         return a;
  23.     }
  24.    
  25.     private void swap(int[] a, int i, int j) {
  26.         int t = a[i];
  27.         a[i] = a[j];
  28.         a[j] = t;
  29.     }
  30. }
复制代码
回复

使用道具 举报

🔗
sharkwolf 2017-2-23 21:02:42 | 只看该作者
全局:
祝楼主好运!
回复

使用道具 举报

全局:
lz加油,我也要像lz一样刷...
回复

使用道具 举报

🔗
 楼主| love1point 2017-3-3 00:07:43 | 只看该作者
全局:
去onsite面试途中,下周拿到offer定回来提供内推
回复

使用道具 举报

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

本版积分规则

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