168 Excel Sheet Column Title
class Solution {
public:
string convertToTitle(int n) {
int tmp=n;
string ans;
ans.clear();
char chara[30];
for(int i=1;i<=25;i++)
chara[i]='A'+i-1;
chara[0]='Z';
if (n<=26)
{
ans.push_back(chara[n % 26]);
return ans;
}
while (tmp!=0)
{
int res = tmp % 26;
tmp = tmp / 26;
if (res==0) tmp--;
ans.push_back(chara[res]);
}
int len=ans.size()-1;
string finalans;
finalans.clear();
for(int i=len;i>=0;i--)
finalans.push_back(ans[i]);
return finalans;
}
};
171 Excel Sheet Column Number
class Solution {
public:
int titleToNumber(string s) {
int len=s.size()-1;
int sum=0,count=1;
for(int i=len;i>=0;i--)
{
sum+=(s[i]-'A'+1)*count;
count*=26;
}
return sum;
}
};
172 Factorial Trailing Zeroes
class Solution {
public:
int trailingZeroes(int n) {
int sum=0;
while (n/5>0)
{
n/=5;
sum+=n;
}
return sum;
}
};
189 Rotate Array
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int len = nums.size();
if (k==0) return;
int rotatek;
rotatek=k%len;
if (rotatek==0) return ;
vector<int> tmp;
tmp.clear();
for(int i=len-rotatek;i<len;i++)
tmp.push_back(nums[i]);
for(int i=0;i<len-rotatek;i++)
tmp.push_back(nums[i]);
for(int i=0;i<len;i++)
nums[i]=tmp[i];
}
};
202 Happy Number
class Solution {
public:
bool isHappy(int n) {
set<int> appear;
appear.clear();
int num=n;
for(;;)
{
//cout<<num<<endl;
if (num==1) return true;
int sum=0;
appear.insert(num);
while (num!=0)
{
int tmp=num%10;
sum+=tmp*tmp;
num/=10;
}
if (appear.find(sum)!=appear.end())
return false;
else
{
appear.insert(sum);
num=sum;
}
}
}
};
203 Remove Linked List Elements
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode tmp(0);
ListNode* p=&tmp;
ListNode* q;
p->next=head;
head=p;
q=head;
p=p->next;
while(p!=NULL)
{
if (p->val==val)
{
q->next=p->next;
p=q->next;
}
else
{
q=p;
p=p->next;
}
}
return head->next;
}
};
119 Pascal's Triangle II
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> tr[2];
int flag=0;
int i=0;
tr[flag].push_back(1);
for(i=1;i<=rowIndex;i++)
{
int tmpflag=1-flag;
tr[tmpflag].clear();
tr[tmpflag].push_back(1);
for(vector<int>::iterator j=tr[flag].begin();j!=tr[flag].end()-1;j++)
{
tr[tmpflag].push_back((*j)+(*(j+1)));
}
tr[tmpflag].push_back(1);
flag=tmpflag;
}
return tr[flag];
}
};
LeetCode 2015.7.8 128,171,172,189,202,203,119
最新推荐文章于 2025-03-24 00:18:04 发布
