From 0210bf6b49a5b19d5f6c45646339bf9a40047526 Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 29 Nov 2020 16:46:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A4=9A=E6=95=B0=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leetcode/array/MajorityElement.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/main/java/com/leetcode/array/MajorityElement.java diff --git a/src/main/java/com/leetcode/array/MajorityElement.java b/src/main/java/com/leetcode/array/MajorityElement.java new file mode 100644 index 0000000..703f388 --- /dev/null +++ b/src/main/java/com/leetcode/array/MajorityElement.java @@ -0,0 +1,53 @@ +package com.leetcode.array; + +import java.util.HashMap; +import java.util.Map; +/** + * 多数元素 +给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。 + +你可以假设数组是非空的,并且给定的数组总是存在多数元素。 + +示例 1: + +输入: [3,2,3] +输出: 3 +示例 2: + +输入: [2,2,1,1,1,2,2] +输出: 2 + +作者:力扣 (LeetCode) +链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xm77tm/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + */ +public class MajorityElement { + + public int majorityElement(int[] nums) { + Map map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + Integer counter = map.getOrDefault(nums[i], 0); + counter ++; + map.put(nums[i], counter); + } + for (Map.Entry entry: map.entrySet()) { + if (entry.getValue() > nums.length/2) { + return entry.getKey(); + } + } + return 0; + } + + public static void main(String[] args) { + MajorityElement majorityElement = new MajorityElement(); + + int[] nums = {2,2,1,1,1,2,2}; + + int res = majorityElement.majorityElement(nums); + + System.out.println(res); + + } +} -- 2.45.2 From a8e22f4c775dd1b41b72d9851f7b009840466afb Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 29 Nov 2020 17:18:18 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E4=BA=8C=E7=BB=B4?= =?UTF-8?q?=E7=9F=A9=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/simple/SearchSortedMatrix.java | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/leetcode/simple/SearchSortedMatrix.java b/src/main/java/com/leetcode/simple/SearchSortedMatrix.java index ddd755c..62f2b26 100644 --- a/src/main/java/com/leetcode/simple/SearchSortedMatrix.java +++ b/src/main/java/com/leetcode/simple/SearchSortedMatrix.java @@ -1,9 +1,18 @@ package com.leetcode.simple; /** - * @apiNote 写出一个高效的算法来搜索 m × n矩阵中的值。这个矩阵具有以下特性: - * 每行中的整数从左到右是排序的。 - * 每行的第一个数大于上一行的最后一个整数。 + * 搜索二维矩阵 + * + 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性: + +每行的元素从左到右升序排列。 +每列的元素从上到下升序排列。 + +作者:力扣 (LeetCode) +链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xmlwi1/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + * @author zeekling * @version 1.0 * @since 2019-12-14 @@ -33,4 +42,18 @@ public class SearchSortedMatrix { } return false; } + + public static void main(String[] args) { + SearchSortedMatrix searchSortedMatrix = new SearchSortedMatrix(); + int[][] matrix = { + {1,4,7,11,15}, + {2,5,8,12,19}, + {3,6,9,16,22}, + {10,13,14,17,24}, + {18,21,23,26,30} + }; + int target = 5; + boolean res = searchSortedMatrix.searchMatrix(matrix, target); + System.out.println(res ? "存在" : "不存在"); + } } -- 2.45.2 From 230234c0320406cf4d3a97bc4ce923fa4076376f Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 29 Nov 2020 22:45:26 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=90=88=E5=B9=B6=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=9C=89=E5=BA=8F=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/leetcode/array/MergeSortedArrays.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/com/leetcode/array/MergeSortedArrays.java diff --git a/src/main/java/com/leetcode/array/MergeSortedArrays.java b/src/main/java/com/leetcode/array/MergeSortedArrays.java new file mode 100644 index 0000000..bc19eae --- /dev/null +++ b/src/main/java/com/leetcode/array/MergeSortedArrays.java @@ -0,0 +1,49 @@ +package com.leetcode.array; + +import java.util.Arrays; + +/** + * 合并两个有序数组 +给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 nums1 成为一个有序数组。 + +说明: + +初始化 nums1 和 nums2 的元素数量分别为 m 和 n 。 +你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素。 + +示例: + +输入: +nums1 = [1,2,3,0,0,0], m = 3 +nums2 = [2,5,6], n = 3 + +输出:[1,2,2,3,5,6] + +作者:力扣 (LeetCode) +链接:https://leetcode-cn.com/leetbook/read/top-interview-questions/xmi2l7/ +来源:力扣(LeetCode) +著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 + */ +public class MergeSortedArrays { + + public void merge(int[] nums1, int m, int[] nums2, int n) { + for (int i = 0;i < n; i++ ) { + nums1[m + i] = nums2[i]; + } + Arrays.sort(nums1); + } + + public static void main(String[] args) { + MergeSortedArrays mArrays = new MergeSortedArrays(); + int[] nums1 = {1,2,3,0,0,0}; + int[] nums2 = {2,5,6}; + int m = 3; + int n = 3; + mArrays.merge(nums1, m, nums2, n); + for (int i = 0; i < nums1.length; i++) { + System.out.print(nums1[i] + ","); + } + System.out.println(); + } + +} -- 2.45.2