摘要: 12345678910111213141516171819class Solution {public: int FindGreatestSumOfSubArray (vector array) { if(array .empty ())return 0; int sum =0; int max =-99999999; ... 阅读全文
posted @ 2016-03-16 21:00 copperface 阅读(144) 评论(0) 推荐(0) 编辑
摘要: 给定一个十进制正整数N,写下从1开始,到N的所有整数,然后数一下其中出现的所有“1”的个数。123456789101112131415161718192021222324252627282930313233343536class Solution {public: int NumberOf1Between1AndN_Solution (int n) { if(n1) ... 阅读全文
posted @ 2016-03-16 20:59 copperface 阅读(172) 评论(0) 推荐(0) 编辑
摘要: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。 该算法没有利用数组是有序的这个信息,虽然算法时间也是O(n),但是空间消耗也是O(n)123456789101112131415161718192021222324252627282930313233343536373839404142434445#include #i... 阅读全文
posted @ 2016-03-16 20:56 copperface 阅读(229) 评论(0) 推荐(0) 编辑
摘要: 剑指offer 判断树是不是对称的 递归是很常见的实现方式,最简便。123456789101112131415161718192021222324252627/*struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : ... 阅读全文
posted @ 2016-03-16 20:55 copperface 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 大多数人注意到元素是行列有序的,会马上想到对每行(或列)进行二分查找,每行(或列)需要logN时间,N行(或列)共需要NlogN时间,很容易写出如下代码1234567891011121314151617181920//注意到元素是行列有序的,会马上想到对每行(或列)进行二分查找,每行(或列)需要logN时间,N行(或列)共需要NlogN时间bool Find (vector > array, i... 阅读全文
posted @ 2016-03-16 20:52 copperface 阅读(170) 评论(0) 推荐(0) 编辑
摘要: 因为有序所以用二分法,分别找到第一个k和最后一个k的下标。时间O(logN)class Solution {public: int GetNumberOfK(vector data ,int k) { int num=0; int size=data.size(); if(size>0){ int num1=getfk(data... 阅读全文
posted @ 2016-03-16 20:31 copperface 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 一个整型数组里除了两个数字之外,其他的数字都出现了两次(想到异或性质:任意数字和自己异或都是0,这样每个数字从头到尾异或一遍,出现两次的数字都被消掉了)。请写程序找出这两个只出现一次的数字。要求时间复杂度O(n),空间复杂度O(1)。异或运算符:^分析:利用异或的性质;异或每个数字,最后的结果是两个不同数字的异或结果。想办法把两个不同的数字分到两个数组中,分别异或;分组的方法:把所有数字在二进制下... 阅读全文
posted @ 2016-03-16 20:30 copperface 阅读(217) 评论(0) 推荐(0) 编辑
摘要: 第一种思路: 用栈第二种思路: 先遍历两个链表,得到各自长度,然后将长的那个链表先往后查找 两长度差的 步数。接着同时向后查找,直到找到相同的节点。class Solution {public: ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) { int size1=0,size2... 阅读全文
posted @ 2016-03-16 20:24 copperface 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 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], retu... 阅读全文
posted @ 2016-03-16 20:24 copperface 阅读(144) 评论(0) 推荐(0) 编辑
摘要: (1)一只青蛙一次可以跳上 1 级台阶,也可以跳上2 级。求该青蛙跳上一个n 级的台阶总共有多少种跳法。//递归方式 public static int f(int n) { //参数合法性验证 if (n 2时候循环求值 int res = 0; int a = 1; int b = 1; for (int ... 阅读全文
posted @ 2016-03-16 20:23 copperface 阅读(240) 评论(0) 推荐(0) 编辑