- 博客(16)
- 收藏
- 关注
原创 137. Single Number II
class Solution {public://考虑每个元素的为一个32位的二进制数,这样每一位上出现要么为1 ,要么为0。对数组,统计每一位上1 出现的次数count,必定是3N或者3N//+1 次。让count对3取模,能够获得到那个只出现1次的元素该位是0还是1。 int singleNumber(vector& nums) { int n = nums
2016-06-24 20:14:34
194
原创 318. Maximum Product of Word Lengths
根据题目的描述,我们可知,最耗时的地方在于判断两个字符串中是否存在重复的字符。一提到判断重复字符,脑海中马上浮现哈希大法。又看到题目要保证字符串均为小写字母,也就说总共26个字母,我们需要26位,一个int足矣。我们自定义,如果某字符出现,该bit置1,否则置0.则比较两个字符串是否有重复字符,就是让这两个标志int 进行与操作,结果为0则表明没有重复字符。
2016-06-20 11:13:34
189
原创 343. Integer Break
由均值不等式(n个数的算术平均数大于等于它们的几何平均数): >= 得:当把输入的n拆分成几个相等的数时它们的积最大。那么问题来了,拆分成几个呢?为了方便使用导数,我们先假设我们可以把n拆分成实数。那么设每一个数为x,则一共有n/x个数。设它们的积为f(x),则f(x)=x(n/x),那么怎么求f(x)最大值呢?求导数!f′(x)=(n
2016-06-20 09:26:01
148
原创 268. Missing Number
class Solution {public: int missingNumber(vector& nums) { /* sort(nums.begin(),nums.end()); int left=0,right=nums.size()-1,mid; while(left mid = (left +
2016-06-16 16:48:58
185
原创 238. Product of Array Except Self
class Solution {public: vector productExceptSelf(vector& nums) { int n=nums.size(),right=1,left=1; vector res(n,1); for(int i=0;i res[i]*=right; rig
2016-06-14 11:34:02
200
原创 347. Top K Frequent Elements
class Solution {public: vector topKFrequent(vector& nums, int k) { map mp; map ::iterator i; for (int i=0;i mp[nums[i]]++; //Map中的元素是自动按key升序排序,所以不能对map用so
2016-06-14 11:09:52
179
原创 119. Pascal's Triangle II
class Solution {public: vector getRow(int rowIndex) {/* vector p; if(rowIndex else{ if (rowIndex==0){ p.push_back(1); }
2016-05-23 10:36:32
164
原创 118. Pascal's Triangle(使用二维vector)
class Solution {public: vector> generate(int numRows) { vector p; vector> pp; if(numRows else{ if(numRows==1){ p.push_back(1);
2016-05-21 14:13:20
232
原创 198. House Robber
class Solution {public: int rob(vector& nums) { int n=nums.size(); int maxi[n]; if(n==0) return 0; if(n==1) return nums[0]; maxi[0]=nums[0];
2016-05-20 15:54:14
1170
原创 21. Merge Two Sorted Lists
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: Li
2016-05-20 11:47:18
170
原创 122. Best Time to Buy and Sell Stock II
class Solution {public: int maxProfit(vector& prices) { if(prices.size() int min=prices[0],maxpro=0,max=0; for (int i=1;i if(prices[i-1]>prices[i]){
2016-05-19 17:48:44
157
原创 263. Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc
2016-05-19 15:22:34
180
原创 217. Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element
2016-05-17 10:53:17
231
原创 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element
2016-05-17 09:19:01
137
原创 258. Add Digits
class Solution {public: int addDigits(int num) { do{ int n=num,i; for(i=0;n!=0;i++) n/=10; int a[i]; int m=num;
2016-05-16 15:29:23
165
原创 344. Reverse String (Iterator简介)
Write a function that takes a string as input and returns the string reversed.Example: Given s = “hello”, return “olleh”.class Solution {public: string reverseString(string s) {
2016-05-16 10:03:06
293
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人