查看: 1198| 回复: 17
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题记录帖

全局:

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

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

x
本帖最后由 用户1234 于 2018-12-18 16:20 编辑

刷题
每天至少三道 找到手感

评分

参与人数 2大米 +8 收起 理由
hyklxf + 5 给你点个赞!
che9695 + 3 给你点个赞!

查看全部评分


上一篇:刷题打卡
下一篇:面试刷题咯~每天10道~
推荐
 楼主| 用户1234 2018-12-18 16:17:54 | 只看该作者
全局:
33. Search in Rotated Sorted Array
class Solution {
    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[start] == target) {
                return start;
            }
            if (nums[mid] >= nums[start]) {
                if (target <= nums[mid] && target >= nums[start]) {
                    end = mid - 1;
                }else {
                    start = mid + 1;
                }
            } else {
                if (target >= nums[mid] && nums[start] >= target) {
                    start = mid + 1;
                } else {
                    end = mid -1 ;
                }
            }
        }
         return -1;
    }
}
回复

使用道具 举报

推荐
 楼主| 用户1234 2018-12-3 05:17:56 | 只看该作者
全局:
2018.12.01
265. Paint House II
256. Paint House
class Solution {
    public int minCostII(int[][] costs) {
                if (costs == null || costs.length == 0) {
            return 0;
        }
        int min = 0;
        int min_sec = 0;
        int index = -1;
        for (int i = 0; i < costs.length; i++) {
            int currentMin = Integer.MAX_VALUE;
            int currentSecMin = Integer.MAX_VALUE;
            int currentIndex = -1;
            for (int j =0; j < costs[0].length; j++) {
                costs[i][j] += (j != index? min: min_sec);
                if (costs[i][j] < currentMin) {
                    currentSecMin = currentMin;
                    currentMin = costs[i][j];
                    currentIndex = j;
                } else if (costs[i][j] < currentSecMin) {
                    currentSecMin = costs[i][j];
                }
            }
            min  = currentMin;
            min_sec = currentSecMin;
            index = currentIndex;
        }
        return min;
    }
}


两题解法通用,动态规划, 记住前一个第二小
回复

使用道具 举报

推荐
 楼主| 用户1234 2018-12-3 06:34:07 | 只看该作者
全局:
class Solution {
    public int rob(int[] nums) {
        /*
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int[] dp = new int[nums.length + 1];
        dp[0] = 0;
        dp[1] = nums[0];
        for (int i = 2; i < nums.length + 1; i++) {
            dp[i] = Math.max(dp[i - 2] + nums[i -1], dp[i - 1]);
        }
        return dp[nums.length];
        */
        if (nums == null || nums.length == 0) {
            return 0;
        }
        
        int max = nums[0];
        int previousMax = 0;
        for (int i = 1; i < nums.length; i++) {
            int currentMax = Math.max(max, nums[i] + previousMax);
            previousMax = max;
            max = currentMax;
        }
        return max;
    }
}
回复

使用道具 举报

🔗
 楼主| 用户1234 2018-11-30 16:29:12 | 只看该作者
全局:
11。30.2018  152. Maximum Product Subarray
class Solution {
    public int maxProduct(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int max_current = nums[0];
        int min_current = nums[0];
        int max = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int max_here = Math.max(Math.max(max_current * nums[i], min_current * nums[i]), nums[i]);
            int min_here = Math.min(Math.min(max_current * nums[i], min_current * nums[i]), nums[i]);

            max_current = max_here;
            min_current = min_here;

            max = Math.max(max, max_current);
        }
        return max;
    }
}

回复

使用道具 举报

🔗
 楼主| 用户1234 2018-11-30 16:46:55 | 只看该作者
全局:
用户1234 发表于 2018-11-30 16:29
11。30.2018  152. Maximum Product Subarray
class Solution {
    public int maxProduct(int[] nums)  ...

198. House Robber
lass Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int max = 0;
        int rob = 0;
        int notRob = 0;
        for (int i = 0; i < nums.length; i++) {
            int currentRob = notRob + nums[i];
            notRob = Math.max(notRob, rob);
            rob = currentRob;
        }
        return Math.max(rob, notRob);
    }
}
回复

使用道具 举报

🔗
 楼主| 用户1234 2018-11-30 17:17:43 | 只看该作者
全局:

221. Maximal Square
class Solution {
    public int maximalSquare(char[][] matrix) {
        if(matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int[][] dp = new int[matrix.length + 1][matrix[0].length + 1];
        int max = 0;
        for (int i = 1; i < matrix.length + 1; i++) {
            for (int j = 1; j < matrix[0].length + 1; j++) {
                if (matrix[i - 1][j - 1] == '1') {
                     //思考这一步怎么得出来的
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
                    max = Math.max(dp[i][j], max);
                }
               
            }
        }
        return max * max;
    }
}
回复

使用道具 举报

🔗
 楼主| 用户1234 2018-12-3 06:56:03 | 只看该作者
全局:
213. House Robber II
class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        return Math.max(rob1(nums, 0, nums.length - 1),
                        rob1(nums, 1, nums.length));
    }
    public int rob1(int[] nums, int start , int end) {
        if (start >= end) {
            return 0;
        }
        int max = nums[start];
        int previousMax = 0;
        for (int i = start + 1; i < end; i++) {
            int currnet = Math.max(max, previousMax + nums[i]);
             previousMax = max;
             max = currnet;
        }
        return max;
    }
}
回复

使用道具 举报

🔗
 楼主| 用户1234 2018-12-3 07:41:46 | 只看该作者
全局:
121. Best Time to Buy and Sell Stock
class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2) {
            return 0;
        }
        int max = 0;
        int minPrice = prices[0];
        for (int i = 1; i < prices.length; i++) {
            max = Math.max(max, prices[i] - minPrice);
            minPrice = Math.min(minPrice, prices[i]);
        }
        return max;
    }
}
回复

使用道具 举报

🔗
 楼主| 用户1234 2018-12-3 08:17:22 | 只看该作者
全局:
122. Best Time to Buy and Sell Stock II
class Solution {
    public int maxProfit(int[] prices) {
        int max = 0;
        for (int i = 1; i < prices.length; i++) {
            max = prices[i] > prices[i - 1]? max + prices[i] - prices[i - 1] : max;
        }
        return max;
    }
}
回复

使用道具 举报

🔗
wangzhuyun32 2018-12-4 00:22:18 | 只看该作者
全局:
像楼主学习!!
回复

使用道具 举报

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

本版积分规则

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