3Sum –>LeetCode

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

 

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

 

@Test
public void test3Sum()
{
int []s={-1,0,1,2,-1,-4};
int temp=0;
ArrayList<String> list=new ArrayList<String>();
for(int i=0;i<s.length-1;i++)
{
temp=s[i];
for(int j=i+1;j<s.length-1;j++)
{

if(temp+s[j]+s[j+1]==0)
{
list.add(temp+”,”+s[j]+”,”+s[j+1]);

}
}//end of for
}

for(String i:list)
{
System.out.println(i);
}
}

 

 

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s