LeetCode
Jae_Wang
~
展开
-
LeetCode718 — Maximum Length of Repeated Subarray
题目:求两个数组的最大公共子串思路:典型的dp问题,状态转移是当两个相等就加1,压缩版和不压缩版写法都差不多~public class Solution718 { public static int findLength(int[] A, int[] B) { int m = A.length; int n = B.length; ...原创 2018-07-14 22:50:21 · 314 阅读 · 0 评论 -
LeetCode695 — Max Area of Island
题目:给一个二维数组,1代表内陆,求相互连接的内陆最大的面积思路:easy~ dfs或bfs都能做出来,争取一次ACpublic class Solution { private int[][] d = {{-1,0},{0,1},{1,0},{0,-1}}; private boolean[][] visited; private int rows, c...原创 2018-07-14 22:50:05 · 377 阅读 · 0 评论 -
LeetCode136 — Single Number
题目:只有一个数是单个出现的,求这个数思路:位运算,异或两个相同的异或结果是0public class Solution136 { public int singleNumber(int[] nums){ int result = 0; for(int i = 0; i < nums.length; i++){ ...原创 2018-07-13 19:02:56 · 212 阅读 · 0 评论 -
LeetCode448 — Find All Numbers Disappeared in an Array
题目:在一个数组找出未出现的数字思路:经过前几题的洗礼,一看到n个数中有1-n个数就应该能知道用索引对应数值的方法区解题了 遍历一遍,如果第一次能到的话,则将其负,这里不管重复,题目只要求输出没有的值,那么遍历一遍之后还有正数的话,说明这个点的索引到不了,没有这个值能到这个索引。public class Solution448 { public Lis...原创 2018-07-13 19:02:26 · 201 阅读 · 0 评论 -
LeetCode442 — Find All Duplicates in an Array
题目:和287类似,只不过这次出现重复的数字不只是一个,是多个。思路:如果可以开辟空间的话很简单,不能开辟空间的话,稍微思考一下还是有思路的。 和287不同的是,这道题没有规定数组不能够修改,那么就可以访问这个数字为标记一下是否访问过。public class Solution442 { public List<Integer> f...原创 2018-07-11 22:09:29 · 339 阅读 · 0 评论 -
LeetCode142 — Linked List Cycle II
题目: 链表判断是否有环,并找到入口public class Solution142 { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head; boolean flag = false; w...原创 2018-07-11 22:09:09 · 223 阅读 · 0 评论 -
LeetCode287 — Find the Duplicate Number
题目: 找出一个重复的值,要去不能开辟空间,并且不能暴力解思路:快慢指针,两者相遇是在一个环中,而再依次递增就在切面点相遇了。证明这里省略(证明想了一上午,终于写出来了)~public class Solution287 { public int findDuplicate(int[] nums) { int slow = 0; int f...原创 2018-07-11 22:08:43 · 239 阅读 · 0 评论 -
LeetCode556 — Reshape the Matrix
题目: 将一个m*n的矩阵替换成r*c的矩阵,当不满足的话,返回原矩阵。思路:easy~ 用队列将所有的数字装起来,再依次取出来放入新数组。不用额外空间的话,就是所有的数字除以,求余列数得到的就是几行几列~class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { ...原创 2018-07-10 23:39:49 · 294 阅读 · 0 评论 -
LeetCode080 — Remove Duplicates from Sorted ArrayII
题目:和上题26一样,只是每个数出现次数不超过2次。思路:同理,有一个索引index,当超过了2,就不赋值了,跳过,否则就赋值,并且将count重置。public class Solution080 { public int removeDuplicates(int[] nums){ int index = 1; int count = 1;...原创 2018-07-10 23:39:23 · 701 阅读 · 0 评论 -
LeetCode026 — Remove Duplicates from Sorted Array
问题:给一个有序数组,移除重复的元素,要求原地思路:给定一个索引, 当index等于前一个,不动,否则就赋值往前移~public int removeDuplicates(int[] nums) { if(nums.length < 2) return 1; int index = 1; for(int i = 1; i &l...原创 2018-07-10 23:38:48 · 233 阅读 · 0 评论 -
LeetCode027 — Remove Element
题目大意: 给一个数组,指定一个值让其移到后面去,其他的按顺序移到前面,并返回移除指定值剩余数的个数~比较easy,遍历一遍,等于指定的不走,不等于就赋值往后走~public class Solution027 { public int removeElement(int[] nums, int val) { int index = 0; ...原创 2018-07-08 23:27:46 · 290 阅读 · 0 评论 -
LeetCode283 — Move Zeroes
思路:原地排,将0排到数组的后面easy,直接遍历一遍,一个index遇到0就不动,遇到非0就赋值,并且index++public class Solution283 { public void moveZeroes(int[] nums) { int index = 0; for(int i = 0; i < nums.length; ...原创 2018-07-08 23:25:56 · 654 阅读 · 0 评论 -
LeetCode034 — Search for a Range
思路:首先数组排好序,简单的思路就是从两侧指针依次向内移动,直到找到相同的为止。上述的复杂度为O(n),没达到题目要求O(lgn)就只能用二分查找了, 这样才满足复杂度。class Solution { public int[] searchRange(int[] nums, int target) { int l = 0; int...原创 2018-07-08 23:25:15 · 644 阅读 · 0 评论 -
股票问题(二)
一、 Best Time to Buy and Sell Stock IISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many ...原创 2018-04-20 10:40:55 · 738 阅读 · 0 评论 -
leetcode342
设计模式原创 2018-07-15 23:33:51 · 380 阅读 · 0 评论 -
LeetCode728 — Self Dividing Numbers
题目: 求这个数能被每个位数整数思路:会求一个数的各个位数,然后相除就可以了public class Solution { public List<Integer> selfDividingNumbers1(int left, int right) { List<Integer> res = new ArrayList<...原创 2018-07-15 23:34:05 · 281 阅读 · 0 评论 -
leetcode123
31原创 2018-07-15 23:34:21 · 876 阅读 · 0 评论 -
股票问题(一)
一、 Best Time to Buy and Sell Stockeg:只能买卖一次股票,求最大利润Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transact...原创 2018-04-19 23:36:13 · 959 阅读 · 0 评论