
C++
LeoSha
求真务实
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
使用O(n)的时间复杂度合并两个数组
题目描述: 有两个排序数组A1和A2,内存在A1的末尾有足够多的空余空间容纳A2,请实现一个函数,把A2中的所有数字插入到A1中并且所有的数字是有序的。#include<iostream> using namespace std; void Merge(int A1[],int n,int A2[],int m) { int i = n-1; int p = n+m-1; int原创 2015-05-28 14:59:44 · 1298 阅读 · 0 评论 -
和为S的连续正数序列
题目: 输入一个正数s,打印出所有和为S的连续正数序列(至少含有两个数)。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以打印出三个结果。void FindContinuousSequence(int sum) { if(sum < 3) return; int small = 1; int big = 2; int middle原创 2015-06-15 15:44:31 · 722 阅读 · 0 评论 -
旋转数组中查找指定元素
如题,在旋转数组中查找指定元素,考虑到多种情况,网上的方法大部分没有考虑,当low,high,mid三个值相等时的情况。 代码如下:int findAll(int A[],int low,int high,int value)//当三个相等时,查找全部元素的函数。 { for(int i = low;i < high;i++) { if(A[i]==value)原创 2015-06-14 16:03:32 · 1331 阅读 · 0 评论 -
用C++解决:把数组排成最小的数问题
问题描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印出拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这3个数字能拍成的最小数字321323。 C++代码:void PrintMinNumber(int* numbers, int length) { if(numbers == NULL || length <= 0)原创 2015-06-12 20:03:11 · 1941 阅读 · 0 评论 -
Leetcode : 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() an原创 2015-04-13 21:53:28 · 563 阅读 · 0 评论 -
八皇后扩展--n皇后问题 使用回溯法
n皇后问题:回溯的想法,有递归和迭代两种解法。递归更加简洁//迭代解法 #include<iostream> #include<string> #include<algorithm> using namespace std; bool place(int k,int x[])//判断当前放的位置是否符合皇后互斥条件 { for(int i = 0; i < k; i++) {原创 2015-04-18 15:07:03 · 733 阅读 · 0 评论 -
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 transaction (ie, buy one and sell one share of the sto原创 2015-06-29 21:37:06 · 717 阅读 · 0 评论 -
Balanced Binary Tree
问题描述: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-06-29 19:44:49 · 881 阅读 · 0 评论 -
Anagrams
问题描述: Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case. 例如:输入为:{abc,bca,123,321,567} 输出为:{abc,bca,123,321} 解决方案:class Sol原创 2015-06-29 17:19:40 · 952 阅读 · 0 评论 -
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原创 2015-06-29 16:34:31 · 729 阅读 · 0 评论 -
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.Note:•Elements in a triplet (a,b,c) must be in原创 2015-06-29 10:29:38 · 826 阅读 · 0 评论 -
Add Binary
问题描述:Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” Return “100”. 解决方案:class Solution { public: string addBinary(string a, string b) { i原创 2015-06-29 15:49:01 · 881 阅读 · 0 评论 -
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 exactly原创 2015-06-29 11:23:13 · 712 阅读 · 0 评论 -
二叉树节点间最长距离
//节点结构体 struct BinaryTreeNode { BinaryTreeNode* left = NULL; BinaryTreeNode* right = NULL; int floor = 1; }; //查找最大路径,返回路径长度 int FindMaxPath(BinaryTreeNode* t) { if原创 2015-06-03 09:15:24 · 848 阅读 · 0 评论 -
旋转数组的查找问题
题目:一个数组是由一个递增数列右移若干位形成的,比如{4,5,1,2,3}是由{1,2,3,4,5}左移两位形成的,在这种数组中查找某一个数。解题思路如下: 首先获取元素分裂点,时间复杂度为O(log(n)) 因为旋转数组是由递增数组右移得到,因此旋转数组中的第一个元素是整个数组的中间元素,比较待查找元素与第一个元素,如果待查找元素大于等于第一个元素,表明待查找元素在前半段有序数组中;如果不是这原创 2015-05-30 09:56:45 · 1015 阅读 · 0 评论 -
C++递归实现格雷码
#include #include using namespace std; void GrayCode(int n,string *data) { if(n==1) { data[0]="0"; data[1]="1"; return; } GrayCode(n-1,data); int len=(int)pow(2,n); for(int i=len/2;i原创 2015-09-07 16:00:09 · 2632 阅读 · 1 评论