Leetcode
sherry_0720
这个作者很懒,什么都没留下…
展开
-
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 用pre,和now分别指向头节点,和头节点的下原创 2015-01-31 15:53:33 · 300 阅读 · 0 评论 -
Reverse Linked List
Reverse a singly linked list. 1、迭代法 遍历该单链表,将节点一个一个摘下来,采用 头插法 插入另一条链表: /** * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */原创 2015-05-31 10:19:13 · 145 阅读 · 0 评论 -
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] class Solution { public:原创 2015-05-30 19:48:31 · 244 阅读 · 0 评论 -
Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 右加左減: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數字加小數字。在較大的羅馬數字的左邊記上較小的羅馬數字,表示大數字减小數字。 class Solutio原创 2015-05-31 20:04:12 · 219 阅读 · 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原创 2015-05-30 16:05:52 · 235 阅读 · 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? 对于产生一个新的行用从后原创 2015-05-30 20:40:21 · 241 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". class Solution { public: string addBinary(string a, string b) { int原创 2015-05-31 15:19:04 · 308 阅读 · 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 defi原创 2015-06-27 18:48:41 · 228 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a pa原创 2015-06-28 15:40:57 · 212 阅读 · 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-06-29 15:24:29 · 259 阅读 · 0 评论 -
Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element原创 2015-05-30 16:30:46 · 230 阅读 · 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 Credits: Special thanks原创 2015-04-26 20:20:05 · 275 阅读 · 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. 合并后的序列仍然为有序的。从小到大。 建立一个新的链表来存放,先开辟一个头结点,头结点为空,头结点的下一个结原创 2015-03-08 18:20:01 · 233 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 求出长度L,将tail指针移到最后一个节点位置,tail的下一个节原创 2015-03-15 21:25:35 · 253 阅读 · 0 评论 -
Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from原创 2015-03-07 21:02:44 · 217 阅读 · 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原创 2015-01-31 14:56:47 · 250 阅读 · 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 A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,原创 2015-03-07 20:58:37 · 276 阅读 · 0 评论 -
Plus one
given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. class Solution { pub原创 2015-03-07 20:54:15 · 241 阅读 · 0 评论 -
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]. 题目明确要求了空间复杂度为O(1),那么肯定不能建立新的数组 。每次把最末尾的数移动到开头,移原创 2015-03-06 21:16:18 · 289 阅读 · 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. c原创 2015-03-07 21:01:45 · 218 阅读 · 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-03-07 21:00:06 · 201 阅读 · 0 评论 -
Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘原创 2015-03-08 20:57:48 · 240 阅读 · 0 评论 -
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, "()" and "()[]{}" are all valid b原创 2015-06-28 14:26:23 · 265 阅读 · 0 评论