昨天有点事情,没有写博客,不过还是刷了题的,所以今天的博客是昨天和昨天两天的总和。
121. 买卖股票的最佳时机
class Solution {
public:
int maxProfit(vector<int>& prices) {
int inf = 1000000;
int minprice = inf, maxprofit = 0;
for (int price: prices) {
maxprofit = max(maxprofit, price - minprice);
minprice = min(price, minprice);
}
return maxprofit;
}
这道题第一次写用了贪心算法,可是贪心算法在最后一组超级多的测试用例中爆炸了,所以只能用了动态规划。
122. 买卖股票的最佳时机 II
class Solution {
public:
int maxProfit(vector<int>& prices) {
int i,j,max=0;
for(i=1;i<prices.size();i++)
{
for(j=i-1;j<i;j++)
{
if(prices[i]>prices[j])
{
max+=(prices[i]-prices[j]);
}
}
}
return max;
}
};
这个题没啥别的思想,干就完事了。
125. 验证回文串
class Solution {
public:
bool isPalindrome(string s) {
string tmp;
for(auto c : s)
{
if(islower(c)||isdigit(c))
tmp+=c;
if(isupper(c))
tmp+=(c+32);
}
int i=0,j=tmp.size()-1;
while(i<j)
{
if(tmp[i]!=tmp[j])
return false;
i++;j--;
}
return true;
}
};
这道题其实可以用栈的思想去做,就像前几天做的有效括号,但是最后还是选择了回文数的做法,因为栈有点费事,这道题虽然简单但还是有几个函数需要强调一下。
islower(char c) 是否为小写字母
isupper(char c) 是否为大写字母
isdigit(char c) 是否为数字
isalpha(char c) 是否为字母
isalnum(char c) 是否为字母或者数字
toupper(char c) 字母小转大
tolower(char c) 字母大转小
136. 只出现一次的数字
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res=0,i;
for(i=0;i<nums.size();i++)
{
res^=nums[i];
}
return res;
}
};
这道题!!!,用了异或^异或这玩意只在离散数学和c语言那本书里看过,没想到在这用到了,学到了学到了,不过强调的是异或是对计算机中二进制异或。
141. 环形链表
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL||head->next==NULL)
{
return false;
}
ListNode *fast=head->next;
ListNode *slow=head;
while(slow!=fast)
{
if(fast->next==NULL||fast->next->next==NULL)
return false;
fast=fast->next->next;
slow=slow->next;
}
return true;
}
};
有缘分总归会遇到的,没有缘分踏遍天涯海角也看不到。哈哈哈哈哈哈有感而发一下后面还有一个类似的题。
160. 相交链表
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *p;
ListNode *q;
p=headA,q=headB;
while(p!=q)
{
if(p!=NULL)
{
p=p->next;
}
else
{
p=headB;
}
if(q!=NULL)
{
q=q->next;
}
else
{
q=headA;
}
}
return p;
}
};
如果为空直接将另外一个指针赋予道另外一个的链表头,所谓有缘分总归会遇到的,没有缘分踏遍天涯海角也看不到。哈哈哈哈。
167. 两数之和 II - 输入有序数组
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int i=0,j=numbers.size()-1;
vector<int>a;
while(i<j)
{
if(numbers[i]+numbers[j]<target)i++;
else if(numbers[i]+numbers[j]>target)j--;
else if(numbers[i]+numbers[j]==target)
{
a.push_back(i+1);
a.push_back(j+1);
break;
}
}
return a;
}
};
这道题不能像第一道题用暴力法了,暴力发会像上面一样挂在最后一个测试用例上面,看来leecode也看透像我这种废物了,不过稍微想一下就想到了类似双指针的想法(为什么可以i=0,j指向末尾,因为他是升序的数组,如果小于说明i小了,如果大了说明j大了,很神奇是不是)
168. Excel表列名称
class Solution {
public:
string convertToTitle(int n) {
string ss;
while(n>0)
{
ss=(char)(--n%26+'A')+ss;
n=n/26;
}
return ss;
}
};
这道题作为去年参加过蓝桥杯省赛的人来说,一看就明白了,就是转换26进制,不过看很多人没明白题意,看来还需要锻炼啊
169. 多数元素
class Solution {
public:
int majorityElement(vector<int>& nums) {
if(nums.size()==1||nums.size()==2)
return nums[0];
int left=0,right=nums.size();
int mid=left+(right-left)/2;
sort(nums.begin(),nums.end());
if(nums[mid]==nums[mid+1])
return nums[mid];
else
return nums[mid-1];
}
};
这道题想了一下他说大于n/2,有没有一丝熟悉,我当初想到了二分法,这道题有一点二分的思想,不过只运行一次。
171. Excel表列序号
class Solution {
public:
int titleToNumber(string s) {
int n = s.size();
int res = 0;
for(int i = 0; i < n; i++) {
res = res * 26 + (s[i] - 'A' + 1); // 加() 防止214783621先+27
}
return res;
}
};
这道题就是上上道题的翻版,这个就是赤裸裸的转换26进制。
总结
这两天发下自己对于刷题找到一些感觉了,可喜可贺,不过万事贵在坚持,最后一句话,奥里给干了兄弟们!