Two Sum - Greater than target

2023-12-23 22:32
文章标签 target sum two greater

本文主要是介绍Two Sum - Greater than target,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given an array of integers, find how many pairs in the array such that their sum is bigger than a specific target number. Please return the number of pairs.

这种题目,相当于是固定一个然后变化另外一个,然后统计总数

java

public class Solution {/** @param nums: an array of integer* @param target: An integer* @return: an integer*/public int twoSum2(int[] nums, int target) {// write your code hereif (nums == null || nums.length < 2) {return 0;}Arrays.sort(nums);int left = 0;int right = 1;int value = 0;int count = 0;while (left < nums.length - 1) {if (right < nums.length) {value = nums[left] + nums[right];if (right < nums.length && value <= target) {right++;} else {count += (nums.length - right);left++;right = left + 1;}} else {left++;right = left + 1;}}return count;}
}
python

class Solution:"""@param: nums: an array of integer@param: target: An integer@return: an integer"""def twoSum2(self, nums, target):# write your code hereif nums == None or len(nums) < 2:return 0left, right = 0, len(nums) - 1value, count = 0, 0nums.sort()while left < right:value = nums[left] + nums[right]if left < right and value <= target:left += 1if left < right and value > target:count += (right - left)right -= 1return count



这篇关于Two Sum - Greater than target的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/529692

相关文章

最大流=最小割=最小点权覆盖集=sum-最大点权独立集

二分图最小点覆盖和最大独立集都可以转化为最大匹配求解。 在这个基础上,把每个点赋予一个非负的权值,这两个问题就转化为:二分图最小点权覆盖和二分图最大点权独立集。   二分图最小点权覆盖     从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。 建模:     原二分图中的边(u,v)替换为容量为INF的有向边(u,v),设立源点s和汇点t

如何导入sun.misc.BASE64Encoder和sum.misc.BASE64Decoder

右击项目名--->Build Path--->Configure Build Path...--->java Build Path--->Access rules:1 rule defined,added to all librar...   --->Edit --->Add...

leetcode#496. Next Greater Element I

题目 You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

18. 4 Sum

题目: 解答: 与之前的三数之和的解法类似,也是先排序,然后不断剔除不可能的条件,最后两个参数,通过两头求和计算得出。 代码: class Solution {public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> result;int len = nums.size

apt-get update更新源时,出现“Hash Sum mismatch”问题

转载自:apt-get update更新源时,出现“Hash Sum mismatch”问题 当使用apt-get update更新源时,出现下面“Hash Sum mismatch”的报错,具体如下: root@localhost:~# apt-get update ...... ...... W: Failed to fetch http://us.archive.ubuntu.com/ub

Java多线程编程模式实战指南:Two-phase Termination模式

文章来源: http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-two-phase-termination?utm_source=infoq&utm_campaign=user_page&utm_medium=link 文章代码地址: https://github.com/Visce

The `XXXUITests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build......

出现的警告: [!] The `ColorInHeartUITests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-ColorInHeart-ColorInHeartUITests/Po

JS实现将两个相同的json对象合并成为一个新对象(对象中包含list或者其他对象)source===target(不破坏target的非空值)

重点申明一下, 这个方法 只限于两个完全一样的对象 ,不一样的对象请使用 下面的进行合并,   <script>let form = {name: 'liming', sex: '男'};let obj = {class: '一班', age: 15};console.log('before', form);Object.assign(form, obj); //该方法可以完成console.