leetcode
summerdj
这个作者很懒,什么都没留下…
展开
-
leetcode 92. Reverse Linked List II
反转指定两个位置之间的树。ListNode *reverseBetween(ListNode *head, int m, int n) { if(m==n)return head; n-=m; ListNode prehead(0); prehead.next=head; ListNode* pre=&prehead; while(--m)pre=pr原创 2017-02-28 21:14:10 · 319 阅读 · 0 评论 -
leetcode-32-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 “()”, which has原创 2016-08-30 09:20:02 · 407 阅读 · 0 评论 -
leetcode-33-search in rotated 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 return its index原创 2016-09-09 16:24:04 · 284 阅读 · 0 评论 -
leetcode-30-Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and wit原创 2016-08-02 17:28:01 · 403 阅读 · 0 评论 -
leetcode-29-Divide Two Integers
题目:Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.题目大意:不用乘法除法和取余运算符来完成除法运算。 思路:只能用减法咯。 a 除以 b 每次我们可以使用2分法来加速这个过程。每次减运算时对除数乘以2加倍,直到它比被除数原创 2016-07-05 17:07:36 · 268 阅读 · 0 评论 -
leetcode-28-Implement strStr()
题目:Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题目大意:给两个字符串haystack和needle。返回needle在haystack中第一次出现的位置。思路:遍历haystack。如果在i位置满足条件就返回当前位置。注意边界条件。原创 2016-06-02 22:01:42 · 205 阅读 · 0 评论 -
leetcode-24-Swap Nodes in Pairs
题目:iven a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.交换链表每两个元素的位置。思路: 1.出现单个的情况链表不变 2.链表为空或者只有一个元素,返回head头原创 2016-05-19 21:50:56 · 575 阅读 · 2 评论 -
leetcode-27-Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The or原创 2016-06-01 11:17:10 · 204 阅读 · 0 评论 -
leetcode-26-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 c原创 2016-06-01 10:49:31 · 222 阅读 · 0 评论 -
leetcode-34-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 the ar原创 2016-09-14 14:41:15 · 211 阅读 · 0 评论 -
leetcode-31-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原创 2016-08-24 15:25:22 · 239 阅读 · 0 评论 -
leetcode-35-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.Here原创 2016-09-14 20:52:05 · 173 阅读 · 0 评论 -
leetcode 84 Remove Duplicates from Sorted List II
删除一个链表里面重复出现过的元素。 11123 -〉 23自己的思路:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class原创 2017-02-28 16:03:42 · 320 阅读 · 0 评论 -
leetcode 41 finding missing positive
找到数组中第一个消失的正整数。先桶排序,将值为i的数和第 i-1 位 交换,然后遍历数组找到第一个值不等于位置 +1 的即可。class Solution { public: int firstMissingPositive(vector<int>& nums) { int l=nums.size(); for(int i=0;i<l;i++){原创 2017-02-27 20:41:47 · 251 阅读 · 0 评论 -
leetcode-38-count and say
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, then one 1” or 1211.求第n个序列的输出。基本思路,迭代。 循环计算下一个输出,循环调用next函数。 int b=8; char a=b原创 2016-10-18 09:46:37 · 240 阅读 · 0 评论 -
leetcode-37-sudoku solver
题目:求解数独,假设只有一种结果。回溯法递归判断每一个 空位置的 所有可能性。 比如 第一个为空 的位置 为1是是有效的,然后递归调用 solvesudoku(新的第一个位置为1的board),依次递归。如果false了,在返回到第一个位置为第二个有效数字的情况。注意: board[i][j] = ‘1’ + k; int 和 char 可以直接加,vector。// LeetCode, Su原创 2016-09-26 11:11:09 · 262 阅读 · 0 评论 -
leetcode-36-valid sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.A partially filled sudoku which原创 2016-09-21 15:05:17 · 235 阅读 · 0 评论 -
leetcode-02-add two numbers-c++
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linke原创 2016-08-26 15:56:06 · 566 阅读 · 0 评论 -
leetcode-23-Merge k Sorted Lists
题意:合并k个有序链表。tags:divide and conquer。分治算法。思路在代码注释里面。 测试通过时间神奇的在32ms。一般为400ms。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)原创 2016-05-11 22:17:51 · 317 阅读 · 0 评论 -
leetcode-19-Remove Nth Node From End of List
题意:删除链表从后往前开始的第n个节点。单链表的操作。别人说用python来实现单链表没有意义,好吧我不知道怎么反驳暂且附和好了。python用list可以实现堆栈和队列(pop(0)啥的…)。熟悉了链表的操作之后,本题的意思就在于从后往前的数,两个指针。第一个先走n步,然后第二个出发,到第一个继续走到最后一个元素的时候,第二个此时的位置的节点就是我们要删除的节点。要注意的是可能要删除的是第一个元素原创 2016-05-05 16:27:40 · 247 阅读 · 0 评论 -
leetcode-25-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.You原创 2016-05-31 22:24:04 · 235 阅读 · 0 评论 -
leetcode-20-Valid Parentheses
题意:判断三种括号出现的次序是否正确。 比如:([{}]),{}[] ,()都为true。{[}]为false。直接用栈。遍历一遍遇到左边的括号入栈,在栈不为空的情况下遇到第一个右边的括号时候判断栈顶元素是否对应。class Solution(object): def isValid(self, s): """ :type s: str :r原创 2016-05-04 19:56:28 · 226 阅读 · 0 评论 -
leetcode-09-Palindrome Number
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool """ b=x if x<0:return False if x>=2147483648:return False原创 2016-04-05 21:35:24 · 602 阅读 · 0 评论 -
leetcode-08-String to Integer (atoi)
cpp:class Solution { public: int myAtoi(string str) { if(str.length() == 0) { return 0; } int i = 0; while(str[i] == ' ')原创 2016-04-05 17:16:53 · 203 阅读 · 0 评论 -
leetcode-07-reverse integer-python
Reverse digits of an integer. 反转一个数,注意符号,溢出范围。题目说的是32位int。 自己的思路:转换成list,先拿出符号,然后不断pop掉的字符加在一起最后如果没溢出就输出。感觉很妙,像堆栈的感觉。class Solution(object): def reverse(self, x): """ :type x: int原创 2016-04-01 11:35:22 · 343 阅读 · 0 评论 -
leetcode-06-zigzag conversion-python
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”. 将字符串以z型重新排列输出。OJ通过。 def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str原创 2016-04-01 10:47:43 · 475 阅读 · 0 评论 -
leetcode-05- Longest Palindromic Substring-python
求最大回文子串。回文子串即为对称的字符串。 本能笨思路:遍历所有元素当作中心元素,左右移位判断是否相等。注意边界问题,还有就是按照是否有中心元素分为两种情况。 代码如下,理所当然没通过。。vs下自己测试了一些应该算法应该没问题。class Solution(object): def longestPalindrome(self,s): maxlen=0 i原创 2016-03-31 16:46:02 · 721 阅读 · 1 评论 -
leetcode04-Median of Two Sorted Arrays-python
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 time complexity should be O(log (m+n)). 找两个序列的中位数。 想当然的思路将两个序列合二为一,原创 2016-03-31 15:11:13 · 1703 阅读 · 0 评论 -
leetcode-03-Longest Substring Without Repeating Characters-python
找出一串字符中最常不重复的字符个数。 o(n*n)的方法class Solution(object): def lengthOfLongestSubstring(self,s): b=[] if len(s)==0: return 0 else: for index,i in enumerate原创 2016-03-30 15:25:56 · 422 阅读 · 0 评论 -
leetcode-001-two sum-python
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example: Given nums = [2, 7, 11,原创 2016-03-29 14:39:47 · 345 阅读 · 1 评论 -
leetcode-10-Regular Expression Matching
正则匹配。最懒的方法,python直接re.match()。。class Solution: # @return a boolean def isMatch(self, s, p): return re.match('^' + p + '$', s) != None 必须要首尾匹配才行。。 例如:a='hello' b='helloworld' re.原创 2016-04-12 22:25:46 · 227 阅读 · 0 评论 -
leetcode-11-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 two lin原创 2016-04-13 14:35:14 · 339 阅读 · 3 评论 -
leetcode-12-integer to roman
百度了一下罗马数字转换规则,百度百科居然就有完整的代码。。 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3; 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12; 小的数字、(限于 Ⅰ、X 和 C)在大的原创 2016-04-13 15:05:15 · 261 阅读 · 0 评论 -
leetcode-15-3sum
题目大意:给一个数组,选出其中所有的3个数加起来等于0的组合。 python:class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ nums.sort()原创 2016-04-19 17:29:19 · 227 阅读 · 0 评论 -
leetcode-22-Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:“((()))”, “(()())”, “(())()”, “()(())”, “()()()”题目大意:原创 2016-05-10 22:41:18 · 314 阅读 · 0 评论 -
leetcode-18-4sum
class Solution { public: vector<vector<int> > fourSum(vector<int> &num, int target) { vector<vector<int> > ret; int len = num.size(); if (len <= 3) return ret;原创 2016-04-28 17:10:08 · 203 阅读 · 0 评论 -
leetcode-21-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。 将两个有序链表按序合并。 主要是链表的操作,与链表有关的题就拿c++做了。思路:依次比较两个链表的当前元素的原创 2016-05-10 17:30:33 · 858 阅读 · 0 评论 -
leetcode-17-Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string “23”原创 2016-04-27 18:32:04 · 291 阅读 · 0 评论 -
leetcode-16-3sum closest
题目:给一个list,一个target。找list中3个数相加最接近target的值。class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int原创 2016-04-27 11:37:20 · 212 阅读 · 0 评论 -
leetcode-14-Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. 题意:求最长前缀字符串。 python:class Solution(object): def longestCommonPrefix(self, strs): """ :type s原创 2016-04-13 17:15:37 · 259 阅读 · 0 评论