随笔分类 - LeetCode
摘要:class Solution { public ListNode swapPairs(ListNode head) { ListNode dummy = new ListNode(-1); ListNode p = dummy; dummy.next = head; while(p != null
阅读全文
摘要:思路 使用队列将每个链表元素放入,每次获取最小的元素加入到链表中 实现代码 class Solution { public ListNode mergeKLists(ListNode[] lists) { Queue<ListNode> heap = new PriorityQueue<>(new
阅读全文
摘要:class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; if(x == 0) return true; vector<int>nums; while(x) nums.push_back(x % 10),
阅读全文
摘要:class Solution { public: int myAtoi(string s) { if(s.empty()) return 0; // 去掉空格 int k = 0; while(s[k] == ' ') k++; bool flag = false; if(s[k] == '-')
阅读全文
摘要:题目思路 翻转一个整数,主要注意溢出的判断 实现代码 class Solution { public: int reverse(int x) { if(!x) return x; int res = 0; while(x) { if(x > 0 && res > (INT_MAX - x % 10)
阅读全文
摘要:class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int tot = nums1.size() + nums2.size(); if(tot % 2 ==
阅读全文
摘要:题目思路 双指针算法,只要没有出现重复的字符,就一直将i指针向后移动,如果出现了,就判断重复字符出现的字符是否是1次,然后j指针不断向后走 class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<
阅读全文
摘要:题目思路 类似于大数加法的思路,只不过每次操作的时候是用链表的节点 实现代码 class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { auto dummy = new ListNode(-1), c
阅读全文
摘要:Leetcode 1.两数之和 题目思路:使用一个哈希表,记录值和下标对应的关系,遍历整个数组,如果map中,存在target-nums[i],就得到了答案,否则就把该元素插入到map中 class Solution { public: vector<int> twoSum(vector<int>&
阅读全文