
LeetCode算法分析
LeetCode题目算法分析,C++实现代码,全部代码在LeetCode上得到Accepted
zhihua_bupt
极客码农,Coding the life,Coding the world!!!
展开
-
经典算法——合并K个有序链表
一、题目要求:将K个有序链表合并为一个有序链表二、实现方法:方法一:利用最小堆方法用一个大小为K的最小堆(用优先队列+自定义降序实现)(优先队列就是大顶堆,队头元素最大,自定义为降序后,就变成小顶堆,队头元素最小),先把K个链表的头结点放入堆中,每次取堆顶元素,然后将堆顶元素所在链表的下一个结点加入堆中。整体测试代码:#include原创 2016-06-01 21:46:57 · 16298 阅读 · 1 评论 -
经典算法——合并两个有序链表
题目描述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.完整测试程序:#inc原创 2016-05-06 15:15:57 · 3711 阅读 · 0 评论 -
经典算法——链表中环的入口结点
题目:一个链表中包含环,如何找出环的入口结点?例如,下图所示的链表中,环的入口结点就是结点3完整测试程序:#include using namespace std;struct ListNode{ int val; ListNode* next;};//利用一快一慢两个指针,快指针速度是慢指针的两倍,找到两个指针在环原创 2016-04-22 21:09:44 · 1047 阅读 · 1 评论 -
经典算法——堆排序笔试题
阿里巴巴2016研发工程师笔试选择题1.将整数数组(7-6-3-5-4-1-2)按照堆排序的方式原地进行升序排列,请问在第一轮排序结束之后,数组的顺序是_____。原创 2016-04-21 15:44:48 · 7068 阅读 · 3 评论 -
经典算法——字符流中第一个不重复的字符
题目描述请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。 输出描述:如果当前字符流没有存在出现一次的字符,返回#字符。class Solution{public: int occurr原创 2016-04-21 15:26:42 · 5186 阅读 · 0 评论 -
经典算法——子矩阵的最大累加和问题
一、题目要求给定一个矩阵matrix,其中的值有正有负,有0,返回子矩阵的最大累加和,例如,矩阵matrix为:-90 48 7864 -40 64-81 07 66其中,最大累加和的子矩阵为:48 78-40 64-7 66所以返回累加和209二、解题思路将矩阵matrix[N][N]的每一列的N个元素累加成一个累加数组,然后求出这个数原创 2016-04-16 22:30:09 · 3348 阅读 · 1 评论 -
经典算法——数组的循环右移K位
void Reverse(vector&nums,int p,int q){ for(;p<q;p++,q--) { int temp=nums[q]; nums[q]=nums[p]; nums[p]=temp; }}void RightShift(vector nums,int k){ int n=nums.size(); k%=n; Reverse(nums原创 2016-04-15 20:09:30 · 24215 阅读 · 1 评论 -
经典算法——单链表反转的递归方法和非递归方法
单链表反转有递归和非递归两种实现方法,首先定义链表结点://定义一个链表节点struct ListNode{ int value; ListNode *next;};一、递归方法先反转后面的链表,从最后面的两个结点开始反转,依次向前,将后一个链表结点指向前一个结点,注意每次反转后要将原链表中前一个结点的指针域置空,表示将原链表中前一个结点指向后一个结点的指向关原创 2016-04-08 15:49:28 · 46014 阅读 · 5 评论 -
经典算法——Jump Game(II)
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal i原创 2016-04-08 15:14:14 · 1369 阅读 · 0 评论 -
经典算法——Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i原创 2016-04-08 09:45:31 · 1417 阅读 · 0 评论 -
经典算法——最长回文子序列
最长回文子序列LPS(Longest Palindromic Subsequence)问题一个字符串有许多子序列,比如字符串cabbeaf,它的子序列有c、abb、e、a、f,可以通过删除某些字符而变成回文字符串,字符串“cabbeaf”,删除掉‘c’、'e'、‘f’后剩下的子串“abba”就是回文字符串,也是其中最长的回文子序列。注意和最长回文子串的区别,最长回文子串必须是连续的,这原创 2016-04-04 14:48:31 · 35607 阅读 · 4 评论 -
经典算法——Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the foll原创 2016-04-03 17:05:21 · 1688 阅读 · 0 评论 -
经典算法——二进制中1的个数
题目描述输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。原创 2016-04-03 14:21:22 · 1765 阅读 · 0 评论 -
经典算法——最长回文子串
时间限制:1000ms单点时限:1000ms内存限制:64MB描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。 这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?”原创 2016-04-01 15:04:26 · 8485 阅读 · 1 评论 -
经典算法——连续子数组的最大乘积
Maximum Product SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous s原创 2016-03-31 14:57:02 · 3952 阅读 · 0 评论 -
经典算法——求绝对值溢出问题
Problem Description求实数的绝对值。Input输入数据有多组,每组占一行,每行包含一个实数。Output对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。Sample Input123-234.00Sample Output123.00234.0原创 2016-03-29 12:52:39 · 2219 阅读 · 0 评论 -
经典算法——hihocoder#1014 : Trie树(字典树)
#1014 : Trie树时间限制:10000ms单点时限:1000ms内存限制:256MB描述小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能对于每一个我给出的字符串,都在原创 2016-03-24 14:53:10 · 2466 阅读 · 3 评论 -
经典算法——字符串的所有组合
#include#include#includeusing namespace std;//从头扫描字符串得到第一个字符,针对第一个字符,有两种选择//把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选取m-1个字符;//如果不把这个字符放到组合中去,则需要在剩下的n-1个字符中选取m个字符 void Combination(char* string, int numb原创 2016-03-21 11:32:24 · 6044 阅读 · 0 评论 -
经典排序算法——堆排序
对于一个int数组,请编写一个堆排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class HeapSort {public: int* heapSort(int* A, int n) { BuildMaxHeap(A, n)原创 2016-03-20 20:06:50 · 2004 阅读 · 0 评论 -
经典算法——扑克牌的顺子
题目描述LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12原创 2016-03-19 21:43:50 · 9242 阅读 · 0 评论 -
经典算法——数组中只出现一次的数字
题目描述一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。class Solution {public: void FindNumsAppearOnce(vector data,int* num1,int *num2) { if(data.size()<2) return ;原创 2016-03-19 17:22:00 · 2936 阅读 · 2 评论 -
经典算法——调整数组顺序使奇数位于偶数前面
一、题目描述输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。二、解题思路方法一:冒泡算法思想,当前面数字为偶数,后面数字为奇数时,相互交换,否则不交换原创 2016-03-19 15:42:04 · 2247 阅读 · 1 评论 -
经典算法——数组中的逆序对
一、题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。二、解题方法利用归并排序的思想,先把数组分隔成子数组,先统计出子数组内部的逆序对的数目,然后再统计出两个相邻子数组之间的逆序对的数目。注意在合并两个已排序的子数组后,要更新数组。class原创 2016-03-18 15:24:42 · 12915 阅读 · 0 评论 -
经典算法——左旋转字符串
题目描述汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!解题思路经典的三次翻转:1.先翻转字符串前n个字符;2.再翻转后面原创 2016-03-17 14:31:40 · 2430 阅读 · 0 评论 -
经典排序算法——归并排序
对于一个int数组,请编写一个归并排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class MergeSort {public: int* mergeSort(int* A, int n) { if(A==NUL原创 2016-03-13 13:16:50 · 1579 阅读 · 2 评论 -
经典算法——连续子数组最大和问题
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1原创 2016-03-11 17:22:25 · 1510 阅读 · 0 评论 -
经典算法——包含min函数的栈
题目描述定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。class Solution {public: stack stack1; stack helpStack;//辅助栈,存放最小值 void push(int value) { stack1.push(value);原创 2016-03-09 11:02:11 · 1536 阅读 · 0 评论 -
经典算法——顺时针打印矩阵
题目描述输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.class Solution {public: vector prin原创 2016-03-09 10:21:48 · 3637 阅读 · 0 评论 -
经典排序算法——快速排序
对于一个int数组,请编写一个快速排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class QuickSort {public: int* quickSort(int* A, int n) { // write code here原创 2016-03-05 21:12:30 · 2017 阅读 · 1 评论 -
经典排序算法——选择排序
对于一个int数组,请编写一个选择排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:[1,2,3,5,2,3],6[1,2,2,3,3,5]class SelectionSort {public: int* selectionSort(int* A, int n){ // write code原创 2016-03-05 14:30:07 · 1552 阅读 · 0 评论 -
经典排序算法——插入排序
对于一个int数组,请编写一个插入排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。测试样例:输入数组:[1,2,3,5,2,3],6输出数组:[1,2,2,3,3,5]class InsertionSort {public: int* insertionSort(int* A, int n) { // wr原创 2016-03-05 14:25:52 · 1329 阅读 · 0 评论 -
经典排序算法——冒泡排序
对于一个int数组,请编写一个冒泡排序算法,对数组元素排序。给定一个int数组A及数组的大小n,请返回排序后的数组。 测试样例:输入数组:[1,2,3,5,2,3],6输出数组:[1,2,2,3,3,5]class BubbleSort {public: int* bubbleSort(int* A, int n) {原创 2016-03-05 13:15:24 · 1504 阅读 · 0 评论 -
Leetcode 160: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 → a2 ↘原创 2016-02-26 00:25:25 · 1220 阅读 · 0 评论 -
Leetcode 225:Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet原创 2016-02-25 14:39:32 · 1049 阅读 · 0 评论 -
Leetcode 326:Power of Three
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?Credits:Special thanks to @dietpepsi for adding this原创 2016-02-24 23:59:47 · 1442 阅读 · 0 评论 -
Leetcode 299:Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a guess, you provide a hint t原创 2016-02-24 23:43:50 · 1419 阅读 · 0 评论 -
Leetcode 328:Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-02-24 22:27:10 · 890 阅读 · 0 评论 -
Leetcode 63:Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2016-02-24 00:15:36 · 1567 阅读 · 0 评论 -
Leetcode 62: Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2016-02-23 18:15:59 · 1023 阅读 · 0 评论 -
Leetcode 131:Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.For example, given s = "aab",Return [ ["aa","原创 2016-02-23 00:20:44 · 1930 阅读 · 2 评论