【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73
2024-10-19 14:32:28
这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么???
这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里???
最近一直苦苦思考,不明所以,刷题刷得更多的感受是机械化的操作。
抽空看了以前乔布斯的演讲有点感受,经过几天的思考最终我想通了。
这里刷题是对自己思考方式的提炼,这种题写多了对自己编码的思维方式有所提升这个是无形的。
其次我没必要去这么刷题,因为题目无限,时间有限,所以应该做的是去品味每一道题,思考如果以后遇到同类的题,我应该从一个什么样的角度去思考,明白如何去实现。
所以要时刻总结,不能题海,还要有质量,只有质量和数量都上去了,才能见识正在的威力,单纯求质量也是不够的,这个度要自己把握了!!!
加油
今日份的leetcode
package y2019.Algorithm.array.medium; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: FindPeakElement
* @Author: xiaof
* @Description: TODO 162. Find Peak Element
* A peak element is an element that is greater than its neighbors.
* Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
* The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
* You may imagine that nums[-1] = nums[n] = -∞.
*
* 峰值元素是指其值大于左右相邻值的元素。
* 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。
* 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
* 你可以假设 nums[-1] = nums[n] = -∞。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/find-peak-element
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
* Input: nums = [1,2,3,1]
* Output: 2
* Explanation: 3 is a peak element and your function should return the index number 2.
*
* @Date: 2019/7/24 8:57
* @Version: 1.0
*/
public class FindPeakElement { public int solution(int[] nums) { if(nums == null || nums.length <= 0) {
return -1;
} if(nums.length == 1) {
return 0;
} //二分查找局部最大值
int l = 0, r = nums.length - 1;
int res = -1; while (l <= r) {
int mid = (l + r) / 2;
//判断mid是否锋值,比之前的数据和之后的数据都要大,如果是边缘数据,那么就只要比较一边就行
int left = (mid - 1) < 0 ? Integer.MIN_VALUE : nums[mid - 1];
int right = (mid + 1) >= nums.length ? Integer.MIN_VALUE : nums[mid + 1];
if(nums[mid] > left && nums[mid] > right) {
res = mid;
return res;
} else if (left > nums[mid]) {
//左边比较大,我们往左边靠
r = mid - 1;
} else {
l = mid + 1;
}
} return res;
}
}
package y2019.Algorithm.array.medium; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array.medium
* @ClassName: SetZeroes
* @Author: xiaof
* @Description: TODO 73. Set Matrix Zeroes
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
*
* Input:
* [
* [1,1,1],
* [1,0,1],
* [1,1,1]
* ]
* Output:
* [
* [1,0,1],
* [0,0,0],
* [1,0,1]
* ]
*
* A straight forward solution using O(mn) space is probably a bad idea.
* A simple improvement uses O(m + n) space, but still not the best solution.
* Could you devise a constant space solution?
*
* @Date: 2019/7/24 9:44
* @Version: 1.0
*/
public class SetZeroes { public void solution(int[][] matrix) {
//这题要求空间复杂度小于O(m+n)
//如果是这个要求的话,双循环直接再原来的矩阵中操作
//1.首先吧矩阵的边上的位置应该为0的设置为0
int c = 1;
for(int i = 0; i < matrix.length; ++i) {
if(matrix[i][0] == 0) c = 0; //如果本身这个位置边上就是0
for(int j = 1; j < matrix[i].length; ++j) {
//设置边为0
if(matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
//2.再次遍历矩阵,只要这个i,j对应的值的i,0或者0,j为0,那么就设置为0
//因为设置为0的是上面和左边,那么我们从右下开始遍历
for(int i = matrix.length - 1; i >= 0; --i) {
for(int j = matrix[i].length - 1; j >= 1; --j) { //第一列不用遍历,因为已经修改过了
//设置边为0
if(matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
//最后一列单独判断,避免重复吧变化了之后的数据再次操作
if(c == 0) {
//c标识这个列上本身存在0
matrix[i][0] = 0;
}
} } }
最新文章
- linux 乱码
- java多线程学习
- Latex常用指令学习
- ubuntu12.04_命令
- html 中根据后台参数显示 相应的样式 EL表达式
- Swift构造器重载
- 【转】经典SQL语句大全
- js获取本月第几周和本年第几周
- Rechability的简单使用
- (中等) POJ 2948 Martian Mining,DP。
- js中的3种弹出式消息提醒(警告窗口,确认窗口,信息输入窗口)的命令式
- Color.js 增强你对颜色的控制
- ThinkPHP 整合 PHPExcel ,数据导出功能实现,解决Invalid cell coordinate
- SpriteBuilder实际操作中如何确定合适Breaking force的值
- Numpy库的学习(二)
- liunx驱动----异步通知
- Angularjs的那些事 – 视图的生命周期
- Servlet基础学习
- Interactive Reporting , SQL*Net not loaded successfully 问题的解决。
- Linux下运行crm项目