
LeetCode
zhihua_bupt
极客码农,Coding the life,Coding the world!!!
展开
-
LeetCode4:Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 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)).一、题目描述给定两个大小分别为m和n的原创 2015-10-17 20:00:15 · 777 阅读 · 0 评论 -
LeetCode3: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 3. Fo原创 2015-10-16 22:53:09 · 618 阅读 · 0 评论 -
LeetCode5: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 palindromic substring.一、题目描述求最长回文子原创 2015-10-20 00:44:24 · 589 阅读 · 0 评论 -
LeetCode6: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 P L S I原创 2015-10-21 00:06:42 · 668 阅读 · 0 评论 -
LeetCode16:3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exa原创 2015-11-06 21:39:15 · 556 阅读 · 0 评论 -
LeetCode7:Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2015-10-21 22:27:20 · 580 阅读 · 0 评论 -
LeetCode1:Two Sum
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-10-07 16:49:40 · 757 阅读 · 0 评论 -
LeetCode8: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 ca原创 2015-10-23 00:32:17 · 630 阅读 · 0 评论 -
LeetCode17:Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digi原创 2015-11-08 15:03:16 · 541 阅读 · 0 评论 -
LeetCode18:4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2015-11-08 15:44:58 · 628 阅读 · 0 评论 -
LeetCode9:Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.判断一个整数是否为回文,解决思路很简单,就是不断取整数的第一位和最后一位进行比较,相等则继续取第二位和倒数第二位,直到整数的所有位都比较完成或者中途找到了不一致的位。#include#includeusing namespace std;原创 2015-10-24 23:23:08 · 704 阅读 · 0 评论 -
LeetCode10:Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input原创 2015-10-26 10:43:30 · 770 阅读 · 0 评论 -
LeetCode11: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 line i is at (i, ai) and (i, 0). Fin原创 2015-10-28 23:58:51 · 716 阅读 · 0 评论 -
LeetCode19: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, th原创 2015-11-14 00:24:39 · 630 阅读 · 0 评论 -
LeetCode12:Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.#include#include#includeusing namespace std;class Solution {public: string int原创 2015-10-29 22:27:05 · 588 阅读 · 0 评论 -
LeetCode13:Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.#include#include#include#includeusing namespace std;class Solution {public: in原创 2015-10-29 23:17:09 · 605 阅读 · 0 评论 -
LeetCode2: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 link原创 2015-10-15 22:12:13 · 574 阅读 · 0 评论 -
LeetCode14:Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.要求找出字符串数组中所有字符串的最长公共前缀。解题思路:两个字符串的最长公共前缀,其长度肯定不会超过最短的字符串的长度,设最短的字符串长度为m,那么只要比较这两个字符串的前m个字符即可。每次得出两个字符串的最长公共前原创 2015-10-30 22:20:33 · 575 阅读 · 0 评论 -
LeetCode23:Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;原创 2015-11-23 12:27:16 · 718 阅读 · 0 评论 -
LeetCode20: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 va原创 2015-11-18 22:21:13 · 733 阅读 · 0 评论 -
LeetCode 235:Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2015-12-11 00:31:53 · 1357 阅读 · 0 评论 -
LeetCode 70:Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?//设f(n)表示爬n阶楼梯的不同方法,有两种选择:/原创 2015-12-11 16:34:34 · 935 阅读 · 0 评论 -
LeetCode 206:Reverse Linked List
Reverse a singly linked list.一、题目描述 翻转单链表二、解题思路利用两个结点指针和一个中间结点指针temp(用来记录当前结点的下一个节点的位置),分别指向当前结点和前一个结点,每次循环让当前结点的指针域指向前一个结点即可,翻转结束后,记得将最后一个节点的链域置为空。class Solution {public: List原创 2015-12-11 11:21:42 · 681 阅读 · 0 评论 -
LeetCode15:3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-11-02 11:03:51 · 617 阅读 · 0 评论 -
LeetCode 191:Number of 1 Bits
Write 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 representation 000000原创 2015-12-11 09:38:56 · 982 阅读 · 0 评论 -
LeetCode 169: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-12-10 23:47:39 · 1126 阅读 · 0 评论 -
LeetCode 83: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.Subscribe to see wh原创 2015-12-11 17:11:56 · 1451 阅读 · 0 评论 -
LeetCode 263:Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2015-12-12 23:20:24 · 2889 阅读 · 0 评论 -
LeetCode 22:Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "原创 2015-11-19 15:45:10 · 551 阅读 · 0 评论 -
LeetCode21: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 ListNo原创 2015-11-19 00:42:10 · 600 阅读 · 0 评论 -
LeetCode 24: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 should use only constant space. Y原创 2015-11-25 23:54:57 · 689 阅读 · 0 评论 -
LeetCode 99:Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devis原创 2015-12-28 15:37:36 · 3455 阅读 · 0 评论 -
LeetCode 111:Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.//递归方法求二叉树的最小深度 //注意判断树的深度应该到叶子节点原创 2015-12-28 16:16:40 · 1479 阅读 · 0 评论 -
LeetCode 202:Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-12-13 00:45:52 · 1352 阅读 · 0 评论 -
LeetCode 232: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.peek() -- Get the front element.em原创 2015-12-13 19:31:59 · 1935 阅读 · 0 评论 -
LeetCode 231:Power of Two
Given an integer, write a function to determine if it is a power of two.//题目要求:求一个数是否是2的幂次方//解题方法://方法一:如果某个值是2的幂次方所得,其对应二进制则是最高位为1,其余位为0.//n-1则相反,除了最高位,其余比特位均为1,则我们可以判断n&(n-1)是否等于0来判断n是否是2的幂原创 2015-12-13 20:11:40 · 1745 阅读 · 2 评论 -
LeetCode 113:Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2015-12-28 17:44:48 · 1674 阅读 · 0 评论 -
LeetCode 107:Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-12-15 12:27:55 · 1146 阅读 · 0 评论 -
LeetCode 102:Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-12-15 12:33:53 · 932 阅读 · 0 评论 -
LeetCode 58: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原创 2015-11-27 16:11:54 · 735 阅读 · 0 评论