Longest Palindromic Substring

Description Difficulty: 1.0-3.5/5.0 star https://oj.leetcode.com/problems/longest-palindromic-substring/ Solution This is an interesting problem. There are five solutions: Brute force ( , ), DP ( , ), LCS-DP ( , ), Center-oriented ( , ), Manacher’s Algorithm ( , ), where the first...

Longest Common Prefix

Description https://oj.leetcode.com/problems/longest-common-prefix/submissions/ Difficulty: 1.0/5.0 Analysis and solution string to_string(int) is a very useful function here. class Solution { public: string longestCommonPrefix(vector<string> &strs) { string result(""); if (!strs.size()) return result; for (int ps = 0; ps < strs[0].size(); ++ ps){ int...

Count and Say

Description https://oj.leetcode.com/problems/count-and-say/ Difficulty: 1.0/5.0 Analysis and solution string to_string(int) is a very useful function here. class Solution { public: string countAndSay(int n) { string result("1"); for (int i = 0;i < n-1; ++ i) result = countAndSayHelper(result); return result; }...

Add Binary

Description https://oj.leetcode.com/submissions/detail/13241213/ Difficulty: 1.0/5.0 Solution class Solution { public: string addBinary(string a, string b) { string result(""); int idxA = a.length()-1, idxB = b.length()-1; int carry = 0; while(idxA >= 0 || idxB >= 0){ int re = carry; re...

Subsets i and ii

Description https://oj.leetcode.com/problems/subsets/ Difficulty: 1.0/5.0 Analysis and solution // Problem i class Solution { public: vector<vector<int> > subsets(vector<int> &S) { vector<vector<int>> result; if (!S.size()) return result; result.push_back(vector<int>()); sort(S.begin(), S.end()); for (int i = 0; i < S.size(); ++ i){ int num...