随笔分类 -  LeetCode

摘要:思路:使用两个指针p1和p2遍历链表,p1在前p2在后。若p1指向的值小于p2指向的值,则p2向前走,直到二者不相等,然后更改p1的指针,再将p1和p2都向前走一步,这么做直到p2到达链表尾部。 阅读全文
posted @ 2017-10-02 13:04 蓦然闻声 阅读(138) 评论(0) 推荐(0)
摘要:class Solution { public: int searchInsert(vector& nums, int target) { int len=nums.size(); if(len nums[mid]) low=mid+1; else high=mid... 阅读全文
posted @ 2017-10-02 11:17 蓦然闻声 阅读(327) 评论(0) 推荐(0)
摘要:题目描述: 思路1: 利用动态规划。由于空间复杂度的限制,直接在triangle数组上进行修改,即把triangle数组当作dp数组。当j == 0时(即每一行的第一个元素),triangle[i][j] += triangle[i - 1][j];当j == col - 1时(即每一行的最后一个元 阅读全文
posted @ 2017-09-28 15:24 蓦然闻声 阅读(100) 评论(0) 推荐(0)
摘要:题目描述:给定一个整形数组,数组是升序排列的,可能存在负数,统计数组中所有不同绝对值的元素的个数。 思路:设置两个指针,分别指向数组的第一个元素和最后一个元素,可以看做是两个有序数组的合并过程(从大到小合并)。 阅读全文
posted @ 2017-09-26 10:46 蓦然闻声 阅读(608) 评论(0) 推荐(0)
摘要:557. Reverse Words in a String III 557. Reverse Words in a String III Given a string, you need to reverse the order of characters in each word within 阅读全文
posted @ 2017-04-13 22:03 蓦然闻声 阅读(157) 评论(0) 推荐(0)
摘要:96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, th 阅读全文
posted @ 2017-04-09 22:04 蓦然闻声 阅读(163) 评论(0) 推荐(0)
摘要:530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find the minimum absolute difference between values of an 阅读全文
posted @ 2017-04-09 22:03 蓦然闻声 阅读(240) 评论(0) 推荐(0)
摘要:520. Detect Capital Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to 阅读全文
posted @ 2017-04-09 22:02 蓦然闻声 阅读(95) 评论(0) 推荐(0)
摘要:leetcode 105题,由树的前序序列和中序序列构建树结构。详细解答参考《剑指offer》page56. 先序遍历结果的第一个节点为根节点,在中序遍历结果中找到根节点的位置。然后就可以将问题拆分,递归求解。 阅读全文
posted @ 2017-04-09 22:01 蓦然闻声 阅读(208) 评论(0) 推荐(0)
摘要:递归方法: 非递归:要借助栈,可以利用C++ STL中的stack。首先将根节点压栈,然后压入栈顶元素的左节点直到叶子节点,然后访问叶子节点后压入该节点的右子节点。 阅读全文
posted @ 2017-04-09 22:01 蓦然闻声 阅读(147) 评论(0) 推荐(0)
摘要:437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The pa 阅读全文
posted @ 2017-04-09 22:00 蓦然闻声 阅读(150) 评论(0) 推荐(0)
摘要:500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the 阅读全文
posted @ 2017-04-09 21:59 蓦然闻声 阅读(367) 评论(0) 推荐(0)
摘要:504. Base 7 Given an integer, return its base 7 string representation. Example 1: Input: 100 Output: "202" Example 2: Input: -7 Output: "-10" Note: Th 阅读全文
posted @ 2017-04-09 21:58 蓦然闻声 阅读(154) 评论(0) 推荐(0)
摘要:28. Implement strStr() Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思 阅读全文
posted @ 2017-04-09 21:56 蓦然闻声 阅读(131) 评论(0) 推荐(0)
摘要:70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many dist 阅读全文
posted @ 2017-04-09 21:55 蓦然闻声 阅读(147) 评论(0) 推荐(0)
摘要:459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of t 阅读全文
posted @ 2017-04-09 21:55 蓦然闻声 阅读(105) 评论(0) 推荐(0)
摘要:53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the a 阅读全文
posted @ 2017-04-09 21:54 蓦然闻声 阅读(142) 评论(0) 推荐(0)
摘要:326. Power of Three Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / rec 阅读全文
posted @ 2017-04-09 21:53 蓦然闻声 阅读(150) 评论(0) 推荐(0)
摘要:Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive intege 阅读全文
posted @ 2017-04-09 21:53 蓦然闻声 阅读(106) 评论(0) 推荐(0)
摘要:405. Convert a Number to Hexadecimal Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method 阅读全文
posted @ 2017-04-09 21:52 蓦然闻声 阅读(200) 评论(0) 推荐(0)