Davey的专栏

不想再做一枚弱菜~Come On!

排序:
默认
按更新时间
按访问量

快排、堆排序

快排:快速排序主要运用了二分的思想,每次选择一个基准元素,比基准元素打的元素都放在基准元素前面,比基准元素小的元素都放在基准元素后面,这样不断递归细分,完成排序。C++代码实现:void QuickSort(int a[], int l, int r){ int temp = a[l]; ...

2017-08-04 18:10:55

阅读数:168

评论数:0

求二叉树深度、判断是否是平衡二叉树

求二叉树深度:递归的方法:从根节点按照先序顺序遍历二叉树,返回左子树和右子树中较大的深度,再加上1就是根节点的深度。C++代码实现:typedef struct TreeNode{ int data; struct TreeNode* leftchild; struct T...

2017-08-04 13:55:53

阅读数:265

评论数:0

数字在排序数组中出现的次数

题目:统计一个数字在排序数组中出现的次数。例如输入{1,2,3,3,3, 3,4,5}和数字3,那么输出应该是4,因为3出现了4次。思路: 直接暴力求解时间复杂度为O(n) ,下面利用二分查找的思想,给出时间复杂度为O(logn) 的算法: 1. 利用二分查找的思想,找到第一个出现的K,时间复...

2017-08-02 22:32:59

阅读数:146

评论数:0

查找树中两个节点的最低公共祖先

求树中两个节点的最低公共祖先给定一棵树和两个节点,求解这两个节点在树中的最低公共祖先节点。(剑指Offer)思路: 从根节点遍历树,直到要查找的节点,保存从根节点到要查找的节点的路径。遍历两次树,即保存了根节点到要查找的两个节点的两条路径,然后求出两条路径的最后一个交点即可。C++代码实现:#i...

2017-08-02 20:56:37

阅读数:189

评论数:0

LeetCode -- 650. 2 Keys Keyboard

题目:Initially on a notepad only one character ‘A’ is present. You can perform two operations on this notepad for each step:Copy All: You can copy all ...

2017-08-02 11:49:22

阅读数:1259

评论数:0

C语言中atoi()函数实现--字符串转int型整数

C语言中有个atoi()函数,将字符串转成整数,返回int类型的数值。思路很简单,但是有很多边界和细节要处理,本文参考剑指offer上的实例,仅供参考。#include <iostream> #include <string>enum Status{kValid = 0, ...

2017-08-01 20:21:53

阅读数:504

评论数:0

LeetCode -- 213. House Robber II

题目:After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. Th...

2017-08-01 17:40:25

阅读数:92

评论数:0

LeetCode -- 198. House Robber

题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stoppin...

2017-08-01 16:28:47

阅读数:104

评论数:0

LeetCode -- Range Sum Query - Immutable

题目:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0,...

2017-08-01 10:54:16

阅读数:61

评论数:0

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,...

2017-07-26 21:53:53

阅读数:110

评论数:0

LeetCode -- 88. Merge Sorted Array

题目:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (siz...

2017-07-26 21:15:06

阅读数:66

评论数:0

LeetCode -- 66. Plus One

题目:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any l...

2017-07-25 23:42:54

阅读数:92

评论数:0

LeetCode -- 122. Best Time to Buy and Sell Stock II

题目:Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may c...

2017-05-26 09:26:24

阅读数:116

评论数:0

LeetCode -- 121. Best Time to Buy and Sell Stock

题目:Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transa...

2017-05-25 17:02:00

阅读数:151

评论数:0

LeetCode -- 120. Triangle

题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the...

2017-05-25 16:12:31

阅读数:124

评论数:0

LeetCode -- 96. Unique Binary Search Trees

题目:Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique B...

2017-05-17 23:20:26

阅读数:96

评论数: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 y...

2017-05-17 11:15:14

阅读数:92

评论数:0

LeetCode -- 64. Minimum Path Sum

题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its pa...

2017-05-17 11:00:03

阅读数:96

评论数: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...

2017-05-17 10:21:01

阅读数:94

评论数: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 ...

2017-05-17 09:41:31

阅读数:163

评论数:0

提示
确定要删除当前文章?
取消 删除
关闭
关闭