LeetCode - C++
文章平均质量分 78
用C++语言完成Leetcode习题。
讲解编程思路,尽量精益求精。
雪水
寶劍鋒從磨礪出
梅花香自苦寒來
展开
-
LeetCode 014. Longest Common Prefix
Longest Common PrefixWrite a function to find the longest common prefix string amongst an array of strings.找出所有字符串的最长公共前缀思路:先求前两个字符串的最长公共前缀,之后再将之前求出的最长公共前缀与第三个字符串求最原创 2015-01-18 15:01:37 · 1008 阅读 · 0 评论 -
LeetCode 012. Integer to Roman
Integer to RomanGiven an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.罗马数字规则:1、罗马数字共有7个,即I(1)、V(5)、X(10)、L原创 2015-01-17 20:32:07 · 926 阅读 · 0 评论 -
LeetCode 013. Roman to Integer
Roman to Integer Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.从左至右进行遍历,如果当前的字符大于或等于后一个字符,那么总和加上该罗马字原创 2015-01-17 20:36:45 · 792 阅读 · 0 评论 -
LeetCode 011. Container With Most Water
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原创 2015-01-09 15:43:18 · 930 阅读 · 0 评论 -
LeetCode 021. Merge Two Sorted Lists
Merge Two Sorted ListsMerge 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.原创 2014-08-27 00:34:08 · 1013 阅读 · 0 评论 -
LeetCode 005. Longest Palindromic Substring
Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palind原创 2015-01-04 12:36:35 · 1026 阅读 · 1 评论 -
LeetCode 029. Divide Two Integers
Divide Two IntegersDivide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这道题要求不用乘法、除法、模运算来计算两个整数的除法运算。原创 2014-12-29 20:43:20 · 893 阅读 · 0 评论 -
LeetCode 028. Implement strStr()
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(原创 2014-12-29 15:22:38 · 991 阅读 · 0 评论 -
LeetCode 023. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.一开始的思路是先将两个链表合并,然后每次从剩下的链表中取出一个链表与之合并。假设共有m条链表且原创 2014-12-25 16:43:16 · 762 阅读 · 0 评论 -
Leetcode - C++ 目录
LeetCode - C++ 目录LeetCode 001. Two SumLeetCode 002. Add Two NumbersLeetCode 003. Longest Substring Without Repeating CharactersLeetCode 004. Median of Two Sorted ArraysLeetCo原创 2014-12-24 13:26:33 · 1462 阅读 · 0 评论 -
LeetCode 026. Remove Duplicates from Sorted Array
Remove Duplicates from Sorted ArrayGiven 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 an原创 2014-12-24 13:15:49 · 751 阅读 · 0 评论 -
LeetCode 067. Add Binary
Add Binary Total Accepted: 15221 Total Submissions: 58891My SubmissionsGiven two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".原创 2014-08-25 23:27:38 · 861 阅读 · 0 评论 -
LeetCode 061. Rotate List
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.这道题要求原创 2014-08-25 22:39:28 · 775 阅读 · 0 评论 -
LeetCode 034. Search for a Range
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).原创 2014-08-25 12:20:12 · 806 阅读 · 0 评论 -
LeetCode 035. Search Insert Position
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 a原创 2014-08-03 21:18:39 · 770 阅读 · 0 评论 -
LeetCode 019. Remove Nth Node From End of List
Remove Nth Node From End of List Total Accepted: 17264 Total Submissions: 58052My SubmissionsGiven a linked list, remove the nth node from the end of list and return its head.For examp原创 2014-08-03 21:06:11 · 871 阅读 · 0 评论 -
LeetCode 025. Reverse Nodes in k-Group
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 node原创 2014-07-27 20:11:08 · 779 阅读 · 0 评论 -
LeetCode 027. Remove Element
Remove ElementGiven 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 bey原创 2014-07-20 19:54:21 · 720 阅读 · 0 评论 -
LeetCode 017. Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Total Accepted: 12594 Total Submissions: 48534My SubmissionsGiven a digit string, return all possible letter combinations that the number could repr原创 2014-07-20 17:05:15 · 906 阅读 · 0 评论 -
LeetCode 024. Swap Nodes in Pairs
Swap Nodes in Pairs Given 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 shoul原创 2014-07-20 16:18:49 · 951 阅读 · 0 评论 -
LeetCode 020. Valid Parentheses
Valid Parentheses Total Accepted: 14193 Total Submissions: 50886My SubmissionsGiven a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input stri原创 2014-07-19 14:14:54 · 848 阅读 · 0 评论 -
LeetCode 009. Palindrome Number
Palindrome Number Total Accepted: 16218 Total Submissions: 55721My SubmissionsDetermine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hi原创 2014-07-17 22:39:58 · 1647 阅读 · 0 评论 -
LeetCode 007. Reverse Integer
Reverse Integer Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before原创 2014-07-17 22:29:10 · 935 阅读 · 0 评论 -
LeetCode 008. String to Integer (atoi)
String to Integer (atoi) Total Accepted: 12465 Total Submissions: 86466My SubmissionsImplement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases.原创 2014-07-10 22:15:44 · 854 阅读 · 0 评论 -
LeetCode 006. ZigZag Conversion
题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA原创 2014-06-20 09:57:59 · 1175 阅读 · 0 评论 -
LeetCode 002. Add Two Numbers
题目: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原创 2014-06-19 19:35:48 · 1018 阅读 · 0 评论 -
LeetCode 003. Longest Substring Without Repeating Characters
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is原创 2014-06-18 17:10:51 · 935 阅读 · 0 评论 -
LeetCode 151. 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".Clarification:What constitutes a word?A sequence原创 2014-06-17 19:04:37 · 1255 阅读 · 1 评论 -
LeetCode 004. Median of Two Sorted Arrays
题目:There are two sorted arrays A and B 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)).这道题一开始是想用如下方法做的:/***原创 2014-05-07 19:04:00 · 1183 阅读 · 0 评论 -
LeetCode 001. Two Sum
/********************************************************************************************************************************************Given an array of integers, find two numbers such that the原创 2014-04-20 21:55:51 · 1233 阅读 · 0 评论