NO1 两个有序数组的交集,时间复杂度O(m+n)
NO2 给出多个pair数组,合并有重叠的区间,并输出最大区间的pair
NO3 求数组中第k大的数字
NO4 两个有序数组的中位数,要求时间复杂度log(len1+len2)
NO5 递增排序数组,后半段移到前半段,求转折点的数值
NO6 二维有序数组(从左到右是升序,从上到下是升序)的二分查找
NO1 两个有序数组的交集,时间复杂度O(m+n)
# 拼多多算法面试题
# 求两个已经排好序的数组的交集,要求算法复杂度O(m+n)
# 不能使用两个 for 循环,算法复杂度为O(m*n)
# 我当时的解决思路:使用 in 来做,但 in 的复杂度是 O(n),总的复杂度还是O(m*n),但这个不用事先排好序
int mixed(int arr1[], int n1, int arr2[], int n2, int* mix)
{
int i = 0;
int j = 0;
int k = 0;
//mix = new int[n1];
while (i < n1 && j < n2)
{
if (arr1[i] == arr2[j])
{
mix[k++] = arr1[i];
//k++;
i++;
j++;
}
else if (arr1[i]>arr2[j])
j++;
else if (arr1[i]<arr2[j])
i++;
}
return k;
}
int main() {
int *m = NULL; //存储相同元素的指针
int num;//存储两个数组相同元素个数
int a[] = { 0, 1, 2, 3, 4 };
int b[] = { 1, 3, 5, 7, 9 };
int len1 = sizeof(a) / sizeof(a[0]);
int len2 = sizeof(b) / sizeof(b[0]);
m = new int[len1];
num = mixed(a, len1, b, len2, m);
printf("两数组相同元素个数: %d\n", num);
printf("相同元素为:\n");
for (int i = 0; i < num; i++)
printf("%d \t", m[i]);
system("pause");
return 0;
}
NO2 给出多个pair数组,合并有重叠的区间,并输出最大区间的pair
示例:
有 [1,3],[2,6],[8,10],[15,18],
返回 [1,6],[8,10],[15,18].
方法1: 别人做法,将所有合并的子集输出来,更简便
typedef pair<int, int> P;
bool compare(P A, P B)
{
if (A.first < B.first)
return true;
else
return false;
}
vector<P> merge(vector<P> arr)
{
vector<P> result;
if (arr.empty()){
return result;
}
sort(arr.begin(), arr.end(), compare);
int i = 0, k = 0;
result.push_back(arr[0]);
for (int i = 1; i < arr.size(); i++)
{
if (arr[i].first>result.back().second){
result.push_back(arr[i]);
}
else{
result.back().second = max(result.back().second, arr[i].second);
}
}
return result;
}
int main() {
vector<P> vec, allP;
vec.push_back(make_pair(1, 3));
vec.push_back(make_pair(2, 6));
vec.push_back(make_pair(8, 10));
vec.push_back(make_pair(15, 18));
vector<P> result = merge(vec);
system("pause");
return 0;
}
方法2 java版本
package com.test.test1;
import java.util.*;
public class TwoSort {
private static ArrayDeque<TwoTuple> sort(TwoTuple[] list) {
ArrayList<TwoTuple> lis = new ArrayList<TwoTuple>(Arrays.asList(list));
Collections.sort(lis, new Comparator<TwoTuple>() {
@Override
public int compare(TwoTuple o1, TwoTuple o2) {
return o1.getVal1() - o2.getVal1();
}
});
ArrayList<TwoTuple> twoTuples = new ArrayList<>();
ArrayDeque<TwoTuple> res = new ArrayDeque<>();
res.addFirst(lis.get(0));
for(int i =1;i<lis.size();i++){
if(res.getLast().getVal2()<lis.get(i).getVal1()){
res.add(lis.get(i));
}else{
TwoTuple twoTuple = res.pollLast();
TwoTuple twoTuple1 = new TwoTuple(twoTuple.getVal1(), Math.max(twoTuple.getVal2(), lis.get(i).getVal2()));
res.addLast(twoTuple1);
}
}
return res;
}
public static void main(String[] args) {
TwoTuple[] twoTuples = new TwoTuple[5];
twoTuples[0] = new TwoTuple(1,3);
twoTuples[1] = new TwoTuple(2,6);
twoTuples[2] = new TwoTuple(8,10);
twoTuples[3] = new TwoTuple(15,28);
twoTuples[4] = new TwoTuple(20,28);
ArrayDeque<TwoTuple> res = sort(twoTuples);
while(!res.isEmpty()){
System.out.println(res.pollFirst());
}
}
}
class TwoTuple {
int val1;
int val2;
public TwoTuple(int val1, int val2) {
this.val1 = val1;
this.val2 = val2;
}
public int getVal1() {
return val1;
}
public void setVal1(int val1) {
this.val1 = val1;
}
public int getVal2() {
return val2;
}
public void setVal2(int val2) {
this.val2 = val2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "TwoTuple{" +
"val1=" + val1 +
", val2=" + val2 +
'}';
}
}
NO3 求数组中第k大的数字
java 版本
package com.lzg.flume.intercepter.suanfati;
public class Finder {
public int findKth(int[] a, int n, int K) {
// write code here
return findK(a, 0, n - 1, K);
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[left];
while (left < right) {
while (left < right && arr[right] <= pivot) {
right--;
}
if (left < right) {
arr[left++] = arr[right];
}
while (left < right && arr[left] >= pivot) {
left++;
}
if(left<right){
arr[right--] = arr[left];
}
}
arr[left] = pivot;
return left;
}
public static int findK(int[] arr, int left, int right, int k) {
if (left <= right) {
int pivot = partition(arr, left, right);
if (pivot == k - 1) {
return arr[pivot];
} else if (pivot < k - 1) {
return findK(arr, pivot + 1, right, k);
} else {
return findK(arr, left, pivot - 1, k);
}
}
return -1;
}
}
NO4 求两个有序数组中位数
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int length1 = nums1.length, length2 = nums2.length;
int totalLength = length1 + length2;
if (totalLength % 2 == 1) {
int midIndex = totalLength / 2;
double median = getKthElement(nums1, nums2, midIndex + 1);
return median;
} else {
int midIndex1 = totalLength / 2 - 1, midIndex2 = totalLength / 2;
double median = (getKthElement(nums1, nums2, midIndex1 + 1) + getKthElement(nums1, nums2, midIndex2 + 1)) / 2.0;
return median;
}
}
public int getKthElement(int[] nums1, int[] nums2, int k) {
/* 主要思路:要找到第 k (k>1) 小的元素,那么就取 pivot1 = nums1[k/2-1] 和 pivot2 = nums2[k/2-1] 进行比较
* 这里的 "/" 表示整除
* nums1 中小于等于 pivot1 的元素有 nums1[0 .. k/2-2] 共计 k/2-1 个
* nums2 中小于等于 pivot2 的元素有 nums2[0 .. k/2-2] 共计 k/2-1 个
* 取 pivot = min(pivot1, pivot2),两个数组中小于等于 pivot 的元素共计不会超过 (k/2-1) + (k/2-1) <= k-2 个
* 这样 pivot 本身最大也只能是第 k-1 小的元素
* 如果 pivot = pivot1,那么 nums1[0 .. k/2-1] 都不可能是第 k 小的元素。把这些元素全部 "删除",剩下的作为新的 nums1 数组
* 如果 pivot = pivot2,那么 nums2[0 .. k/2-1] 都不可能是第 k 小的元素。把这些元素全部 "删除",剩下的作为新的 nums2 数组
* 由于我们 "删除" 了一些元素(这些元素都比第 k 小的元素要小),因此需要修改 k 的值,减去删除的数的个数
*/
int length1 = nums1.length, length2 = nums2.length;
int index1 = 0, index2 = 0;
int kthElement = 0;
while (true) {
// 边界情况
if (index1 == length1) {
return nums2[index2 + k - 1];
}
if (index2 == length2) {
return nums1[index1 + k - 1];
}
if (k == 1) {
return Math.min(nums1[index1], nums2[index2]);
}
// 正常情况
int half = k / 2;
int newIndex1 = Math.min(index1 + half, length1) - 1;
int newIndex2 = Math.min(index2 + half, length2) - 1;
int pivot1 = nums1[newIndex1], pivot2 = nums2[newIndex2];
if (pivot1 <= pivot2) {
k -= (newIndex1 - index1 + 1);
index1 = newIndex1 + 1;
} else {
k -= (newIndex2 - index2 + 1);
index2 = newIndex2 + 1;
}
}
}
}
NO5 递增排序数值,后半段移到前半段,求转折点数值
两种方法,一是遍历 二是二分查找
package com.lzg.flume.intercepter.suanfati;
public class Solution {
//遍历方法
public int findKth1(int[] nums) {
int len = nums.length;
if (nums[0] < nums[len - 1])
return nums[0];
int i = 0, j = len - 1;
while (i < j) {
if (nums[i] > nums[i + 1])
return nums[i + 1];
if (nums[j] < nums[j - 1])
return nums[j];
i++;
j--;
}
return nums[i];
}
//二分查找法
public int findKth2(int[] nums) {
int len = nums.length;
if (nums[0] < nums[len - 1])
return nums[0];
int i = 0, j = len - 1;
while (i < j) {
int mid = i + (j - i) / 2;
if (nums[i] == nums[j - 1]) {
return Math.min(nums[i], nums[j]);
}
if (nums[i] < nums[mid]) {
i = mid;
} else {
j = mid;
}
}
return nums[i];
}
}
NO6 二维有序数组(从左到右是升序,从上到下是升序)的二分查找
编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target。该矩阵具有以下特性:
(1)每行的元素从左到右升序排列。
(2)每列的元素从上到下升序排列。
现有矩阵 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] ] 给定 target = 5,返回 true。 给定 target = 20,返回 false。
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix.length == 0 || matrix[0].length == 0 || target < matrix[0][0]) return false;
int row = 0, col = matrix[0].length-1;
while(row < matrix.length && col >= 0){
if(matrix[row][col] == target) return true;
else if(matrix[row][col] < target) row++;
else col--;
}
return false;
}
}