【LeetCode OJ 016】3Sum Closest

题目链接:https://leetcode.com/problems/3sum-closest/

题目: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.

 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).

解题思路:先对数组进行排序,然后遍历数组。寻找和最接近target的数。

演示样例代码:

import java.util.Arrays;
public class Solution
{
    public int threeSumClosest(int[] nums, int target)
    {
    	int length=nums.length;
    	int minNum=Integer.MAX_VALUE;
    	int tempsubNum=Integer.MAX_VALUE;
    	Arrays.sort(nums);//先对nums进行排序
    	for(int i=0;i<length-2;i++)
        {
        	//略过同样的数
        	if(i!=0&&nums[i]==nums[i-1])
        	{
        		continue;
        	}
        	int left=i+1;
 	        int right=length-1;
        	while(left<right)
	        {
	        	int a1=nums[left];
	        	int a2=nums[right];
	        	int sum=a1+a2+nums[i];
	        	int subNum=Math.abs(sum-target);
	        	//当前的和值sum离target更近一点
	        	if(subNum<=tempsubNum)
	        	{
	        		tempsubNum=subNum;  //保存这一步中的sum-target的差值
	        		minNum=sum;
	        	}
	        		if(sum-target<0) //说明sum<target,
	        			left++;
	        		else
	        			right--;
	        }
        }
    	return minNum;
    }
}
时间: 2024-07-30 17:50:51

【LeetCode OJ 016】3Sum Closest的相关文章

【LeetCode OJ 075】Sort Colors

题目链接:https://leetcode.com/problems/sort-colors/ 题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0,

【LeetCode OJ 14】Longest Common Prefix

题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest common prefix string amongst an array of strings. 解题思路:寻找字符串数组的最长公共前缀,将数组的第一个元素作为默认公共前缀,依次与后面的元素进行比较,取其公共部分,比较结束后,剩下的就是该字符串数组的最长公共前缀,示例代码如下: public clas

【LeetCode OJ 242】Valid Anagram

题目链接:https://leetcode.com/problems/valid-anagram/ 题目:Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", re

【LeetCode OJ 004】Median of Two Sorted Arrays

题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ 题目:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路:将两个有序数

【LeetCode OJ 328】Odd Even Linked List

题目链接:https://leetcode.com/problems/odd-even-linked-list/ 题目:Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to d

【LeetCode OJ 225】Implement Stack using Queues

题目链接:https://leetcode.com/problems/implement-stack-using-queues/ 题目:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. empt

【LeetCode OJ 101】Symmetric Tree

题目链接:https://leetcode.com/problems/symmetric-tree/ 题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \

【LeetCode OJ 34】Search for a Range

题目链接:https://leetcode.com/problems/search-for-a-range/ 题目:Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not foun

【LeetCode OJ 232】Implement Queue using Stacks

题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the fro