1.盛最多水的容器
11. 盛最多水的容器
已解答
中等
相關標簽
相關企業
提示
給定一個長度為?n
?的整數數組?height
?。有?n
?條垂線,第?i
?條線的兩個端點是?(i, 0)
?和?(i, height[i])
?。
找出其中的兩條線,使得它們與?x
?軸共同構成的容器可以容納最多的水。
返回容器可以儲存的最大水量。
說明:你不能傾斜容器。
示例 1:
輸入:[1,8,6,2,5,4,8,3,7] 輸出:49 解釋:圖中垂直線代表輸入數組 [1,8,6,2,5,4,8,3,7]。在此情況下,容器能夠容納水(表示為藍色部分)的最大值為?49。
示例 2:
輸入:height = [1,1] 輸出:1
提示:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
public static void main(String[] args) {int[] nums = {1,8,6,2,5,4,8,3,7};int maxArea = maxArea3(nums);System.out.println(maxArea);}public static int maxArea3(int[] height) {int left = 0, right = height.length - 1;//左指針,右指針int res = 0;//maxAreawhile(left < right){//左指針<右指針int w = right - left;//xint h = Math.min(height[left], height[right]);//yres = Math.max(res, w * h);while(left < right && height[left] <= h) left++;//移動左指針,尋找比當前y值更大的y值while(left < right && height[right] <= h) right--;//移動右指針,尋找比當前y值更大的y值}return res;}//1-my(2)public static int maxArea2(int[] height) {int maxArea = 0;for (int i = 0; i < height.length; i++) {//左指針for (int j = height.length-1; j >= 0 && j>=i ; j--) {//右指針int x = j - i;int y = Math.min(height[i],height[j]);int area = x * y;maxArea = Math.max(maxArea,area);if (height[i] < height[j]){break;}}}return maxArea;}//1-mypublic static int maxArea(int[] height) {int maxArea = 0;for (int i = height.length-1; i >= 0 ; i--) {//x軸大小for (int j = 0; j < height.length; j++) {//indexif (j+i<height.length) {int left = height[j];int right = height[j + i];int min = Math.min(left, right);int area = min * i;maxArea = Math.max(maxArea,area);}}}return maxArea;}
2.三數之和
15. 三數之和
給你一個整數數組?nums
?,判斷是否存在三元組?[nums[i], nums[j], nums[k]]
?滿足?i != j
、i != k
?且?j != k
?,同時還滿足?nums[i] + nums[j] + nums[k] == 0
?。請你返回所有和為?0
?且不重復的三元組。
注意:答案中不可以包含重復的三元組。
示例 1:
輸入:nums = [-1,0,1,2,-1,-4] 輸出:[[-1,-1,2],[-1,0,1]] 解釋: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。 nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。 nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。 不同的三元組是 [-1,0,1] 和 [-1,-1,2] 。 注意,輸出的順序和三元組的順序并不重要。
示例 2:
輸入:nums = [0,1,1] 輸出:[] 解釋:唯一可能的三元組和不為 0 。
示例 3:
輸入:nums = [0,0,0] 輸出:[[0,0,0]] 解釋:唯一可能的三元組和為 0 。
提示:
3 <= nums.length <= 3000
-105 <= nums[i] <= 105
my(1-3)
public static List<List<Integer>> threeSum3(int[] nums) {Arrays.sort(nums);Set<List<Integer>> temp = new HashSet<>();for (int i = 0; i < nums.length && nums[i] <= 0 ; i++) {for (int j = i+1; j < nums.length; j++) {for (int k = j+1; k < nums.length; k++) {if (i!=j && j!=k && k!= i && nums[i]+ nums[j]+nums[k]==0 ){List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[j]);inner.add(nums[k]);temp.add(inner);}}}}return new ArrayList<>(temp);}public static List<List<Integer>> threeSum2(int[] nums) {Set<List<Integer>> temp = new HashSet<>();for (int i = 0; i < nums.length; i++) {for (int j = 0; j < nums.length; j++) {for (int k = 0; k < nums.length; k++) {if (i!=j && j!=k && k!= i && nums[i]+ nums[j]+nums[k]==0 ){List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[j]);inner.add(nums[k]);inner = inner.stream().sorted().collect(Collectors.toList());temp.add(inner);}}}}return new ArrayList<>(temp);}public static List<List<Integer>> threeSum(int[] nums) {//請你返回所有和為 0 且不重復的三元組 的下標組合List<List<Integer>> res = new ArrayList<>();Map<Integer,List<Integer>> map = new HashMap<>();for (int left = 0; left < nums.length; left++) {for (int right = nums.length - 1; right >=0; right--) {int temp = 0 - nums[left] - nums[right];List<Integer> mapOrDefault = map.getOrDefault(temp, new ArrayList<>());if (mapOrDefault.size()==0){mapOrDefault.add(left);mapOrDefault.add(right);map.put(temp,mapOrDefault);}}}for (int i = 0; i < nums.length; i++) {List<Integer> mapOrDefault = map.getOrDefault(nums[i], new ArrayList<>());if (mapOrDefault.size()==2){mapOrDefault.add(i);int size = mapOrDefault.stream().collect(Collectors.toSet()).size();if (size==3) {res.add(mapOrDefault);}}}return res;}
others-1
public static void main(String[] args) {int[] nums1 = {-1,0,1,-2,0,2,-2,-1,0};int[] nums2 = {0,0,0};int[] nums = {-1,0,1,2,-1,-4,-2,-3,3,0,4};List<List<Integer>> threeSum = findTriplets(nums);System.out.println(threeSum);}public static List<List<Integer>> findTriplets(int[] nums) {Set<List<Integer>> result = new HashSet<>(); // 用于去重Arrays.sort(nums); // 先排序數組,方便去重和雙指針for (int i = 0; i < nums.length - 2; i++) {if (i > 0 && nums[i] == nums[i - 1]) continue; // 跳過重復的 iint left = i + 1;int right = nums.length - 1;while (left < right) {//i!=j && j!=k && k!= iint sum = nums[i] + nums[left] + nums[right];if (sum == 0) { // 三數之和為 0,nums[i]+ nums[j]+nums[k]==0List<Integer> inner = new ArrayList<>();inner.add(nums[i]);inner.add(nums[left]);inner.add(nums[right]);result.add(inner); // 自動去重left++;right--;} else if (sum < 0) {left++;} else {right--;}}}return new ArrayList<>(result);}
others-2
public static List<List<Integer>> findTriplets2(int[] nums) {return new AbstractList<List<Integer>>() {// Declare List of List as a class variableprivate List<List<Integer>> list;// Implement get method of AbstractList to retrieve an element from the listpublic List<Integer> get(int index) {// Call initialize() methodinitialize();// Return the element from the list at the specified indexreturn list.get(index);}// Implement size method of AbstractList to get the size of the listpublic int size() {// Call initialize() methodinitialize();// Return the size of the listreturn list.size();}// Method to initialize the listprivate void initialize() {// Check if the list is already initializedif (list != null)return;// Sort the given arrayArrays.sort(nums);// Create a new ArrayListlist = new ArrayList<>();// Declare required variablesint l, h, sum;// Loop through the arrayfor (int i = 0; i < nums.length; i++) {// Skip the duplicatesif (i != 0 && nums[i] == nums[i - 1])continue;// Initialize l and h pointersl = i + 1;h = nums.length - 1;// Loop until l is less than hwhile (l < h) {// Calculate the sum of three elementssum = nums[i] + nums[l] + nums[h];// If sum is zero, add the triple to the list and update pointersif (sum == 0) {list.add(getTriple(nums[i], nums[l], nums[h]));l++;h--;while (l < h && nums[l] == nums[l - 1])l++;while (l < h && nums[h] == nums[h + 1])h--;} else if (sum < 0) {// If sum is less than zero, increment ll++;} else {// If sum is greater than zero, decrement hh--;}}}}};}private static List<Integer> getTriple(int i, int j, int k){return new AbstractList<Integer>() {private int[] data;// Constructor to initialize the triple with three integers// Method to initialize the listprivate void initialize(int i, int j, int k) {if (data != null)return;data = new int[] { i, j, k };}// Implement get method of AbstractList to retrieve an element from the triplepublic Integer get(int index) {// Call initialize() methodinitialize(i, j, k);return data[index];}// Implement size method of AbstractList to get the size of the triplepublic int size() {// Call initialize() methodinitialize(i, j, k);return 3;}};}