Daily Archives: February 5, 2015

[leetcode] Sort List

Sort List Sort a linked list in O(n log n) time using constant space complexity. tag: list, sort 9/22/2015 update a cleaner solution /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class […]

[leetcode] 3Sum Closest

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 […]

[leetcode] Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 tag: tree, […]

[leetcode] Longest Common Prefix

Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. tag: string 好久没一遍AC了。 直接暴力搜就好。 遍历整个字符串数组，每次查看位于n位置的字符是否相等，如果有一个不等则返回，如果全相等则n++。 class Solution { public: string longestCommonPrefix(vector<string> &strs) { int n = 0; bool isFinish = false; if(strs.empty()){ return string(“”); } while(!isFinish){ char c; if(strs[0].size() == n){ isFinish […]