📣 独立日限时特惠: VIP通行证立减$68
查看: 830| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

秋招刷题打卡

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
一天有空就多刷刷,大四明年毕业,JAVA为主

上一篇:开始高强度刷题!
下一篇:在职刷题打卡帖
🔗
 楼主| SleepingLion 2018-10-25 10:34:35 | 只看该作者
全局:
760 Find Anagram Mappings ---- Easy 用時4分鐘
用了兩個方法去解
1. Brute force
    public int[] anagramMappings(int[] A, int[] B) {
        int[] result = new int[A.length];
        for(int i = 0; i < A.length; i++){
            for(int j = 0; j < B.length; j++){
                if (B[j] == A[i]){
                    result[i] = j;
                }
            }
        }
        return result;
    }

2. HashMap
    public int[] anagramMappings(int[] A, int[] B) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < B.length; i++){
            map.put(B[i], i);
        }
        
        int[] result = new int[B.length];
        for(int i = 0; i < A.length;i++){
            result[i] = map.get(A[i]);
        }
        
        return result;
    }
回复

使用道具 举报

🔗
 楼主| SleepingLion 2018-10-25 10:46:00 | 只看该作者
全局:
657. Robot Return to Origin ---- Easy  Time : 4:37

1. For loop + swtich case / if else O(N)
    public boolean judgeCircle(String moves) {
        int x = 0;
        int y = 0;
        for(int i = 0; i < moves.length(); i++){
            switch(moves.charAt(i)){
                case('U'):
                    y += 1;
                    break;
                case('D'):
                    y -= 1;
                    break;
                case('L'):
                    x -= 1;
                    break;
                case('R'):
                    x += 1;
                    break;
                default:
                    break;
            }
        }
        return(x == 0 && y == 0);
    }
回复

使用道具 举报

🔗
 楼主| SleepingLion 2018-10-25 10:59:22 | 只看该作者
全局:
832. Flipping an Image --- Easy Time : 7:23

1. 想不到更好的方法 for loop走一遍, 第二個forloop換方向, 第三個forloop 10互換
    public int[][] flipAndInvertImage(int[][] A) {
        for(int i = 0; i < A.length; i++){
            for(int j = 0; j < A[i].length / 2; j++){
                int temp = A[i][j];
                A[i][j] = A[i][A[i].length - j - 1];
                A[i][A[i].length - j - 1] = temp;
            }
            for(int k = 0; k < A[i].length; k++){
                if(A[i][k] == 1){
                    A[i][k] = 0;
                }else{
                    A[i][k] = 1;
                }
            }
        }
        return A;
    }
回复

使用道具 举报

🔗
 楼主| SleepingLion 2018-10-25 12:16:15 | 只看该作者
全局:
617. Merge Two Binary Trees --- Easy

1. recursion to go through left side and right side.

    public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
        //BFS
        
        if(t1 == null){
            return t2;
        }
        if(t2 == null){
            return t1;
        }
        t1.val += t2.val;
        t1.left = mergeTrees(t1.left, t2.left);
        t1.right = mergeTrees(t1.right, t2.right);
        return t1;

    }
回复

使用道具 举报

🔗
 楼主| SleepingLion 2018-10-25 23:28:04 | 只看该作者
全局:
700. Search in a Binary Search Tree   Easy

    public TreeNode searchBST(TreeNode root, int val) {
        while(root != null){
            if(root.val < val){
                root = root.right;
            }else if(root.val > val){
                root = root.left;
            }else{
                return root;
            }
        }
        return null;
    }
回复

使用道具 举报

🔗
 楼主| SleepingLion 2018-10-26 07:22:28 | 只看该作者
全局:
905. Sort Array By Parity

While loop + 2 pointers O(N)

    public int[] sortArrayByParity(int[] A) {
        int i = 0;
        int j = A.length - 1;
        int temp;
        while (i < j){
            if (A[i] % 2 == 1 && A[j] % 2 == 0) {
                temp = A[i];
                A[i] = A[j];
                A[j] = temp;
                i += 1;
                j -= 1;
            } else if (A[i] % 2 == 0) {
                i += 1;
            } else if (A[j]%2 == 1) {
                j -= 1;
            }
        }
        return A;
    }
回复

使用道具 举报

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

本版积分规则

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