
leetcode
lixq05
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode--线性表--数组专题
1. Single Number 1题目描述: given an array of integres, every elements appears twice except for one ,find that single one Note: your algorithm should have a linear runtime complexity,could yo...原创 2018-09-06 17:33:47 · 416 阅读 · 0 评论 -
866. Prime Palindrome
解题思路:发现有个规律ABCCBA,ABCDDCBA这种偶数形式的一定不是质数(11除外),一定是11的倍数,所以只要找奇数位长度的数了 直接暴力求解,剪去位数为偶数的分支,以节省时间。bool isPrime(int num){ if (num < 2) return false; for (int i = 2; i <= sqrt(n...原创 2018-07-23 15:40:43 · 446 阅读 · 0 评论 -
树的遍历
树的深度优先搜索和广度优先搜索树的深度优先搜索: 前序遍历:根-左-右 中序遍历:左-根-右 后序遍历:左-右-根循环将根节点的左子树压入栈中(并visit),直到没有左结点。将栈顶结点弹出,并遍历右子树 代码:class Solution {public: vector<int> preorderTraversal(TreeNod...原创 2018-03-20 18:23:09 · 142 阅读 · 0 评论 -
Number of 1 Bits
题目描述:Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 000...原创 2018-03-14 09:32:01 · 154 阅读 · 0 评论 -
189. Rotate Array
题目:Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 解题思路:将数组向右移动k步代码:第一种方法: 将数组分为两部分:对...原创 2018-03-13 13:09:50 · 267 阅读 · 0 评论 -
Pascal's Triangle II
题目描述: Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].Note: Could you optimize your algorithm to use only O(k) extra space? 题目解析: 在上一...原创 2018-03-05 21:41:44 · 124 阅读 · 0 评论 -
172. Factorial Trailing Zeroes
题目描述:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Credits: Special thanks to @ts for adding this problem and creat...原创 2018-03-12 12:59:17 · 128 阅读 · 0 评论 -
206. Reverse Linked List
题目描述:Reverse a singly linked list.click to show more hints. Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?解题思路:采用迭代的方式: 循环将当前节点移动到前一个节点的前面...原创 2018-03-17 15:50:36 · 113 阅读 · 0 评论 -
205. Isomorphic Strings
题目描述:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with a...原创 2018-03-17 12:47:55 · 132 阅读 · 0 评论 -
170: Two Sum III - Data structure design
题目描述:Design and implement a TwoSum class. It should support the following operations:add and find.add - Add the number to an internal data structure. find - Find if there exists any pair of numbe...原创 2018-03-11 11:31:50 · 278 阅读 · 0 评论 -
Read N Characters Given Read4
题目:The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in ...原创 2018-03-10 16:59:35 · 150 阅读 · 0 评论 -
Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element...原创 2018-03-10 16:26:09 · 139 阅读 · 0 评论 -
204. Count Primes
题目描述:Description:Count the number of prime numbers less than a non-negative number, n.解题思路:使用见埃拉托色尼筛法。 一个数的n倍一定是合数代码:class Solution { public int countPrimes(int n) { //这里...原创 2018-03-16 12:46:25 · 137 阅读 · 0 评论 -
Remove Linked List Elements
题目描述:Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5 ...原创 2018-03-16 12:28:19 · 123 阅读 · 0 评论 -
202. Happy Number
题目: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 integer, replace the number by the sum of the squa...原创 2018-03-15 14:57:40 · 130 阅读 · 0 评论 -
166. Fraction to Recurring Decimal
解题思路: 我们每次都作除,然后取整数部分,然后余数*10,继续下去。 在此基础上,要判断小数部分是否重复 借用string来实现在对应重复的位置上插入(,并在结果后面追加) 借用map来实现对重复部分的标记其位置,并做查询#include<stdio.h>#include<string>#include<map>using namespa...原创 2018-07-24 10:51:14 · 147 阅读 · 0 评论 -
130. Surrounded Regions
周围区域不应位于边界上,这意味着板边框上的任何“O”都不会翻转到“X”。 任何不在边框上并且未连接到边框上的“O”的“O”将翻转为“X”。 如果它们是水平或垂直连接的相邻单元,则连接两个单元。解决思路:只有边界上是‘O’以及与边界是‘O’相连的不变,其余的‘O’要变为’X’ 对棋盘进行遍历,利用DFS将边界上的以及与边界相连的‘O’先存成’*’ 然后对整个棋盘进行遍历,将依旧是’O’的改为...原创 2018-07-24 15:44:07 · 145 阅读 · 0 评论 -
Reverse Words in a String
主要是空格的问题:前导空格,后置空格,以及中间多余的空格。不能够用额外的空间,所以我们只能在原字符串上做修改,而不能另申请 解题思路:先翻转单词,再整体翻转,最后遍历的过程中,将多余的空格删除核心解决问题:最后遍历已经逆转好的字符串时,如何去除多余空格: 前导空格和中间多余空格通过: //如果当前字符是空格,且之前已经加入一个空格,则这个空格跳过s[i] !=' ' || ...原创 2018-07-22 19:56:36 · 339 阅读 · 0 评论 -
29. Divide Two Integers
题目要求:给定两个整数,要求不用乘除法和取模运算,计算出a/b的值,当结果越界的时候输出INT最大值解题思路:用两个数组暂存b对应的倍数,和相应的系数 long long ABS(long long a){ return a > 0 ? a : -a;}int divide(int dividend, int divisor){ if (divisor ...原创 2018-07-22 18:45:06 · 993 阅读 · 0 评论 -
98. Validate Binary Search Tree
解题思路:验证合法的BST,采用递归的方式,分别检验左子树和右子树是否满足要求即可,但是会出现这种情况: 10 5 15 6 20所以要在递归判断的时候,带上上下限,判断左子树的时候,其上限为root...原创 2018-07-27 10:23:35 · 274 阅读 · 0 评论 -
179. Largest Number
解题思路:关键问题是确定一个排序规则(mnbool cmp(string str1, string str2){ string strtmp1 = str1 + str2; string strtmp2 = str2 + str1; return strtmp1 > strtmp2;}string largestNumber(vector<int>...原创 2018-07-27 09:59:31 · 191 阅读 · 0 评论 -
LeetCode 8 String to Integer (atoi)
解题思路:) 开始有空格的话,直接++;开始的正负号做一个保存;数字逐位计,碰到非数字的情况,直接返回当前数值;碰到超过上下限的问题,直接返回对应的最大值和最小值int myAuto(char *str){ int flag = 1; //保存符号 int res = 0; int dig; //清理开始的空格 while (*str == '...原创 2018-07-22 10:36:10 · 224 阅读 · 0 评论 -
542. 01 Matrix
解题思路:采用BFS寻找最短路径。具体操作为: 1. 首先对矩阵进行一次循环,将所有的0压入队列中(寻找最短路径中已知的最短的一部分),将所有不是0 的标记为-1 2. 从队列中弹出一个,对四个方位进行遍历,没有访问过的位置得以更新最短路径,并压入队列 3. 矩阵中所有元素都被访问完即结束 vector<vector<int>> update...原创 2018-07-30 10:09:33 · 249 阅读 · 0 评论 -
132 Pattern
解题思路:ibool find132pattern(vector&lt;int&gt;&amp; nums) { int n = nums.size(); if(n&lt;=2) return false; stack&lt;int&gt; s; int third = INT_MIN; ...原创 2018-07-30 09:38:50 · 265 阅读 · 0 评论 -
523. Continuous Subarray Sum
解题思路一:从长度为2开始循环遍历数组,逐步增加长度来求连续子数组的和,判断其是否满足条件:这里要注意k=0和sum=0的情况。 采用动态规划的方法,在上层循环的基础上再多加一个元素求和即可bool checkSubarraySum(vector<int>& nums, int k){ int sz = nums.size(); vector<...原创 2018-07-26 16:51:01 · 225 阅读 · 0 评论 -
457. Circular Array Loop
题目大意:确定一个数组中是否有循环。一个循环在一个特定的索引开始和结束,循环中有超过1个元素。循环必须是“向前”或“向后”。解题思路:循环遍历数组,并为每个元素设置标志是否已经访问过。然后根据坐标建立一对一映射,一旦某个达到的坐标已经有映射了,说明环存在,这里有一系列的限制条件:如果next和cur相等,说明此时是一个数字的循环,不符合题意在一个loop中必须同...原创 2018-07-26 10:57:26 · 349 阅读 · 0 评论 -
15. 3Sum
解题思路:类似于两数和问题 首先对给定数组进行排序 选定第一个数,从后续数中找到和为(0-num[i])的两个数设定三个指针: p,q,r 1. p为选定的第一个数,如果满足条件(后面找到满足条件的两个数)则push到最终结果中,如果不满足,则p++ 。遍历终止为len-2。**(这里要注意,跳过重复数字)----while循环** 2. q指向p+1,r指向len-1。求两...原创 2018-07-26 10:04:46 · 158 阅读 · 0 评论 -
165. Compare Version Numbers
解题思路: 采用两个指针,分别遍历用’.’分割开打的数据区域进行比较int getVer(string str, int &cur, int len){ int buff = 0; while (str[cur] != '.' && cur < len) { buff = buff * 10 + str[cur] -...原创 2018-07-25 16:21:58 · 362 阅读 · 0 评论 -
91. Decode Ways
解题思路:采用动态规划来解决:关键是状态转移方程的全面考虑 s[i-1]='2' && s[i]>='1' &&s[i]<='6'int numDecodings(string s) { if (s.empty() || s[0] == '0') return 0; int num2 = 1; int...原创 2018-07-25 16:03:59 · 134 阅读 · 0 评论 -
127. Word Ladder
解题思路: 求最短的路径长度—-采用BFS,状态变化通过上一层结点中某个字母变化后并且出现在wordlist中(利用一个函数表示是临近状态)int ladderLength(string beginWord, string endWord, vector<string>& wordList) { int dis = 1; int len = wordLis...原创 2018-07-25 15:18:21 · 174 阅读 · 0 评论 -
468. Validate IP Address
解题思路: 两个函数分别判断是否是IPV4和IPV6,两个函数的思想差不多:逐位遍历,到分隔符,对每一部分做合法检验,最后对总长度(4 or 8)做一个合法检验string validIpAddress(string IP){ if (IsIPv4(IP)) return "IPv4"; else if (IsIPv6(IP)) return...原创 2018-07-24 17:25:45 · 178 阅读 · 0 评论 -
198. House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent ...原创 2018-03-15 14:30:45 · 135 阅读 · 0 评论 -
168. Excel Sheet Column Title
题目:Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example:1 -> A2 -> B3 -> C...26 -> Z27 -> AA28 -> AB 解题思路:进制转换问题...原创 2018-03-09 10:56:53 · 159 阅读 · 0 评论 -
Add Binary
题目描述:Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”. 解题思路:从后往前遍历即可,找出其中比较长的字符串。用一个整数专门来记录进位代码一:class Solution { pub原创 2018-01-27 15:58:42 · 160 阅读 · 0 评论 -
Plus One
题目描述:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The d原创 2018-01-26 21:40:58 · 235 阅读 · 0 评论 -
Length of Last Word
题目:Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2018-01-26 17:57:33 · 152 阅读 · 0 评论 -
Maximum Subarray
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1]原创 2018-01-26 17:26:18 · 133 阅读 · 0 评论 -
Search Insert Position
题目描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the a原创 2018-01-25 20:45:18 · 190 阅读 · 0 评论 -
Count and Say
题目描述The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 111221 1 is read off as "one 1" or 11.11 is原创 2018-01-25 16:53:15 · 236 阅读 · 0 评论 -
Implement strStr()
题目描述Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1:Input: haystack = "hello", needle = "ll"Output: 2原创 2018-01-24 22:04:10 · 155 阅读 · 0 评论