随笔分类 - LeetCode
leetcode里面的一些解决方案
摘要:class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { set<vector<int>> res; sort(nums.begin(), nums.end()); for (int k = 0; k < n
阅读全文
摘要:class Solution { public: string longestCommonPrefix(vector<string>& strs) { if (strs.empty()) return ""; for (int j = 0; j < strs[0].size(); ++j) { fo
阅读全文
摘要:class Solution { public: int romanToInt(string s) { int res = 0; unordered_map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D'
阅读全文
摘要:class Solution { public: string intToRoman(int num) { string res = ""; vector<int> val{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; vector<s
阅读全文
摘要:class Solution { public: bool isMatch(string s, string p) { int m = s.size(), n = p.size(); vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false))
阅读全文
摘要:class Solution {public: bool isPalindrome(int x) { //negative number if(x < 0) return false; int len = 1; while(x / len >= 10) len *= 10; while(x > 0)
阅读全文
摘要:public class Solution { public int myAtoi(String str) { if (str.isEmpty()) return 0; int sign = 1, base = 0, i = 0, n = str.length(); while (i < n &&
阅读全文
摘要:class Solution { public: int reverse(int x) { int res = 0; while (x != 0) { int t = res * 10 + x % 10; if (t / 10 != res) return 0; res = t; x /= 10;
阅读全文
摘要:class Solution { public: string convert(string s, int nRows) { if (nRows <= 1) return s; string res = ""; int size = 2 * nRows - 2; for (int i = 0; i
阅读全文
摘要:// Time complexity O(n*n) class Solution { public: string longestPalindrome(string s) { int startIdx = 0, left = 0, right = 0, len = 0; for (int i = 0
阅读全文
摘要:class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int total = nums1.size() + nums2.size(); if (total %
阅读全文
摘要:class Solution { public: int lengthOfLongestSubstring(string s) { int m[256] = {0}, res = 0, left = 0; for (int i = 0; i < s.size(); ++i) { if (m[s[i]
阅读全文
摘要:public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(-1); ListNode cur = dummy; int carry =
阅读全文
摘要: public class Solution { public int[] twoSum(int[] numbers, int target) { HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); int[]
阅读全文