Monthly Archives: February 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] 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 […]


[leetcode] Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. tag: math, string 9/28/2015 update class Solution { public: unordered_map<char, int> map; int romanToInt(string s) { map[‘I’] = 1; map[‘V’] = 5; map[‘X’] = 10; map[‘L’] […]


[leetcode] Integer to Roman

Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. tag: string, math 9/28/2015 update class Solution { public: vector<string> roman = {“M”, “CM”, “D”, “CD”,”C”,”XC”,”L”,”XL” ,”X”,”IX”,”V”,”IV”,”I”}; vector<int> value = {1000, 900, 500, 400, 100, 90, […]