![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
AIvin24
关注人工智能、机器学习、计算机视觉、认知科学的普通青年!
展开
-
LeetCode problem 1: Reverse Words in a String
思路: 尽可能将元字符串的空格字符位置找到,采用两个索引变量i, j 指示相邻的空字符,如果| i -j | >= 2说明a[i]与a[j] 之间有正常字符,所以可以取为一个word存储下来。此外,将一个额外的空字符加到原字符串的末尾,可以将这设置为第一个空字符的位置(我们已知),类似于哨兵思想。具体代码:class Solution {public: void reverseW原创 2014-03-30 21:15:04 · 949 阅读 · 0 评论 -
LeetCode problem 2: Evaluate Reverse Polish Notation
思路:碰到符号从数据栈中取两个数计算,再将计算结果压入数据栈中,最后得到的数据栈栈顶就是所求的结果。class Solution {public: int evalRPN(vector &tokens) { stack num; int a,b,c; for(vector::iterator it = tokens.begin();it != tokens.en原创 2014-03-30 22:09:51 · 575 阅读 · 0 评论 -
LeetCode: Longest Consecutive Sequence
思路:class Solution {public: int longestConsecutive(vector &num) { unordered_map f; int max_v(1); for(auto val: num) f[val] = 1; for(auto val: num){ int left = val - 1, right原创 2014-04-16 20:44:00 · 1915 阅读 · 1 评论 -
LeetCode problem 3: Max Points on a Line
思路: 这道题目刚开始的思路就是霍夫变换,但是发现无法累计参数空间的点数(至少我没想到相应的方法,因为直线的斜率不一定是整数),根据霍夫变换中的p和\theta也不好设计,所以,放弃了这一思路。后来根据别人的一些想法,统计每个点i所在的直线中,包含点数最多的直线k(i),然后取一个整体最大值max{k(i)},i = 1,...,n,得到最后的结果,需要注意的是,计算斜率的时候一定要注意浮点数和整原创 2014-04-01 13:22:41 · 577 阅读 · 0 评论 -
LeetCode: Reorder List
思路:先逆序排列,然后再class Solution {public: void reorderList(ListNode *&head) { if(head){ //先备份 ListNode *p, *q, *t; ListNode *h = new ListNode(head->val); p = head; q = h; int len原创 2014-04-17 10:11:31 · 863 阅读 · 0 评论 -
LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
思路:参考class Solution {public: TreeNode *buildTree(vector &inorder, vector &postorder) { TreeNode * root; int len = postorder.size(); if(len < 1)return NULL; else root = constructTree(原创 2014-04-17 15:32:47 · 536 阅读 · 0 评论 -
LeetCode problem 4:Sort List
思路:这道题目主要考链表的操作,由于很久没有写链表了,这道题目花费的时间不少,主要是一些边界条件的考虑、指针的操作等等。指针的操作是个非常重要的基本功,我就是指针操作悲剧了许久 T_T。另外由于题目对时间的要求是O(nlogn)的复杂度,这个我直接能够想到的就是归并排序,由于是链表排序,所以取数组的一半可以通过两个指针来跑,跑在前面的指针是后面指针“速度”的一倍就可以了,等前面的指针跑完了,后面的原创 2014-04-02 13:17:07 · 559 阅读 · 0 评论 -
LeetCode: Triangle
思路:ziclass Solution {public: int minimumTotal(vector > &triangle) { int layer = triangle.size(); int *f = new int[layer], *pre = new int[layer]; for(int i = 0;i<layer;i++){ if(0 == i)原创 2014-04-18 10:23:32 · 498 阅读 · 0 评论 -
LeetCode: Valid Palindrome
思路:这个问题比较简单,头尾两个suoyinclass Solution {public: bool isPalindrome(string s) { int len = s.length(); if(len == 0)return true; int i = 0, j = len - 1; while(i < j){原创 2014-05-03 16:00:47 · 472 阅读 · 0 评论 -
LeetCode problem 5: Insertion Sort List
思路:完全是链表的操作,可惜还是写了很久,而且最后自己的结果竟然TLE了,无解,以后再来看吧,还是用换值先水过了……先贴一个TLE的代码(不知道哪里的问题T_T):class Solution {public: ListNode *insertionSortList(ListNode *&head) { if(head){ ListNode *p,*q,*cur,*原创 2014-04-02 16:18:58 · 682 阅读 · 0 评论 -
LeetCode: LRU Cache
class LRUCache{public: LRUCache(int capacity) { cap = capacity; size = 0; } int get(int key) { if(cap<1)return -1; unordered_map::iterator>::iterator it; it = m.find(原创 2014-04-19 10:13:31 · 1172 阅读 · 1 评论 -
LeetCode: Spiral Matrix II
思路:好吧,这个题目是很容易的,也是本人到目前为止做的zuoclass Solution {public: vector> generateMatrix(int n) { vector> S; int **ret = new int*[n]; for(int i=0;i<n;i++) ret[i] = new int[n]; int number = 0; int原创 2014-04-18 16:10:11 · 503 阅读 · 0 评论 -
LeetCode: Remove Duplicates from Sorted List II
class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(head){ ListNode *p, *q, *t; p = q = head; t = head->next; bool flag = 0; while(t){ if(t->val != q->val原创 2014-04-18 15:40:01 · 635 阅读 · 0 评论 -
LeetCode: Binary Tree Postorder Traversal
思路:digclass Solution {public: vector postorderTraversal(TreeNode *root) { if(root){ vector left, right,ret; left = postorderTraversal(root ->left); int len = left.size(); for(int原创 2014-04-18 14:15:25 · 545 阅读 · 0 评论 -
LeetCode: Edit Distance
思路:class Solution {public: int minDistance(string word1, string word2) { int len1 = word1.length(), len2 = word2.length(); int ** f = new int* [len1+1]; for(int i = 0;i < len1+1;i++) f原创 2014-04-19 10:55:56 · 560 阅读 · 0 评论 -
LeetCode: Median of Two Sorted Arrays
思路:归并排序只做前面(m+n)/2个元素class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int *C = new int[m+n]; int i=0, j=0, k = 0; for(; k *2 <= m原创 2014-04-19 16:22:09 · 539 阅读 · 0 评论 -
LeetCode: Two Sum
思路:struct Node{ int number; int index;};bool compare(Node a,Node b){ return a.number < b.number;}class Solution {public: vector twoSum(vector &numbers, int target) { vector node; int原创 2014-04-19 15:35:00 · 491 阅读 · 0 评论 -
LeetCode: Longest Substring Without Repeating Characters
思路:class Solution {public: int lengthOfLongestSubstring(string s) { if(s.empty())return 0; unordered_map flag; int ret = 0, start = 1, len = s.length(), * f; f = new int[len]; for(int原创 2014-04-21 10:58:36 · 485 阅读 · 0 评论 -
LeetCode: Add Two Numbers
class Solution {public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2){ int addBit = 0; ListNode * firstList = l1, *secondList = l2, *h = l1, *p = l1; while(firstList && secondLi原创 2014-04-21 11:39:52 · 607 阅读 · 0 评论 -
LeetCode: Reverse Integer
思路:水题,mark下class Solution {public: int reverse(int x) { int ret=0; bool flag = 0; if(x<0){flag = 1;x = abs(x);} while(x){ ret *= 10; ret原创 2014-04-21 14:19:38 · 421 阅读 · 0 评论 -
LeetCode: Longest Valid Parentheses
思路:搜索字符串,struct Symbol{ char sym; int position; Symbol(char a,int b):sym(a), position(b){}};class Solution {public: int longestValidParentheses(string s) { if(s.empty())return 0; stack原创 2014-04-21 10:13:47 · 656 阅读 · 0 评论 -
LeetCode: Remove Nth Node From End of List
思路:xianclass Solution {public: ListNode *removeNthFromEnd(ListNode *head, int n) { int count = n; ListNode *postPointer = head, *prePointer = head, *linkPointer = head; if(head){原创 2014-04-22 10:45:39 · 596 阅读 · 0 评论 -
LeetCode: Valid Parentheses
思路:如class Solution {public: bool isValid(string s) { stack stackStr; int len = s.length(), count = 0; for(int i = 0;i<len;i++){ if(s[i] == '(' || s[i] == '{'原创 2014-04-22 11:38:10 · 624 阅读 · 0 评论 -
LeetCode: Palindrome Number
思路:整数的回文注意class Solution {public: bool isPalindrome(int x) { if(x<0)return false; int temp = x; int len = 0; while(x){ x /= 10; len++;原创 2014-04-21 16:55:55 · 479 阅读 · 0 评论 -
LeetCode: Longest Common Prefix
思路:找出最短长度,依次sclass Solution {public: string longestCommonPrefix(vector &strs) { if(strs.size()>0){ int len = strs[0].length(); for(int i=1;i<strs.size();i++){原创 2014-04-21 17:39:46 · 457 阅读 · 0 评论 -
LeetCode: Linked List Cycle II
siclass Solution {public: ListNode *detectCycle(ListNode *head) { if(head){ while(head){ if(head->val == int(head))return head; head->val = int(head); head = head->next; }原创 2014-04-19 10:35:21 · 468 阅读 · 0 评论 -
LeetCode: Pascal's Triangle
思路:(好吧,这个题目是直接在网页上敲出来的,并且)class Solution {public: vector > generate(int numRows) { vector > ret; if(numRows >= 1){ vector temp; temp.push_back(1);原创 2014-04-22 20:58:43 · 489 阅读 · 0 评论 -
LeetCode: Divide Two Integers
思路:class Solution {public: int divide(int dividend, int divisor) { if(dividend==0) return 0; int isNegative = 1; if((dividend 0) || (dividend > 0 && divisor<0)) isNegative =原创 2014-04-22 16:17:40 · 494 阅读 · 0 评论 -
LeetCode: Remove Element
思路:class Solution {public: int removeElement(int A[], int n, int elem) { if(n int end = n - 1; for(int i =0;i if(A[i] == elem){ while(A[原创 2014-04-22 16:37:41 · 477 阅读 · 0 评论 -
LeetCode: Rotate Image
思路:class Solution {public: void rotate(vector > &matrix) { vector > new_matrix(matrix); int len = matrix.size(); for(int i = 0;i<len;i++) for(int j=0;j<len;j++) new_matrix[j][len-i原创 2014-04-23 10:25:13 · 528 阅读 · 0 评论 -
LeetCode: Search a 2D Matrix
思路:《》class Solution {public: bool searchMatrix(vector > &matrix, int target) { int r = 0, c = matrix[0].size()-1; //记录行列位置 while(1){ if(matrix[r][c] == target) return true; else if(ma原创 2014-04-09 21:04:09 · 588 阅读 · 0 评论 -
LeetCode: Pow(x, n)
class Solution {public: double pow(double x, int n) { if(0 == n) return 1; int isNegative = n <0 ? -1:1; long long N = n, times; N = abs(N); long double X原创 2014-04-25 08:34:31 · 549 阅读 · 0 评论 -
LeetCode: Generate Parentheses
class Solution {public: vector generateParenthesis(int n) { vector ret; string s1; if(0 == n)return ret; if(1 == n){ s1+='('; s1+=')'; ret.push_back(s1); } else for(原创 2014-04-25 11:56:52 · 465 阅读 · 0 评论 -
LeetCode: Jump Game
思路:如果碰到某个位置,元素不再前进了,而且当前位置weclass Solution {public: bool canJump(int A[], int n) { for(int i=0;i<n-1;){ int next = i + A[i]; if(next == i )return false;原创 2014-04-25 20:49:30 · 478 阅读 · 0 评论 -
LeetCode: Add Binary
思路:模拟加法运算即可class Solution {public: string addBinary(string a, string b) { int len1 = a.size(), len2 = b.size(); vector ret; int i = len1 -1, j = len2-1, addition = 0;原创 2014-04-25 20:39:51 · 632 阅读 · 0 评论 -
LeetCode: Populating Next Right Pointers in Each Node
思路:遍历过程是广度优先,class Solution {public: void connect(TreeLinkNode *root) { if(root){ queue que1,que2; TreeLinkNode * curNode = root; que1.push(curNode);原创 2014-05-12 17:57:23 · 497 阅读 · 0 评论 -
LeetCode: Combination Sum II
思路:和前面一题一样,不过这次是要保证bunen原创 2014-06-23 12:33:17 · 467 阅读 · 0 评论 -
LeetCode: Trapping Rain Water
思路:从当前位置pos的柱子开始,往后搜索原创 2014-06-23 13:44:11 · 417 阅读 · 0 评论 -
LeetCode: Jump Game II
思路:动态规划,设ret[i] 表示从第0位置到达i位置需要的最小bushu原创 2014-06-23 21:13:01 · 515 阅读 · 0 评论 -
LeetCode: Spiral Matrix
思路:先计算好需要遍历多少层,每一层原创 2014-06-24 11:56:13 · 451 阅读 · 0 评论