上一页 1 ··· 9 10 11 12 13 14 15 下一页
摘要: 在一个长度为n的数组中找出出现次数超过(n+1)/2次的数 说明请参考编程之美中的2.3 class Solution { public: int majorityElement(vector<int>& nums) { int candidate; int ntimes,i; for(ntimes 阅读全文
posted @ 2016-01-16 21:05 Breeze0806 阅读(335) 评论(0) 推荐(0) 编辑
摘要: 有n盏关着的灯,第k轮把序号为k倍数的关着的灯打开,开着的灯关闭。class Solution {public: int bulbSwitch(int n) { return (int)sqrt(n*1.0); }}; 阅读全文
posted @ 2016-01-16 20:59 Breeze0806 阅读(205) 评论(2) 推荐(0) 编辑
摘要: 用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。交易次数不限,但一次只能交易一支股票,也就是说手上最多只能持有一支股票,求最大收益。 关键:能赚就赚 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 阅读全文
posted @ 2016-01-16 20:35 Breeze0806 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 找出数组中重复的数,裸的map和set题 1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 map<int,int> mii; 5 for (int i = 0;i<nums.size() ;++ 阅读全文
posted @ 2016-01-16 20:23 Breeze0806 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 本质是把26进制转化为10进制 A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 1 class Solution { 2 public: 3 int titleToNumber(string s... 阅读全文
posted @ 2016-01-16 20:13 Breeze0806 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 1 class Solution { 2 public: 3 int maxDepth(TreeNode* root) { 4 if(!root) return 0; 5 else return max(maxDepth(ro 阅读全文
posted @ 2016-01-16 20:07 Breeze0806 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次)同样用亦或来解决(参考编程之美的1.5)先去取出总亦或值然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1]a&(-a)就是计算出这个数,可以参考树状数组。最后就是注意(nums[i] & a) == 0要加()。注意符号运... 阅读全文
posted @ 2016-01-16 20:02 Breeze0806 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 就是判断两棵树的值和结构是否相同注意:要判断是否所有的树节点是否为NULL 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; ... 阅读全文
posted @ 2016-01-16 18:51 Breeze0806 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 给出一个数组 nums[i](i = 0,1,...,n-1) 输出数组output[i]满足output[i] =nums[0] * num[1] * num[2] *..*num[i-1] * num[i+1]*... *num[n-1]要求不能使用除了output之外的大内存,满足时间复杂度O... 阅读全文
posted @ 2016-01-16 18:32 Breeze0806 阅读(499) 评论(0) 推荐(0) 编辑
摘要: 将链表节点序号(不是值)是偶数的放到链表后面, 如Given1->2->3->4->5->NULL,return1->3->5->2->4->NULL.我首先统计了下链表的大小cnt,同时求出链表尾端end,然后直接将每个链表节点序号是奇数的点后面的节点放到end后面去,同时更新end,这样更新cn... 阅读全文
posted @ 2016-01-16 17:33 Breeze0806 阅读(987) 评论(0) 推荐(0) 编辑
上一页 1 ··· 9 10 11 12 13 14 15 下一页