您的位置:首页 > 编程语言 > C语言/C++

LeetCode的easy题集合(C++实现)一

2015-04-28 09:14 567 查看
1.Happy Number

A happy
number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which
does not include 1. Those numbers for which this process ends in 1 are happy numbers.

如:


该题的关键是每一论计算过后不等于1的值存入set容器,如果该值已存在set中,则会出现循环,无法等于1.

bool
isHappy(int n)

{
int sum=n;

set<int> reg;

while(sum!=1)

{

n = sum;

sum = 0;

while(n!=0)

{

sum=sum+n%10*(n%10);

n/=10;

}

if(reg.find(sum)==reg.end())

reg.insert(sum);

else return false;

}

return true;

}

2.


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 stopping you from robbing each of them is that adjacent houses have security system connected and it
will automatically contact the police if two adjacent houses were broken into on the same night.Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without
alerting the police.
这是一个简单的动态规划问题,用d[i]表示抢到第i家的最大收益,d[i]=max(d[i-1],pre+num[i]),d[i-1]表示不抢地第i家,pre表示抢第i家之前的总收益,
num[i]表示抢第i家的收益。

int
rob(vector<int>& nums) {

int pre=0,len=nums.size();

if(len==0)

return 0;

if(len==1)

return nums[0];

vector<int> d(len,0);

d[0]=nums[0];

for(int i=1;i<len;i++)

{

d[i]=max(d[i-1],(pre+nums[i]));

pre=d[i-1];

}

return d[len-1];

}


3.Number of 1 Bits

Write
a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming
weight).For
example, the 32-bit integer ’11' has binary representation
00000000000000000000000000001011
,
so the function should return 3.

x-1使得以二进制表示的x,从低向高位开始直到遇见第一个1在内的值,都由0变成1,由1变成0。如11-01
= 10, 10 – 01 = 01, 01 – 01 = 00, 100 – 001 = 011。这样每实现一次n&(n-1),就会减少一个1.

int
hammingWeight(uint32_t n) {

int count=0;

while(n!=0)

{

n=n&(n-1);

count++;

}

return count;

}

4.Reverse
Bits

Reverse
bits of a given 32 bits unsigned integer.

uint32_t
reverseBits(uint32_t n) {

vector<int> mid(32,0);

uint32_t res=0;

for(int i=0;i<32;i++)

{

mid[i]=n%2;

n/=2;

if(n==0)

break;

}

for(int j=0;j<32;j++)

{

res=res*2+mid[j];

}

return res;

}

5.Rotate
Array

Rotate
an array of n elements
to the right by k steps.

void
reserve(int nums[],int begin,int end)

{

while(begin<end)

{

int temp=nums[begin];

nums[begin]=nums[end];

nums[end]=temp;

begin++;

end--;

}

}

void rotate(int nums[], int n, int k) {

k%=n;

if(n<=1) return;

reserve(nums,0,n-1);

reserve(nums,0,k-1);

reserve(nums,k,n-1);

}

6.Factorial
Trailing Zeroes

Given
an integer n,
return the number of trailing zeroes in n!.
要求: 时间复杂度为logn.

分析:要得到0,得通过2乘以5,明显2的倍数的个数比5的倍数的个数多,所以只要求得5的倍数的个数。

int trailingZeroes(int n) {

int count=0;

while(n)

{

count+=n/5;

n/=5;

}

return count;

}


7.Excel Sheet Column Number

Given
a column title as appear in an Excel sheet, return its corresponding column number.如A--1,B--2......Z--26,AA--27,....AZ--52.

int
titleToNumber(string s) {

int len=s.length();

int sum=0;

for(int i=0;i<len;i++)

{

sum=26*sum+s[i]-'A'+1;

}

return sum;

}


8.Excel Sheet Column Title

Given
a positive integer, return its corresponding column title as appear in an Excel sheet.

string
convertToTitle(int n) {

vector<char> result;

int i = 0;

if (n <= 0)

return NULL;

while (n)

{

if (n % 26 == 0)

{

result.push_back('Z');

n = (n / 26 - 1);

}

else

{

result.push_back(n % 26 - 1 + 'A');

n = n / 26;

}

}

int len = result.size();

string res;

for (int j = 0; j<len; j++)

{

res+= result[len - 1 - j];

}

return res;

}

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

int
majorityElement(vector<int> &num) {

sort(num.begin(),num.end());

int length=num.size()/2;

return num[length];

}


10.Compare Version Numbers

Compare two version numbers version1 and version2.

If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the
.
character.

The
.
character does not represent a decimal point and is used to separate
number sequences.

For instance,
2.5
is not "two and a half" or "half way to version three",
it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37

int compareVersion(string version1, string version2) {

int len1=version1.length();

int len2=version2.length();

int i=0,j=0;

while(i<len1&&j<len2)

{

int sum1=0,sum2=0;

while(i<len1&&version1[i]!='.')

{

sum1=sum1*10+version1[i]-'0';

i++;

}

while(j<len2&&version2[j]!='.')

{

sum2=sum2*10+version2[j]-'0';

j++;

}

if(sum1<sum2)

return -1;

else if(sum1>sum2)

return 1;

else

{

i++;

j++;

}

}

int sum1=0,sum2=0;

while(i<len1&&version1[i]!='.')

{

sum1=sum1*10+version1[i]-'0';

i++;

}

if(sum1>0)

return 1;

while(j<len2&&version2[j]!='.')

{

sum2=sum2*10+version2[j]-'0';

j++;

}

if(sum2>0)

return -1;

else

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: