leetcode数组遍历技巧
2016-12-08 20:24
746 查看
1、Container With Most Water
链接:https://leetcode.com/problems/container-with-most-water/
思路:从数组头尾向中间移动。取较小的那边移动,因为隔板盛水的最大值,在距离极可能大的情况下,另一边>=当前高度 时取得。
2、Two Sum
链接:https://leetcode.com/problems/two-sum/
思路:引入map,缩短时间
3、3Sum
链接:https://leetcode.com/problems/3sum/
思路:先排序;确定一个数,遍历时,头尾开始,中途跳过一些数
4、3Sum Closest
链接:https://leetcode.com/problems/3sum-closest/
思路:排序,确定一个数,头尾开始遍历
4、4Sum
链接:https://leetcode.com/problems/4sum/
思路:先排序,与前面思路一样,固定两个,头尾开始遍历,注意不能重复,可以使用set避免重复,或者通过跳过相同值避免重复。
5、Set Matrix Zeroes
链接:https://leetcode.com/problems/set-matrix-zeroes/
思路:先遍历第一行,第一列是否有零,保存标识;将其余行、列存在的0,保存在第一行、第一列中,然后遍历第一行、第一列;最后处理先前保存的标识。
链接:https://leetcode.com/problems/container-with-most-water/
思路:从数组头尾向中间移动。取较小的那边移动,因为隔板盛水的最大值,在距离极可能大的情况下,另一边>=当前高度 时取得。
public int maxArea(int[] height) { int len = height.length,area = 0, i= 0, j = len - 1; while(i < j){ area = area > Math.min(height[i], height[j]) *(j - i) ? area : Math.min(height[i], height[j]) *(j - i); if(height[i] > height[j]) j--; else i++; } return area; }
2、Two Sum
链接:https://leetcode.com/problems/two-sum/
思路:引入map,缩短时间
public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; int i = 0; HashMap<Integer,Integer> map = new HashMap<>(); for(int j : nums){ map.put(j,i++); } for(int j = 0; j < nums.length; j++){ int temp = target - nums[j]; if(map.containsKey(temp) && map.get(temp) != j){ result[0] = j; result[1] = map.get(temp); return result; } } return result; }
3、3Sum
链接:https://leetcode.com/problems/3sum/
思路:先排序;确定一个数,遍历时,头尾开始,中途跳过一些数
public List<List<Integer>> threeSum(int[] nums) { int len = nums.length; Arrays.sort(nums); List<List<Integer>> list = new ArrayList<>(); int i, j, k; for(i = 0; i < len - 2; i++){ j = i+1; k = len -1; while(j < k){ if(nums[i] + nums[j] + nums[k] == 0){ List<Integer> listnew = new ArrayList<>(); listnew.add(nums[i]); listnew.add(nums[j]); listnew.add(nums[k]); list.add(listnew); j++; k--; while(j < k && nums[j] == nums[j-1]){ j++; } while(k > j && nums[k] == nums[k+1]){ k--; } }else if(nums[i] + nums[j] + nums[k] > 0){ k--; }else j++; while(i < len-2 && nums[i] == nums[i+1]){ i++; } } } return list; }
4、3Sum Closest
链接:https://leetcode.com/problems/3sum-closest/
思路:排序,确定一个数,头尾开始遍历
public int threeSumClosest(int[] nums, int target) { Arrays.sort(nums); int len = nums.length; int a = Math.abs(nums[0] + nums[1] + nums[2] - target); int sum, n, j, k, result = nums[0] + nums[1] + nums[2]; for(int i = 0; i < len -2; i++){ j = i + 1; k = len - 1; while(j < k){ sum = nums[i] + nums[j] + nums[k]; if(sum == target){ return sum; }else if(sum > target){ k--; }else{ j++; } n = Math.abs(sum-target); if(n < a){ a = n; result = sum; } } } return result; }
4、4Sum
链接:https://leetcode.com/problems/4sum/
思路:先排序,与前面思路一样,固定两个,头尾开始遍历,注意不能重复,可以使用set避免重复,或者通过跳过相同值避免重复。
public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); int len = nums.length; List<List<Integer>> list = new ArrayList<>(); for(int i = 0; i < len - 3; i++){ for(int j = i + 1; j < len - 2; j++){ int p = j + 1, q = len - 1; while( p < q ){ if(nums[i] + nums[j] + nums[p] + nums[q] < target){ p++; }else if(nums[i] + nums[j] + nums[p] + nums[q] > target){ q--; }else{ List<Integer> list1 = new ArrayList<>(); list1.add(nums[i]); list1.add(nums[j]); list1.add(nums[p]); list1.add(nums[q]); list.add(list1); do{ p++; }while( p < q && nums[p] == nums[p-1]); do{ q--; }while( p < q && nums[q] == nums[q+1]); } } while( j < len - 2 && nums[j] == nums[j+1]){ j++; } } while( i < len - 3 && nums[i] == nums[i+1]){ i++; } } return list; }
public List<List<Integer>> fourSum(int[] nums, int target) { Arrays.sort(nums); int len = nums.length; HashSet<List<Integer>> set = new HashSet<>(); for(int i = 0; i < len - 3; i++){ for(int j = i + 1; j < len - 2; j++){ int p = j + 1, q = len - 1; while( p < q ){ if(nums[i] + nums[j] + nums[p] + nums[q] == target){ List<Integer> list1 = new ArrayList<>(); list1.add(nums[i]); list1.add(nums[j]); list1.add(nums[p]); list1.add(nums[q]); set.add(list1); p++; q--; }else if(nums[i] + nums[j] + nums[p] + nums[q] > target){ q--; }else p++; } } } return new ArrayList<>(set); }
5、Set Matrix Zeroes
链接:https://leetcode.com/problems/set-matrix-zeroes/
思路:先遍历第一行,第一列是否有零,保存标识;将其余行、列存在的0,保存在第一行、第一列中,然后遍历第一行、第一列;最后处理先前保存的标识。
public void setZeroes(int[][] matrix) { if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return; int leni = matrix.length, lenj = matrix[0].length; boolean zerorow = false, zerocol = false; for(int i = 0; i < leni; i++) if(matrix[i][0] == 0){ zerocol = true; break; } for(int j = 0; j < lenj; j++) if(matrix[0][j] == 0){ zerorow = true; break; } for(int i = 1; i < leni; i++) for(int j = 1; j < lenj; j++){ if(matrix[i][j] == 0){ matrix[0][j] = 0; matrix[i][0] = 0; } } for(int i = 1; i < leni; i++) for(int j = 1; j < lenj; j++){ if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; } if(zerorow){ for(int j= 0; j < lenj; j++) matrix[0][j] = 0; } if(zerocol){ for(int i= 0; i < leni; i++) matrix[i][0] = 0; } }
相关文章推荐
- Javascript技巧之不要用for in语句对数组进行遍历
- Javascript的数组与字典用法与遍历对象的属性技巧
- leetcode 697. Degree of an Array 等于数组的阶的最短的数组 + 遍历即可
- js奇淫技巧之遍历数组
- 【神奇的技巧】leetcode - 661. Image Smoother【整数位信息使用 + 二维数组边界考虑 + 数组的巧妙使用】
- leetcode -- 58. Length of Last Word【遍历数组的次序:前后】
- Javascript的数组与字典用法与遍历对象的属性技巧
- CCArray数组或者vector容器元素在遍历时删除的删除技巧
- [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法
- 《Java 编程技巧1001条》 第388条: 遍历多维数组
- LeetCode 遍历技巧 | 15. 3Sum
- Javascript技巧之不要用for in语句对数组进行遍历
- 数组遍历技巧
- Longest Increasing Subsequence(最长增长子数列)-LeetCode关于数组的思路和技巧
- LeetCode 遍历技巧 | 18. 4Sum
- LeetCode | 448. Find All Numbers Disappeared in an Array 数组技巧题
- Javascript技巧之不要用for in语句对数组进行遍历
- Javascript技巧之不要用for in语句对数组进行遍历
- Javascript技巧-不要用for in语句对数组进行遍历
- Javascript技巧之不要用for in语句对数组进行遍历