- 博客(49)
- 收藏
- 关注
原创 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 476
原创 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 428
原创 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 573
原创 LeetCode 之 Merge Intervals — C++ 实现
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].给定一个区间集合,合并所有重复区间。例如,给定 [
2015-06-19 11:42:05 576
原创 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 512
原创 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 289
原创 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 349
原创 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 285
原创 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 379
原创 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 276
原创 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 328
原创 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 283
原创 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 340
原创 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 864
原创 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 316
原创 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 373
原创 LeetCode 之 Number of 1 Bits — C 实现
Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary
2015-06-11 21:10:33 272
原创 LeetCode 之 Rotate Array — C++ 实现
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].Note:Try to come up as
2015-06-11 20:59:32 266
原创 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 253
原创 LeetCode 之 Min Stack — C++ 实现
Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the s
2015-06-11 20:34:25 349
原创 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 359
原创 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 301
原创 LeetCode 之 Valid Palindrome — C 实现
Valid PalindromeGiven 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.
2015-06-11 17:59:16 341
原创 LeetCode 之 Pascal's Triangle — C++ 实现
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]]
2015-06-11 17:50:52 503
原创 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 293
原创 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 349
原创 LeetCode 之 Search a 2D Matrix — C 实现
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to righ
2015-06-11 17:08:54 262
原创 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 211
原创 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 271
原创 LeetCode 之 Plus One — C 实现
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.给定一个数
2015-06-11 16:37:59 432
原创 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 291
原创 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 260
原创 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 308
原创 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 335
原创 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 367
原创 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 363
原创 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 256
原创 LeetCode 之 Swap Nodes in Pairs — C 实现
Swap Nodes in PairsGiven 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.Your algorithm should u
2015-06-11 10:59:15 334
原创 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 262
原创 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 366
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人