Merge pull request '合并两个有序数组' (#5) from dev into master
Reviewed-on: #5
This commit is contained in:
commit
56af701f28
53
src/main/java/com/leetcode/array/MajorityElement.java
Normal file
53
src/main/java/com/leetcode/array/MajorityElement.java
Normal file
@ -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<Integer, Integer> 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<Integer, Integer> 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);
|
||||
|
||||
}
|
||||
}
|
49
src/main/java/com/leetcode/array/MergeSortedArrays.java
Normal file
49
src/main/java/com/leetcode/array/MergeSortedArrays.java
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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 ? "存在" : "不存在");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user