第二周leetcode
4/4 这周莫名得忙,一天是做编译,一天是做模式识别作业,(一天刷魔兽皮肤),周末玩了两天,总的来说还是松懈了,大概只做了两天的leetcode,刷了10道题,羞愧羞愧。
决定每次把代码附上在这个总结里,一来是方便,二来是有利于以后回顾。(其实想找一个更好的排列方法,以后再说吧)
283. Move Zeroes
将数组中的0移到数组末尾,是正常的for循环,不过in-place要考虑一下了,总之我看了答案
依次调换,同时记录非0的个数
(万恶的校园网,贴不上代码)
237. Delete Node in a Linked List
删除链表节点,答案很奇怪,也想知道为啥,这种解答我可以理解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: void deleteNode(ListNode* node) { node->val = node->next->val; node->next = node->next->next; } };
但这种解答我就不知为什么
void deleteNode(ListNode* node) { *node = *node->next; }
换成这样就运行失败
void deleteNode(ListNode* node) { node = node->next; }
算是个不懂的地方
260. Single Number III
返回数组中出现是single number,因为是多个numbers,所以用vector加上push_back()返回,很方便
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: vector<int> singleNumber(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<int> single; int i = 0; if(nums[i]!=nums[i+1]) { single.push_back(nums[i]); } for(i=1;i<nums.size()-1;i++) { if(nums[i]!=nums[i-1]&&nums[i]!=nums[i+1]) { single.push_back(nums[i]); } } if(nums[i]!=nums[i-1]) { single.push_back(nums[i]); } return single; } };
100. Same Tree
判断两个树是否完全相同,看上去很简单,但是返回值是时候难到我了,如何在子树(即递归多次以后)发现不同的时候,将结果正确地返回到第一层递归里,而且不用全局,看了答案才发现自己太蠢了,简单的尾递归。算是有了解了吧。
238. Product of Array Except Self
数组对应的值和其他值相乘,算上有一个0的边界条件和有2个以上0的边界条件即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: vector<int> productExceptSelf(vector<int>& nums) { int ele = 1; int count0 = 0; for(int i=0;i<nums.size();i++){ if(nums[i]==0) count0++; else ele = ele*nums[i]; } if(count0>1) { for(int i=0;i<nums.size();i++) { nums[i] = 0; } } else if(count0==1) { for(int i=0;i<nums.size();i++) { if(nums[i]== 0) nums[i] = ele; else nums[i] = 0; } } else { for(int i=0;i<nums.size();i++) { if(nums[i]== 0) nums[i] = ele; else nums[i] = ele/nums[i]; } } return nums; } };
242. Valid Anagram
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
这我想得很复杂,看了答案真是羞愧,应该多用用int数组了,创建一个长度26的数组。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: bool isAnagram(string s, string t) { if(s.size()!=t.size()) { return false; } int n = s.size(); int count[26] = {0}; for(int i = 0 ;i < n;i++) { count[s[i] - 'a']++; count[t[i] - 'a']--; } for(int j = 0;j < 26;j++) { if(count[j]) return false; } return true; } };
168. Excel Sheet Column Title
1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
简单的余数和除数的题,回顾了一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: string convertToTitle(int n) { int x,y; string s = ""; while(n) { x = (n-1)/26; y = (n-1)%26; if(y) { string s1 = ""; s1 += y+'A' ; s = s1 + s; } else { s = 'A' + s; } n = x; } return s; } };
171. Excel Sheet Column Number
和上一题相反的题,根据string返回int,已知string长度(.size())那就简单很多了
(gtmd校园网,gtmd方校长,网页又崩了)
217. Contains Duplicate
判断数组中是否有相同数,先sort再for,水题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: bool containsDuplicate(vector<int>& nums) { if(nums.size()<2) return false; sort(nums.begin(),nums.end()); for(int i = 0;i < nums.size()-1;i++) { if(nums[i]==nums[i+1]) return true; } return false; } };
319. Bulb Switcher
灯泡问题,我先写了一个长的代码,结果999999超时
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
vector<int> nums(n,1); if(n==0) return 0; if(n==1) return 1; for(int i = 1;i <= n;i++ ) { if(i==2) { for(int j=1;j<n;j=j+2) { nums[j] = 0; } } else if(i<n) { for(int j=i-1;j<n;j=j+i) { nums[j] = nums[j]^1; } } else if(i==n) { nums[n-1] = nums[n-1]^1; } } int counts = 0; for(int j=0;j<n;j++) { if(nums[j]) counts++; } return counts;
结果答案是直接返回sqrt。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int bulbSwitch(int n) { /* vector<int> nums(n,1); if(n==0) return 0; if(n==1) return 1; for(int i = 1;i <= n;i++ ) { if(i==2) { for(int j=1;j<n;j=j+2) { nums[j] = 0; } } else if(i<n) { for(int j=i-1;j<n;j=j+i) { nums[j] = nums[j]^1; } } else if(i==n) { nums[n-1] = nums[n-1]^1; } } int counts = 0; for(int j=0;j<n;j++) { if(nums[j]) counts++; } return counts; */ return sqrt(n); } };
169. Majority Element
返回一个占数组超过一半的数(一定存在)
先sort,然后直接取中间数,机智如我
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Solution { public: int majorityElement(vector<int>& nums) { sort(nums.begin(),nums.end()); return nums[nums.size()/2]; } };