LeetCode
文章平均质量分 70
abc123man
因为喜欢,所以努力。。。
展开
-
LeetCode 之 Delete Node in a Linked List — C++ 实现
Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and yo原创 2015-07-27 17:30:23 · 346 阅读 · 0 评论 -
LeetCode 之 Palindrome Linked List — C++ 实现
Palindrome Linked List Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?给定一个单链表,判断是不是回文。追问:可否实现时间复杂度为 O(n),空间复杂度为 O原创 2015-07-10 21:34:17 · 640 阅读 · 0 评论 -
LeetCode 之 Populating Next Right Pointers in Each Node I II — C++ 实现
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate原创 2015-07-04 20:37:44 · 334 阅读 · 0 评论 -
LeetCode 之 Contains Duplicate I II III — C++ 实现
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 retur原创 2015-06-11 21:43:24 · 854 阅读 · 0 评论 -
LeetCode 之 Sort List — C 实现
Sort List Sort a linked list in O(n log n) time using constant space complexity.对链表进行排序,要求时间复杂度为O(nlogn),空间复杂度为 O(1)。分析: 归并排序。struct ListNode* mergeSort(struct ListNode* head)原创 2015-06-11 19:20:44 · 293 阅读 · 0 评论 -
LeetCode 之 Merge Sorted Array — C++ 实现
Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or eq原创 2015-06-11 17:44:09 · 288 阅读 · 0 评论 -
LeetCode 之 Sqrt(x) — C 实现
Sqrt(x) Implement int sqrt(int x).Compute and return the square root of x.实现 int sqrt(int x)。计算并返回x的平方根。分析:二分法实现。注意计算平方可能溢出。int mySqrt(int x) { int left = 0, right =原创 2015-06-11 17:02:15 · 202 阅读 · 0 评论 -
LeetCode 之 Add Binary — C 实现
Add BinaryGiven two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".给定两个二进制字符串,返回它们的和(仍是二进制字符串)。例如,a = "11"b = "1"Return "100"原创 2015-06-11 16:51:55 · 255 阅读 · 0 评论 -
LeetCode 之 Rotate List — C 实现
Rotate ListGiven 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给定一个链表,向右旋转 k (k 为非原创 2015-06-11 15:24:19 · 278 阅读 · 0 评论 -
LeetCode 之 Length of Last Word — C 实现
Length of Last WordGiven 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, ret原创 2015-06-11 15:04:50 · 252 阅读 · 0 评论 -
LeetCode 之 Reverse Linked List — C 实现
Reverse Linked List Reverse a singly linked list.翻转链表。struct ListNode* reverseList(struct ListNode* head) { struct ListNode *pre = NULL, *cur = NULL, *ne = NULL; if(head == NULL)原创 2015-06-11 21:30:48 · 301 阅读 · 0 评论 -
LeetCode 之 Kth Largest Element in an Array — C++ 实现
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,原创 2015-07-11 17:37:55 · 430 阅读 · 0 评论 -
LeetCode 之 Linked List Cycle I II — C++ 实现
Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?给定一个链表,验证是否存在环。附加:能否不额外分配空间。分析: 维护两个指针,ne 每次移动两个节点原创 2015-06-13 17:33:55 · 357 阅读 · 0 评论 -
LeetCode 之 Remove Nth Node From End of List — C++ 实现
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 r原创 2015-06-13 16:55:50 · 271 阅读 · 0 评论 -
LeetCode 之 Reverse Integer — C++ 实现
Reverse Integer Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321将一个整数按数字翻转。分析: 每次取数字的最低位作为翻转后整数的最高位,计算时就要进行溢出判断,为防止溢出用 long long i原创 2015-06-14 14:16:24 · 271 阅读 · 0 评论 -
LeetCode 之 Palindrome Number — C++ 实现
Palindrome NumberDetermine whether an integer is a palindrome. Do this without extra space.确定一个整数是不是回文。要求空间复杂度为O(1).负数不是回文分析: 首先计算正数是 10 的几次幂,然后分别取最高位和最低位比较,如果不相等则不是回文,相等则去掉最高位和最低位继原创 2015-06-14 14:24:28 · 502 阅读 · 0 评论 -
LeetCode 之 Rotate Image — C/C++ 实现
Rotate Image You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?void rotate(int** matrix,原创 2015-06-11 14:24:57 · 291 阅读 · 0 评论 -
LeetCode 之 Search for a Range — C++ 实现
Search for a RangeGiven 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原创 2015-06-11 14:15:28 · 297 阅读 · 0 评论 -
LeetCode 之 Implement Queue using Stacks — C++ 实现
Implement Queue using Stacks Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue原创 2015-07-11 18:08:52 · 766 阅读 · 0 评论 -
LeetCode 之 Add Two Numbers — C 实现
Add Two NumbersYou 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 an原创 2015-06-11 08:18:28 · 361 阅读 · 0 评论 -
LeetCode 之 Intersection of Two Linked Lists — C/C++ 实现
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原创 2015-06-13 17:52:55 · 328 阅读 · 0 评论 -
LeetCode 之 Insertion Sort List — C++ 实现
Insertion Sort List Sort a linked list using insertion sort.对链表使用插入排序。class Solution {public: ListNode* insertionSortList(ListNode* head) { if(!head) { ret原创 2015-06-13 17:49:42 · 270 阅读 · 0 评论 -
LeetCode 之 Partition List — C++ 实现
Partition ListGiven 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原创 2015-06-13 17:20:06 · 260 阅读 · 0 评论 -
LeetCode 之 Merge Two / k Sorted Lists — C/C++ 实现
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-06-13 16:36:43 · 325 阅读 · 0 评论 -
LeetCode 之 Remove Duplicates from Sorted List I II — C++ 实现
Remove Duplicates from Sorted ListGiven 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, retu原创 2015-06-13 17:08:03 · 312 阅读 · 0 评论 -
LeetCode 之 Remove Linked List Elements — C 实现
Remove Linked List ElementsRemove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4原创 2015-06-11 21:25:37 · 363 阅读 · 0 评论 -
LeetCode 之 Compare Version Numbers — C 实现
Compare Version Numbers Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the vers原创 2015-06-11 20:41:37 · 235 阅读 · 0 评论 -
LeetCode 之 Reverse Words in a String — C 实现
Reverse Words in a String Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".Update (2015-02-12):For C progra原创 2015-06-11 20:17:07 · 345 阅读 · 0 评论 -
LeetCode 之 Implement strStr() — C 实现
Implement strStr() Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.实现 strStr().返回字串在字符串中第一次出现的位置,不存在则返回-1.原创 2015-06-11 11:25:54 · 347 阅读 · 0 评论 -
LeetCode 之 Longest Common Prefix — C++实现
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.找出字符串数组中字符串的最大共同前缀。分析:只需要将前两个字符串比较找出最大共同前缀,然后使用共同前缀再和其他字符串比较,这样就只需要比较前缀部分。原创 2015-06-11 10:37:19 · 356 阅读 · 0 评论 -
LeetCode 之 Reverse Nodes in k-Group — C 实现
Reverse Nodes in k-GroupGiven 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原创 2015-06-11 11:12:53 · 247 阅读 · 0 评论 -
LeetCode 之 Valid Parentheses — C++ 实现
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-06-11 10:44:41 · 249 阅读 · 0 评论 -
LeetCode 之 Remove Duplicates from Sorted Array I II — C 实现
Remove Duplicates from Sorted Array I 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 spa原创 2015-06-10 21:45:40 · 246 阅读 · 0 评论 -
LeetCode 之 Search Insert Position — C 实现
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-06-10 20:00:03 · 227 阅读 · 0 评论 -
LeetCode 刷题之旅
最近开始在 LeetCode 刷题,后来发现在两台不同的电脑上写下的代码竟然没有保存,突然觉得辛辛苦苦写出的代码一点痕迹都没有留下,为了保留自己的劳动成果,也为了方便自己和他人查看,决定在 CSDN 上留下自己的脚印,方便大家交流,请不吝赐教。下面就开始我的刷题之旅。。。原创 2015-06-09 11:04:12 · 455 阅读 · 0 评论 -
LeetCode 之 Search in Rotated Sorted Array I II — C/C++ 实现
Search in Rotated Sorted ArraySuppose 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原创 2015-06-11 14:09:27 · 358 阅读 · 0 评论 -
LeetCode 之 Largest Number — C++ 实现
Largest Number Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.N原创 2015-06-19 17:58:13 · 453 阅读 · 0 评论 -
LeetCode 之 Two Sum — C++ 实现
Two SumGiven 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 th原创 2015-06-19 16:55:29 · 394 阅读 · 0 评论 -
LeetCode 之 Evaluate Reverse Polish Notation — C++ 实现
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expre原创 2015-06-19 16:15:29 · 560 阅读 · 0 评论 -
LeetCode 之 Sort Colors — C 实现
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we wi原创 2015-06-11 17:18:15 · 323 阅读 · 0 评论