- 博客(63)
- 收藏
- 关注
原创 基本排序算法--归并排序
#includeusing namespace std;void Merge(int a[], int first, int mid, int last){ int *b = new int[last - first + 1]; int first1 = first; int last1 = mid; int first2 = mid + 1; int last2 = last;
2015-08-25 22:11:07 294
原创 基本排序算法--快速排序
#includeusing namespace std;void QuickSort(int a[], int low,int high){ if (low < high) { int i = low - 1; int key = a[high]; for (int j = low; j < high; j++) { if (a[j] < key) {
2015-08-25 21:53:29 285
原创 基本排序算法--冒泡排序
#includeusing namespace std;void BubbleSort(int a[], int n){ for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (a[j]>a[j + 1]) { int temp = a[j]; a[j]
2015-08-25 20:36:43 231
原创 基本排序算法--堆排序
#includeusing namespace std;void HeapAdjust(int a[], int n,int i){ int left = 2 * i; int right = 2 * i + 1; int max = i; if (left < n&&a[max] < a[left]) max = left; if (right < n&&a[max] <
2015-08-25 16:36:04 285
原创 基本排序算法--选择排序
#includeusing namespace std;void SelectSort(int a[], int n){ for (int i = 0; i < n; i++) { int min = a[i]; int index = i; for (int j = i; j < n; j++) { if (a[j] < min) { min =
2015-08-25 15:50:17 262
原创 基本排序算法--希尔排序
#includeusing namespace std;void ShellSort(int a[], int n){ for (int gap = n / 2; gap > 0; gap /= 2 ) { for (int i = gap; i < n; i++) { int key = a[i]; int j = i - gap; while (j >=
2015-08-25 11:25:26 327
原创 基本排序算法--插入排序
#includeusing namespace std;void InsertSort(int a[], int n){ for (int i = 1; i < n; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && key < a[j]) { a[j + 1] = a[j]; --j; }
2015-08-25 11:15:59 269
转载 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".class Solution {public: void reverseWords(string &s) {
2015-07-26 22:28:15 272
转载 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 ↘
2015-07-26 19:59:55 370
转载 LeetCode---(197)Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve
2015-07-26 16:16:55 346
转载 LeetCode---(155)Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get
2015-07-21 12:49:24 221
转载 LeetCode---(69)Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.利用二分法查找:第一种可能性是直接找到能够麻烦要求的数;第二种可能性是找到相邻的两个数,可以比较两个数哪一个离target更近,不过题目当中希望找的是更小的那个数。class Solution {public: int mySqr
2015-07-16 22:13:13 223
转载 LeetCode---(50)Pow(x, n)
Implement pow(x, n).pow(x,n)就是求x的n次方。x的N次方可以看做:x^n = x^(n/2)*x^(n/2)*x^(n%2)。所以利用递归求解,当n==1的时候,x^n=x。当然n是可以小于0的,2^(-3) = 1/(2^3)。按照上面那个规律就可以解决了。class Solution {public: double myPow(do
2015-07-16 21:26:20 263
转载 LeetCode---(154)Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unk
2015-07-16 20:10:36 237
转载 LeetCode---(153)Find Minimum in Rotated Sorted Array
Suppose a sorted array 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).Find the minimum element.You may assume no duplicate exists in
2015-07-16 19:08:09 219
转载 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
2015-07-15 21:18:41 189
转载 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.empty(
2015-07-15 20:42:41 168
原创 LeetCode---(106)Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree./** * Definition for a binary tree node. * struct Tr
2015-07-15 18:48:33 211
原创 LeetCode---(105)Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.第一个元素即a是根节点,从对应的中序中找到a。从而进一步知道其左边的b在左树中,其右边的c在右树中,这样结合前
2015-07-15 17:28:52 178
原创 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-07-11 13:46:31 172
转载 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-07-11 11:10:52 187
转载 LeetCode---(8)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-07-10 22:11:45 197
转载 LeetCode---(6)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-07-10 19:59:19 188
原创 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-07-10 14:58:28 281
原创 LeetCode---(84)Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width o
2015-06-23 23:22:45 222
原创 LeetCode---(150)Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "
2015-06-23 22:49:10 216
原创 LeetCode---(42)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,1]
2015-06-23 22:06:14 226
原创 LeetCode---(32)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-06-23 16:55:11 225
原创 LeetCode---(20)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-06-23 15:35:04 270
转载 LeetCode---(152)Maximum Product Subarray
Find 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 subarray [2,3] has the largest
2015-06-21 20:01:37 257
原创 LeetCode---(53)Maximum Subarray
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] ha
2015-06-21 10:50:55 279
转载 LeetCode---(137)Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.之前做过了数组中其他数出现两次,仅有一个出现一次的,直接用所有元素异或就行了(只要是偶数次,都可以用这个方法),本题变为其他元素出现3次,而且时间复杂度要求线性,空间为常数。解法为:
2015-06-19 22:15:14 225
转载 LeetCode---(164)Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements
2015-06-19 20:31:53 194
转载 Leetcode---(104)Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.首先考虑递归/** * Definition for a bina
2015-06-17 22:03:51 176
原创 LeetCode---(100)Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.首先考虑递归的方法:
2015-06-17 21:34:54 217
转载 LeetCode---(119)Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].方法一:由于之前我们曾经遇到过建立Pascal三角形,所以我们自然联想到建立一个K+1的三角形,然后取出来第K行,就得到所求的结果。class Soluti
2015-06-16 22:41:23 186
转载 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-06-16 22:04:48 341
转载 LeetCode---(118)Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]将该三角形的左边对齐,就能够发现,tri[i][j] =
2015-06-15 22:54:02 227
转载 LeetCode---(13)Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.罗马数字有如下符号:基本字符IVXLCDM对应阿拉伯数字15105010
2015-06-15 20:03:54 221
转载 Leetcode---(38)Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as
2015-06-15 19:02:07 165
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人