C实现 LeetCode->3SumClosest
2015-06-15 10:10
330 查看
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have
exactly one solution.
一个整形数组,找到3个数的总和 最接近 一个目标target值;返回3个整数的总和。
//
// 3SumClosest.c
// Algorithms
//
// Created by TTc on 15/6/15.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/**
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定的n个整数数组,找到三个整数的年代,总数接近给定目标数字。返回三个整数之和。你可能认为每个输入会完全一个解决方案。
EG: S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
#include "3SumClosest.h"
#include <stdlib.h>
#include <string.h>
static int
cmp(const int*a,const int*b){
return (*a) - (*b);
}
int
threeSumClosest(int* nums, int numsSize, int target) {
int ans = 0;
int sum;
qsort(nums, numsSize, sizeof(int), (int(*)(const void *,const void *))cmp);
if(numsSize <= 3){
for (int i =0 ; i< numsSize; i++) {
ans += nums[i];
}
return ans;
}
//数组已经是排序好的
/*
1: 现在我们每一个数字V[i]:我们寻找一对数字 v[j] 和 v[k];
2: 这样绝对值(target -(v[i] + v[j] + v[k])) 最小化
3: 如果3个元素的 总和 > target ,我们需要减小sum的值,需要做k = k -1操作
4: 如果3个元素的 总和 < target ,我们需要增加sum的值,需要做j = j +1操作
*/
ans = nums[0] + nums[1] + nums[2];
for (int i = 0; i < numsSize - 2; i++) {
int j = i + 1;
int k = numsSize - 1;
while (j < k) {
sum = nums[i] + nums[j] + nums[k];
if(abs(target -ans) > abs(target - sum)){
ans = sum;
if(ans == target) return ans;
}
(sum > target) ? k-- : j++;
}
}
return ans;
}
void
test_threeSUmCLosest(){
int nums[10] = {-1,2,1,-4};
int result = threeSumClosest(nums, 4, 1);
printf("result===%d",result);
}
exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
一个整形数组,找到3个数的总和 最接近 一个目标target值;返回3个整数的总和。
//
// 3SumClosest.c
// Algorithms
//
// Created by TTc on 15/6/15.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/**
* Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
给定的n个整数数组,找到三个整数的年代,总数接近给定目标数字。返回三个整数之和。你可能认为每个输入会完全一个解决方案。
EG: S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*/
#include "3SumClosest.h"
#include <stdlib.h>
#include <string.h>
static int
cmp(const int*a,const int*b){
return (*a) - (*b);
}
int
threeSumClosest(int* nums, int numsSize, int target) {
int ans = 0;
int sum;
qsort(nums, numsSize, sizeof(int), (int(*)(const void *,const void *))cmp);
if(numsSize <= 3){
for (int i =0 ; i< numsSize; i++) {
ans += nums[i];
}
return ans;
}
//数组已经是排序好的
/*
1: 现在我们每一个数字V[i]:我们寻找一对数字 v[j] 和 v[k];
2: 这样绝对值(target -(v[i] + v[j] + v[k])) 最小化
3: 如果3个元素的 总和 > target ,我们需要减小sum的值,需要做k = k -1操作
4: 如果3个元素的 总和 < target ,我们需要增加sum的值,需要做j = j +1操作
*/
ans = nums[0] + nums[1] + nums[2];
for (int i = 0; i < numsSize - 2; i++) {
int j = i + 1;
int k = numsSize - 1;
while (j < k) {
sum = nums[i] + nums[j] + nums[k];
if(abs(target -ans) > abs(target - sum)){
ans = sum;
if(ans == target) return ans;
}
(sum > target) ? k-- : j++;
}
}
return ans;
}
void
test_threeSUmCLosest(){
int nums[10] = {-1,2,1,-4};
int result = threeSumClosest(nums, 4, 1);
printf("result===%d",result);
}
相关文章推荐
- 在IIS网站搭建过程中经常出现一些问题,对这些问题进行总结,供以后参考
- 实现一个弹窗
- [突发奇想] 验证码 识别人与自动化操作是否可采用 鼠标 停留位置 与 时间 来自动验证 是否是人呢?
- JQuery
- 线程同步总结
- nodejs 中压缩/解压方案
- 解决整合 UCenter 首次注册同步登录与自动激活
- Multiple markers at this line @Override的解决方法
- 关于android渐变色的配置使用
- JFlow&CCFlow流程引擎的业务数据与流程数据同步的操作步骤.
- static 方法
- objective c 类目 延展 协议
- SCORM内容聚合模型 (CAM)V1.3.1
- Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase
- c#using用法详解篇
- JavaScript中使用指数方法Math.exp()的简介
- Jquery简单实现GridView行高亮的方法
- 图结构练习——判断给定图是否存在合法拓扑序列
- 泛微E-Mobile4.5服务端安装手册
- 利用API方式进行数据库的增删改查