LeetCode算法
MatthiasDong
这个作者很懒,什么都没留下…
展开
-
HouseRobber(即求一串数中不连续的数之和中最大的)
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-05-09 12:01:15 · 377 阅读 · 0 评论 -
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-05-28 11:01:23 · 321 阅读 · 0 评论 -
求字符串中最长回文
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.Hide Tags Stri原创 2015-05-31 00:13:24 · 501 阅读 · 0 评论 -
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, where原创 2015-05-31 17:08:05 · 437 阅读 · 0 评论 -
Repeated DNA Sequences(统计字符串出现次数)
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri原创 2015-05-18 22:16:07 · 456 阅读 · 0 评论 -
实现atoi函数
要求:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or m原创 2015-06-01 23:22:08 · 300 阅读 · 0 评论 -
删除链表倒数第N个元素
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-06-02 22:31:06 · 452 阅读 · 0 评论 -
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 linei is at (i, ai) and (i, 0原创 2015-06-03 00:04:40 · 366 阅读 · 0 评论 -
判断二叉数是否是平衡树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2015-05-19 11:47:00 · 468 阅读 · 0 评论 -
Add Binary(二进制字符串相加)
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".Solutions:刚开始走了弯路,将2进制字符串转化为整型int,相加后结果转化为2进制string。麻烦且字符串太长的话会出错。后来用一个进原创 2015-05-19 16:44:43 · 498 阅读 · 0 评论 -
3sum和4sum(从数组中选3/4个数,使其和为目标值)
都要求结果集中不能有重复的。且每个子集中的元素为非递减顺序。可以从实现O(N^(k-1))的复杂度。利用求两个数相加和为target的算法,扩展到3个和4个的情况。一下给出4个时的程序,3个的情况可以类推。class Solution {public: vector > fourSum(vector& nums, int target) { vector >原创 2015-06-04 16:49:15 · 583 阅读 · 0 评论 -
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:Digit st原创 2015-06-13 23:25:37 · 362 阅读 · 0 评论 -
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-06-14 15:32:54 · 300 阅读 · 0 评论 -
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-06-14 11:38:41 · 333 阅读 · 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-06-14 16:02:30 · 305 阅读 · 0 评论 -
判断是否是二叉查找树
给定一个二叉树,判断它是否是合法的二叉查找树(BST)一棵BST定义为:节点的左子树中的值要严格小于该节点的值。节点的右子树中的值要严格大于该节点的值。左右子树也必须是二叉查找树。样例一个例子: 1 / \ 2 3 / 4 \ 5解答:容易忽略根节点要大于所有左子树原创 2015-09-24 21:22:12 · 659 阅读 · 0 评论 -
求两个有序数组的中位数(扩展求第k大元素)
数组A和数组B都是已经排好序的数组,求其中位数。Solutions: 开辟一个数组,返回其中位数。时间复杂度O(n),空间也是O(n)。class Solution {public: double findMedianSortedArrays(vector& nums1, vector& nums2) { int size1=nums1.size(),size2=num转载 2015-09-29 17:36:09 · 576 阅读 · 0 评论 -
First Bad Version(二分查找的应用)
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the原创 2015-09-23 21:12:00 · 317 阅读 · 0 评论 -
不用额外空间判断一个数是否是回文形式
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin原创 2015-05-29 00:44:09 · 422 阅读 · 0 评论 -
Excel Sheet Column Title(1->A, 2->B, 28->AB )
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB SOLUT原创 2015-05-21 17:20:23 · 398 阅读 · 0 评论 -
ZigZag Conversion(Z形排列的字符/数,求其横向观察结果)
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-05-21 16:28:56 · 746 阅读 · 0 评论 -
实现二分查找树的迭代器(Binary Search Tree Iterator )
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2015-05-09 16:10:32 · 592 阅读 · 0 评论 -
求一个数的阶乘后面有多少个0
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.Solutions:n!可以分解为n!=2x*3y*5z*....。可以得知结果中0的个数取决于5z*。(1,n)的原创 2015-05-10 10:13:12 · 958 阅读 · 0 评论 -
经典算法题之Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assu原创 2015-05-03 22:10:43 · 417 阅读 · 0 评论 -
求数组中出现次数查过n/2的元素
前提:此数组不为空,且这样的元素存在solutions:分析:可以将数组排序,出现在n/2+1位置的一定是所求。故排序的话也可以只排前n/2+1个。class Solution {public: void quikSort(vector& nums, int low1, int high1) { //int low=0, high=nums.size(); int原创 2015-05-11 22:02:56 · 569 阅读 · 0 评论 -
经典算法题之Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.暴力算法:class Solution {public: int rangeBitwiseAnd(int m, int n) { if(0<=m && m<=n && n<=21原创 2015-05-04 22:11:51 · 499 阅读 · 0 评论 -
经典算法题之求小于N的所有素数
Description:Count the number of prime numbers less than a non-negative number, nclass Solution {public: void arrset(int *a, int val, int length) { for(int i=0; i<length; ++i) { *(a+i)=v原创 2015-05-03 22:44:47 · 1729 阅读 · 0 评论 -
经典算法题之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原创 2015-05-01 12:35:29 · 782 阅读 · 0 评论 -
从链表删除所有值为val的元素
问题描述:Remove 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 --> 5/** * Definit原创 2015-05-01 11:02:34 · 674 阅读 · 0 评论 -
经典算法之Isomorphic Strings
问题描述:Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replace原创 2015-05-01 12:27:01 · 396 阅读 · 0 评论 -
经典算法题之就地反转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode*原创 2015-05-06 17:30:17 · 612 阅读 · 0 评论 -
Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",原创 2015-05-19 22:40:39 · 342 阅读 · 0 评论 -
实现字典树Trie的基本操作
字典树:根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。实现字典树的插入insert(),查找search(string key),判断是否有以某字符串为前缀的字符串starsWith(string s)。实现代码如下,可以将bool hasWord改为一个int型来统计原创 2015-05-16 21:41:07 · 551 阅读 · 0 评论 -
将无符号数对应二进制反转
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101原创 2015-05-07 10:38:50 · 778 阅读 · 0 评论 -
转动数组
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 many solutions as yo原创 2015-05-07 12:04:32 · 345 阅读 · 0 评论 -
二叉树层序遍历应用之Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary tree, 1原创 2015-05-07 17:22:41 · 407 阅读 · 0 评论 -
[LeetCode]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-05-27 00:21:45 · 313 阅读 · 0 评论 -
将整数翻转输出
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-05-27 23:51:15 · 481 阅读 · 0 评论 -
求字典序的下一个排列(对应lc的Next Permutation)
如果字典序的下一个排列不存在,则返回升序排序。过程如下:(1) 求满足关系式pj-1 I=max{ j | pj-1(2) 求满足关系式pj-1(3) 交换pj-1和ph的值(4) 从pj开始进行逆序排列class Solution {public: void nextPermutation(vector& nums) { if(n原创 2015-06-20 16:38:51 · 685 阅读 · 0 评论