今天下午,看了一会github,想刷个题呢,就翻出来了刷点题提高自己的实际中的解决问题的能力,在面试的过程中,我们发现,其实很多时候,面试官 给我们的题,其实也是有一定的随机性的,所以我们要多刷更多的题.去发现问题. 题目:     给定一个整数数组 nums 和一个目标值 taget,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 解析: 实际这里就是给你的一个列表的数字,给你一个预期,让你返…
/** * 给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标. * * 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素在答案里不能重复出现. * * 你可以按任意顺序返回答案. * 示例 1: * 输入:nums = [2,7,11,15], target = 9 * 输出:[0,1] * 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] . * 示例 2: * 输入:num…
//统计一个长度为2的字符串在另外一个字符串中出现的次数. #include <conio.h> #include <stdio.h> #include <string.h> #include <stdlib.h> int fun(char *str, char *substr) { char *z, *c; z = str; c = substr; ; while (*z!='\0') { if (*z== *c) { z++; c++; if (*z =…
这个是来自力扣上的一道c++算法题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum自己采用的解法还有网上学习来的方法. 暴力方法:(遍历每个元素 xx,并查找是否存在一个值与 target - xtarget−x 相等的…
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序       Arrays.sort(arr); 将前三个数相加赋给closeNum,表示初始化     int closeNum = arr[0] + arr[1] + arr[2]; 在第一层循环中for(int i = 0;i<arr.length;i++),我们定义双指针就j和k,j指向当前i的下一个…
第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) public static int[] getMinKNumsByHeap(int[] arr, int k) { if (k < 1 || k > arr.length) { return arr; } int[] kHeap = new int[k];//存放第k小的数 for (int i =…
题目 输入一个整型数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为 O(n). 输入 [1,-2,3,10,-4,7,2,-5] 返回值 18 说明: 输入的数组为{1,-2,3,10,-4,7,2,一5},和最大的子数组为{3,10,一4,7,2},因此输出为该子数组的和 18. 分析 定义一个dp数组,dp[i]表示前i个元素的最大和.状态方程 dp[i] = dp[i-1]<0?array[i]:dp[i-1]+array[i…
Decrisption Given an array of integers arr, find the sum of min(b), where b ranges over every (contiguous) subarray of arr. Since the answer may be large, return the answer modulo \(10^9\) + 7. Input Example 1: Input: \(arr = [3,1,2,4]\) Output: 17 E…
Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be large, return the answer modulo 10^9 + 7. Example 1: Input: [3,1,2,4] Output: 17 Explanation: Subarrays are [3], [1],…
var threeSumClosest = function(nums, target) { let ans = nums[0] + nums[1] + nums[2]; const len = nums.length; nums.sort((a, b) => a - b); // 排序 for (let i = 0; i < len ; i++) { let L = i+1; let R = len-1; while(L < R){ const sum = nums[i] + nums…