Can you guys please help me with this problem? via /r/learnprogramming


Can you guys please help me with this problem?

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? This is what I have so far, but it doesn't work, however, I'm sure there are better approaches too, any tips or suggestions are greatly appreciated. Thanks:)

 public static int[][]threeSum(int[] nums, int target){ Hashtable<Integer, Integer> values = new Hashtable<Integer, Integer>(); for(int cnt=0; cnt < nums.length; cnt++){ values.put(nums[cnt], cnt); } int[][] list = new int[4][4]; int count = 0; for(int cnt=0; cnt < nums.length; cnt++){ for(int cnt1=cnt+1; cnt1 < nums.length; cnt1++){ int n = nums[cnt]+nums[cnt1]; if (count >3){break;} if (values.get(target-n)!=null){ list[count][0]=cnt; list[count][1]=cnt1; list[count][2]=values.get(target-n); count++; } } } return list; } 

Submitted July 12, 2017 at 03:08AM by limbwal
via reddit http://ift.tt/2uc48Uj

Advertisements

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