5 Responses

  1. Yueyong Su
    Yueyong Su May 17, 2012 at 9:34 pm |

    My Solution costs 12 milli secs, exceeding the time limit.

    class Solution {
    public:
    vector<vector > threeSum(vector &num) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    vector temp2;
    vector<vector > temp1;
    for(size_t i=0; i<num.size();++i)
    {
    for(size_t j=i+1; j<num.size();++j)
    {
    for(size_t k=j+1; k=B)
    {
    D=B;
    B=C;
    C=D;
    }
    if(B>=A)
    {
    D=A;
    A=B;
    B=D;
    }

    if(C>=B)
    {
    D=B;
    B=C;
    C=D;
    }

    temp2.push_back(C);
    temp2.push_back(B);
    temp2.push_back(A);
    bool same=false;
    for(size_t l=0; l<temp1.size(); ++l)
    {
    if(temp1[l]==temp2)
    same=true;
    }
    if(!same)
    temp1.push_back(temp2);
    }
    }
    }
    }
    }
    return temp1;
    }
    };

    Reply
  2. some links | codingfan
    some links | codingfan October 5, 2012 at 3:43 pm |

    […] http://blog.theliuy.com/2012/3sum/ Like this:LikeBe the first to like this. This entry was posted in interview and tagged bookmark. Bookmark the permalink. ← decode ways […]


  3. Ozan Tabak
    Ozan Tabak November 24, 2012 at 2:53 am |

    Valid Java Solution

    public class Solution {
    public ArrayList<ArrayList> threeSum(int[] list) {
    // Start typing your Java solution below
    // DO NOT write main() function
    ArrayList<ArrayList> result = new ArrayList<ArrayList>();

    Arrays.sort(list);
    HashMap set = new HashMap();
    int size = list.length;
    int size1 = size – 1;
    int size2 = size1 – 1;
    int[] item = new int [3];

    for (int i = 0; i < size2; i++) {

    int j = i + 1;
    int ival = list[i];
    item[0] = ival;

    for (; j < size1; j++) {
    int k = j+1;
    int jval = list[j];
    int sum = ival + jval;
    item[1] = jval;

    for (; k < size; k++) {
    int kval = list[k];
    if (kval + sum == 0) {
    item[2] = kval;
    set.put((new StringBuilder()).append(ival).append(jval).append(kval).toString(),item.clone());
    }
    }

    }
    }

    for (int[] aritem : set.values() ) {
    ArrayList p = new ArrayList();
    for (int ii : aritem) {
    p.add(ii);
    }
    result.add(p);
    }

    return result;

    }
    }

    Reply

Leave a Reply