算法分析
Stella-Song
这个作者很懒,什么都没留下…
展开
-
【LeetCode算法练习(C++)】Multiply Strings
题目: Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: The length of both num1 and num2 is < 110. Both num1 and num2 contains only digi原创 2018-01-09 10:42:09 · 387 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Substring with Concatenation of All Words
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once a原创 2017-11-12 20:43:49 · 169 阅读 · 0 评论 -
【LeetCode算法练习(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.链接:Implement strStr() 解法:两重循环暴力求解。时间O(mn)class Solution {public:原创 2017-10-25 18:32:48 · 119 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Remove Element
题目: Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.原创 2017-10-25 17:04:28 · 134 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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 w原创 2017-10-25 16:56:23 · 140 阅读 · 0 评论 -
【LeetCode算法练习(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.连接:Merge Two Sorted Lists 解法:比较两链表头值大小,将小者插入新链表尾,若任一链表为空原创 2017-10-16 10:16:00 · 165 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Divide Two Integers
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.链接:Divide Two Integers 解法:将除法转换为减法,被除数每次减去的值翻倍,注意符号的变化和临界值处理。时间O(logn)class Soluti原创 2017-11-01 13:59:06 · 176 阅读 · 0 评论 -
【LeetCode算法练习(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, “()” and “()[]{}” are all vali原创 2017-10-13 15:13:00 · 189 阅读 · 0 评论 -
【LeetCode算法练习(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 removing the second node from the end, the l原创 2017-10-13 14:56:42 · 176 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possib原创 2017-11-13 09:32:51 · 196 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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 “()”, wh原创 2017-11-13 18:39:39 · 164 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Search in Rotated Sorted Array
题目: Suppose an array sorted in ascending order 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 search. If found原创 2017-11-20 22:15:57 · 138 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Trapping Rain Water
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2原创 2018-01-07 17:57:40 · 223 阅读 · 0 评论 -
【算法习题】证明吝啬SAT问题为NP完全问题
题目: 吝啬SAT问题是这样的:给定一组子句(每个子句都是其中文字的析取)和整数k,求一个最多有k个变量为true的满足赋值——如果该赋值存在。证明吝啬SAT是NP-完全问题。SAT问题: 假设有子句: (a⋃b⋃c)⋂(a⋃b¯)⋂(b⋃c¯)(a¯⋂c)⋂(a¯⋃b¯⋃c¯) 求a,b,c的取值(true or false)使得该表达式的结果是true。不难发现,这个表达式不存在一个合适原创 2017-12-28 20:19:35 · 469 阅读 · 0 评论 -
【LeetCode算法练习(C++)】First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constan原创 2017-12-26 10:53:44 · 246 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Search for a Range
题目: Given an array of integers sorted in ascending order, 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 the原创 2017-11-27 09:39:39 · 186 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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 assume no duplicates in the arr原创 2017-12-04 10:30:53 · 202 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combin原创 2017-12-20 15:08:27 · 290 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Combination Sum
题目: Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen原创 2017-12-11 11:20:30 · 214 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Count and Say
题目: The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as “one 1” or 11. 11 is原创 2017-12-11 09:13:28 · 267 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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. k is a positive integer and is less than or equal to the length of the linked list. If the numbe原创 2017-10-20 16:00:50 · 283 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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.原创 2017-10-20 14:51:22 · 142 阅读 · 0 评论 -
【LeetCode算法练习(C语言)】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters.Examples: Given “abcabcbb”, the answer is “abc”, which the length is 3. Given “bbbbb”, the answer is “b”, w原创 2017-09-07 11:16:47 · 354 阅读 · 0 评论 -
【LeetCode算法练习(C语言)】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)).Example 1: nums1 = [1,原创 2017-09-09 15:08:44 · 238 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings.链接:Longest Common Prefix 解法:逐个字符比较,时间O(N*L),N是字符串个数,L是最长前缀的长度class Solution {public: string longestComm原创 2017-09-25 23:18:36 · 139 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Roman to Integer
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.链接:Roman to Integer 解法:用数组存字母表示的数class Solution { public: int romanToInt(string原创 2017-09-25 23:00:43 · 135 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Integer to Roman
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. ps: I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000;链接:Integer to Rom原创 2017-09-24 12:29:21 · 173 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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). Find t原创 2017-09-24 12:17:42 · 273 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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 in转载 2017-09-23 21:21:30 · 215 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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原创 2017-09-13 15:53:07 · 181 阅读 · 0 评论 -
【LeetCode算法练习(C语言)】Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and retu原创 2017-09-07 10:38:28 · 340 阅读 · 0 评论 -
【LeetCode算法练习(C语言)】Two Sum
【LeetCode算法练习(C语言)】Two Sum题目:给定一个整型数组,返回两个数的下标,满足两个数相加为一个特定整数。假定只有一个正确答案 例如: nums = [2, 7, 11, 15], target = 9,因为nums[0] + nums[1] = 2 + 7 = 9,返回[0,1] 链接:Two Sum解法:穷举,时间O(n^2)int* twoSum(int* nums,原创 2017-09-06 21:16:56 · 443 阅读 · 0 评论 -
【LeetCode算法练习(C语言)】 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.Example: Input: “babad” Output: “bab” Note: “aba” is also a valid answer.Exam原创 2017-09-09 17:00:56 · 290 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Merge k Sorted Lists
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.链接:Merge k Sorted Lists 解法:递归实现归并,利用merge Two Lists,每次合并两个序列。时间O(nlogn)class Solution {public:原创 2017-10-18 17:38:53 · 188 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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: [ “((()))”, “(()())”, “(())()”, “原创 2017-10-18 15:24:43 · 147 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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: The solutio原创 2017-10-09 08:56:13 · 239 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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 s原创 2017-10-08 16:07:30 · 319 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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. For example, given array S = [-1, 0, 1原创 2017-10-08 15:19:26 · 216 阅读 · 0 评论 -
【LeetCode算法练习(C++)】Palindrome Number
题目: Determine whether an integer is a palindrome. Do this without extra space.链接:Palindrome Number 解法:循环,与itoa类似,时间O(n)class Solution {public: bool isPalindrome(int x) { int ans = 0, y原创 2017-09-19 20:28:02 · 283 阅读 · 0 评论 -
【LeetCode算法练习(C++)】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原创 2017-09-27 16:41:03 · 228 阅读 · 0 评论