leetcode-
adawangyi
这个作者很懒,什么都没留下…
展开
-
Divide Two Integers
1、有数字判断时,先看这个数字是正是负; 2、注意最大范围2147483647,和-2147483648的判断; 3、这个题是要背住的; 题目: Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT. 思路: 1、分成两次循翻译 2015-05-19 11:29:50 · 229 阅读 · 0 评论 -
Valid Sudoku
1、判断数组中是否有重复的数,先令数组中的数为0,然后当出现的时候+1,若再次出现的时候此数组中的数>0,表示重复出现,返回false; 题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where emp翻译 2015-05-22 09:38:51 · 492 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with翻译 2015-05-18 11:40:01 · 201 阅读 · 0 评论 -
Reverse Nodes in k-Group
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is翻译 2015-05-18 11:08:11 · 245 阅读 · 0 评论 -
Swap Nodes in Pairs
1、这种倒置的问题一半都要重新再选取一个listnode,然后再加上辅助的指针,cur,ret; 2、两个结点两个结点的倒置,间隔的方法为next=ret->next->next; 题目: Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4,翻译 2015-05-18 10:48:15 · 228 阅读 · 0 评论 -
Merge k Sorted Lists
1、新建一个结点ListNode *head=new ListNode(0); 2、privory—queue中可以按大小进行存储的功能, priority_queue < Type, Container, Functional> 其中Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。 Container 必须是用数组实现的容器,比如 vect翻译 2015-05-18 10:12:47 · 359 阅读 · 0 评论 -
Remove Element
题目: Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn’t matter what you leave beyond the new length. 如翻译 2015-05-18 14:39:59 · 253 阅读 · 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 array.翻译 2015-05-21 17:27:47 · 270 阅读 · 0 评论 -
Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm’s runtime complexity must be in the order of O(log n).If the target is not found in t翻译 2015-05-21 16:46:39 · 262 阅读 · 0 评论 -
Add Two Numbers
1、新建一个链表,并赋值ListNode *result=new ListNode(c%10);if(head==NULL) head=result;else prev->next=result;prev=result;return head;题目: You are given two linked lists representing two non-negative numbers.翻译 2015-05-08 11:18:35 · 198 阅读 · 0 评论 -
Container With Most Water
题目: Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find tw翻译 2015-05-12 10:27:03 · 315 阅读 · 0 评论 -
Count and Say
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2翻译 2015-05-22 10:33:07 · 242 阅读 · 0 评论 -
Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible翻译 2015-05-19 11:54:26 · 278 阅读 · 0 评论 -
Search in Rotated Sorted Array
二分方法的变种 题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array re翻译 2015-05-19 15:08:05 · 221 阅读 · 0 评论 -
Longest Valid Parentheses
题目: Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.For “(()”, the longest valid parentheses substring is “()”, whic翻译 2015-05-19 14:42:25 · 237 阅读 · 0 评论 -
Implement strStr()
1、要记住这种用双重循环的查找方法;for(int i=0;i<=lengthA-lengthB;++i) { bool flag=true; for(int j=0;j<lengthB;++j) { if(haystack[i+j]!=needle[j])翻译 2015-05-19 09:52:49 · 259 阅读 · 0 评论 -
First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. 给定一个无序整数数组,找到的第一个失踪的正整数。 思路: 1、先用桶排序,将每个元素都放到和其位置相对应的地方[1,-1,3,4]; 2、然后再进行查找每个元素是否和其位置相对应,不对应返回i+1,否则,返回n+1; 代码:class翻译 2015-05-22 11:15:46 · 257 阅读 · 0 评论 -
Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]翻译 2015-05-22 11:52:50 · 298 阅读 · 0 评论 -
Multiply Strings
1、判断乘数或者被乘数是不是负数,如果是的话去掉 ’ - ‘if(num1[0]=='-' || num2[0]=='-') { if(num1[0]=='-') { flag*=-1; num1=num1.substr(1,num1.size()-1);翻译 2015-05-22 14:46:40 · 274 阅读 · 0 评论 -
Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combinati翻译 2015-05-22 10:47:27 · 243 阅读 · 0 评论 -
Combination Sum
1、典型的动态规划题 if(sum>target)return; if(sum==target){res.push_back(path);return;} for(int i= index; i<candidates.size();i++) { path.push_back(candidates[i翻译 2015-05-22 10:43:31 · 287 阅读 · 0 评论 -
序
这里是leetcode里面的一些基本的算法题,由于本人基础较差,做题基本是参考别人的思路,做了不少题,在这里总结下,免得白做,做完就忘了。原创 2015-05-08 09:51:42 · 279 阅读 · 0 评论 -
Merge Two Sorted Lists
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路: 1、首先判断边界条件,看是否两个都为NULL,或者是否有其中一个为NULL; 2、新建一个新的翻译 2015-05-15 09:33:40 · 164 阅读 · 0 评论 -
Palindrome Number
1、一提到数字的时候就要想到正负的判断; 2、巧妙的利用/,%来取数字的每位数; 题目: Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints: Could negative integers be palindromes? (ie翻译 2015-05-11 10:52:21 · 177 阅读 · 0 评论 -
String to Integer (atoi)
1、做字符串的题要注意字符串前面是否有空格等其他字符; 2、一个字符串循环结束的标志是遇到’\0’; 3、注意正负的判断; 4、注意正无穷大和负无穷大的判断; 题目: Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want翻译 2015-05-11 10:08:31 · 235 阅读 · 0 评论 -
Reverse Integer
求一个整数的长度: int getLen(int x) { int len = 0, t = x; while (t) { t = t / 10; len += 1; } return len; }题目: Reverse digits of an integer.Example1: x = 123, retu原创 2015-05-11 09:51:39 · 239 阅读 · 0 评论 -
ZigZag Conversion
找规律的题:通过从简入繁的方法发现规律,从而进行计算; 题目: The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibi翻译 2015-05-10 21:33:19 · 249 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. F翻译 2015-05-08 11:37:32 · 409 阅读 · 0 评论 -
Longest Palindromic Substring
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 动态规划 代码:class Solut翻译 2015-05-08 21:50:18 · 234 阅读 · 0 评论 -
Median of Two Sorted Arrays
1、一提到中位数就要考虑数组的个数是奇数还是偶数; 2、涉及到排序数组的求和、求中位数这类的问题都要想到用二分法的思想; 题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run t翻译 2015-05-08 15:52:57 · 288 阅读 · 0 评论 -
Two Sum
1、pair是一个返回两个值的函数,可将两个值视为一个单元 2、make_pair函数:用来生成我们想要的pair template pair make_pair(T1 a, T2 b) { return pair(a, b); }题目: Given an array of integers, find two numbers such that they add up to a specif翻译 2015-05-08 10:28:53 · 159 阅读 · 0 评论 -
leetcode---Unique Binary Search Trees
二分查找树的特点:左边结点的必须比根结点小,右边结点的必须比根结点大。 代码如下:class Solution {public: int numTrees(int n) { //dp[i] = sigma(dp[0...k] * dp[k+1...i]) 0 <= k < i - 1; //递推出来的,从n=0开始,然后n=1,n=2等等原创 2015-03-20 20:56:34 · 208 阅读 · 0 评论 -
Regular Expression Matching
题目: Implement regular expression matching with support for ‘.’ and ‘*’.‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element.The matching should cover the entire input s翻译 2015-05-12 10:10:18 · 214 阅读 · 0 评论 -
Integer to Roman
罗马数字: 1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”};10~90: {“X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”};100~900: {“C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”};10翻译 2015-05-12 11:14:43 · 181 阅读 · 0 评论 -
Valid Parentheses
1、Valid Parentheses 括号的匹配 题目: Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.The brackets must close in the correct order, “()”翻译 2015-05-14 11:57:19 · 214 阅读 · 0 评论 -
Letter Combinations of a Phone Number
1、一个简单的动态规划;lettercombination(string digits){ help(digits,i,len);}void help(string digits,int i,int len){ if(i==len) { str[len]='\0'; string temp=str; res.push_b翻译 2015-05-14 10:13:48 · 232 阅读 · 0 评论 -
Remove Nth Node From End of List
1、遇到链表的问题,首先要记得判断head==NULL; 2、链表中查找某个结点时,通常用三个辅助指针来表示; 题目: Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n =翻译 2015-05-14 11:33:17 · 273 阅读 · 0 评论 -
3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exa翻译 2015-05-14 09:37:44 · 167 阅读 · 0 评论 -
3Sum
1、结果为vector < vector> res这样的数组时,要记得先把res进行clear; 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum翻译 2015-05-14 09:16:40 · 276 阅读 · 0 评论 -
4Sum
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: Elements in a翻译 2015-05-14 10:53:13 · 192 阅读 · 0 评论