leetcode
鹏割
勿忘初心
展开
-
leetcode Reverse integer
题目: Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321Here are some good questions to ask before coding. Bonus points for you if you have already though原创 2015-07-09 22:25:29 · 474 阅读 · 0 评论 -
linked list cycle
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?快慢指针class Solution{ public: bool hasCycle(ListNode *head)原创 2016-03-17 10:37:17 · 247 阅读 · 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./** * Definition for singly-linked list. * struct Li原创 2016-03-17 16:36:15 · 294 阅读 · 0 评论 -
rotate list
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given1->2->3->4->5->NULLand k =2, return4->5->1->2->3->NULL.循环右移链表,注意k可能大于链表长度/** * Definition f原创 2016-03-18 11:13:24 · 292 阅读 · 0 评论 -
reverse linked list II
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given1->2->3->4->5->NULL, m = 2 and n = 4, return1->4->3->2->5->NULL. Note: Given m, n satisfy the fo原创 2016-03-18 16:36:32 · 268 阅读 · 0 评论 -
remove duplicates from sorted list
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given1->1->2, return1->2. Given1->1->2->3->3, return1->2->3./** * Definition for singly-原创 2016-03-18 23:11:37 · 387 阅读 · 0 评论 -
partition list
题目: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in ea原创 2016-03-19 10:52:08 · 301 阅读 · 0 评论 -
remove duplicates from sorted list II
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given1->2->3->3->4->4->5, return1->2->5. Given1->1->原创 2016-03-19 15:46:34 · 342 阅读 · 0 评论 -
add Two Numbers
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-04-01 22:06:45 · 272 阅读 · 0 评论 -
copy-list-with-random-pointer
题目: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list./** * Definition for singly-lin原创 2016-03-14 13:56:46 · 303 阅读 · 0 评论 -
convert sorted list to binary search tree
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.先找到中间结点作为二叉搜索树根节点,然后递归。/** * Definition for singly-linked list. * struct ListNode {原创 2016-03-14 16:22:33 · 327 阅读 · 0 评论 -
swap nodes in pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given1->2->3->4, you should return the list as2->1->4->3. Your algorithm should use only constant space. You may原创 2016-04-07 23:14:49 · 285 阅读 · 0 评论 -
longest consecutive sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given[100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is[1, 2, 3, 4].原创 2016-04-12 23:33:21 · 413 阅读 · 0 评论 -
merge k sorted lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.class Solution{ public: ListNode* mergeKLists(vector<ListNode*> &lists) { if (lists.原创 2016-04-09 23:56:16 · 351 阅读 · 0 评论 -
reorder list
题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes’ values. For example, Given{1,2,3,4}, reorder it to{1原创 2016-03-16 10:55:15 · 241 阅读 · 0 评论 -
sort list
题目: Sort a linked list in O(n log n) time using constant space complexity.//merge sort/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN原创 2016-03-15 18:59:03 · 266 阅读 · 0 评论 -
leetcode Median of Two Sorted Arrays
class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int total = m + n; if (total & 0x1) return find_kth(A, m, B, n, total / 2 + 1); else return (fi原创 2016-02-06 23:26:15 · 285 阅读 · 0 评论 -
leetcode 1 twoSum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-06-13 19:35:02 · 457 阅读 · 0 评论 -
leetcode Palindrome Number (判断整数是否为回文)
题目: Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints: Could negative integers be palindromes? (ie, -1)If you are thinking of converting the原创 2015-07-13 21:39:42 · 615 阅读 · 0 评论 -
leetcode Roman to integer
题目: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.要把罗马数字转换为整数, 罗马数字自行百度code:class Solution{ public: int romanToInt(string s) {原创 2015-07-14 22:49:51 · 555 阅读 · 0 评论 -
leetcode longestCommonPrefix
题目: Write a function to find the longest common prefix string amongst an array of strings.就是要求一些字符串的最长公共前缀。code: class Solution { public: string longestCommonPrefix(vector& strs) {原创 2015-07-17 12:35:36 · 542 阅读 · 0 评论 -
leetcode 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 cons原创 2015-08-10 18:35:54 · 604 阅读 · 0 评论 -
leetcode Remove Nth Node From End of List
first:class Solution{ public: ListNode* removeNthFromEnd(ListNode* head, int n) { if (head==NULL) { return NULL; } ListNode* ptmp = head;原创 2015-07-20 10:17:48 · 466 阅读 · 0 评论 -
leetcode 2 Add two numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode*原创 2015-06-18 00:14:41 · 854 阅读 · 0 评论 -
leetcode Valid Parentheses
今天看书的时候发现一个类里的一个成员函数是这样的: operator int() const; 。。。以前从来没见过。 这玩意儿叫做转换函数,它是一种特殊的类成员函数。 它定义将类类型值转变为其他类型值的转换。 转换操作符在类定义体内声明, 在保留字operator之后跟着转换的目标类型:一. 转换函数采用如下通用形式: operator type();原创 2015-07-31 23:29:32 · 463 阅读 · 0 评论 -
leetcode Merge Two Sorted Lists
class Solution{ public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL) { return l2; } if (l2 == NULL)原创 2015-08-05 20:03:26 · 439 阅读 · 0 评论 -
leetcode 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.class Solu原创 2015-08-13 15:53:06 · 590 阅读 · 0 评论 -
Implement strStr
mplement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.先来暴力解法: (kmp看懂后再补。。)class Solution { public: int strStr(st原创 2015-10-16 21:27:12 · 266 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elemen原创 2015-10-23 22:01:29 · 297 阅读 · 0 评论 -
leetcode String to Integer (atoi)
题目: Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input c原创 2015-07-12 23:20:33 · 687 阅读 · 0 评论 -
remove nth node from end of list
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 = 2.After removing the second node from the end, the linked原创 2016-04-10 23:18:20 · 235 阅读 · 0 评论