Rrui的Leetcode算法刷题笔记(七)

注:本页有一定量的锁定题,由于本人非常穷,所以本页的题较少。

318. Maximum Product of Word Lengths

 

注:简单题,不解释,浪费时间。faster than 100.00%。

 

326. Power of Three

class Solution {
public:
    bool isPowerOfThree(int n) {
        
        if(n==0)
            return false;
        
        while(n!=1)
        {
            if(n%3)
                return false;
            n/=3;
        }
        return true;
    }
};

注:简单题,好题呀,其实可以用对数求出3的幂次,就不用迭代求解了。faster than 20.11%。

 

328. Odd Even Linked List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        
        if(!head||!head->next)
            return head;
        ListNode* p=head;
        ListNode* a=p;
        ListNode* q=new ListNode(0);
        ListNode* b=q;   
        
        while(a->next)
        {
            b->next=a->next;
            b=b->next;
            if(a->next->next)
            {
                 a->next=a->next->next;
                 a=a->next;
            }
            else
            {
                b->next=a->next;
                b=b->next;
                a->next=NULL;
            }
        }
        b->next=NULL;
        a->next=q->next;
        return p; 
    }
};

注:中等题,链表题画图就好做了。faster than 99.29%。

 

334. Increasing Triplet Subsequence

class Solution {
public:
	bool increasingTriplet(vector<int>& nums) {
		if (nums.size() < 3) return false;
		int firstMin = INT_MAX;
		int secondMin = INT_MAX;
		for (int i = 0; i < nums.size(); i++){
			if (nums[i] <= firstMin){//<=
				firstMin = nums[i];
			}
			else if (nums[i] <= secondMin){
				secondMin = nums[i];
			}
			else{
				return true;
			}
		}
		return false;
	}
};

注:中等题,。faster than XX%。

 

 

338. Counting Bits

class Solution {
public:
    vector<int> countBits(int num) {
        
        vector<int> a;
        for(int i=0;i<=num;i++)
        {
            int sum=0,j=i;
            while(j)
            {
                sum+=j%2;
                j/=2;
            }
            a.push_back(sum);
        }
        return a;
    }
};

注:中等题,只会这么做,位运算不是很会。faster than 76.87%。

341. Flatten Nested List Iterator

/**
 * // This is the interface that allows for creating nested lists.
 * // You should not implement it, or speculate about its implementation
 * class NestedInteger {
 *   public:
 *     // Return true if this NestedInteger holds a single integer, rather than a nested list.
 *     bool isInteger() const;
 *
 *     // Return the single integer that this NestedInteger holds, if it holds a single integer
 *     // The result is undefined if this NestedInteger holds a nested list
 *     int getInteger() const;
 *
 *     // Return the nested list that this NestedInteger holds, if it holds a nested list
 *     // The result is undefined if this NestedInteger holds a single integer
 *     const vector<NestedInteger> &getList() const;
 * };
 */
class NestedIterator {
public:
    vector<int> a;
    int i=0,size=0;
    
    NestedIterator(vector<NestedInteger> &nestedList) {
        a=digui(nestedList);
        size=a.size();
    }

    vector<int> digui(vector<NestedInteger> nestedList)
    {
        vector<int> a;
        
        for(int i=0;i<nestedList.size();i++)
        {
            if(nestedList[i].isInteger())
                a.push_back(nestedList[i].getInteger());
            else
            {
                vector<int> b=digui(nestedList[i].getList());
                a.insert(a.end(),b.begin(),b.end());
            }
        }
        return a; 
    }
    
    int next() {
        return a[i++];
    }

    bool hasNext() {
        return i<size;
    }
};

/**
 * Your NestedIterator object will be instantiated and called as such:
 * NestedIterator i(nestedList);
 * while (i.hasNext()) cout << i.next();
 */

注:中等题,类的使用与递归。faster than 97.16%。

342. Power of Four

class Solution {
public:
    bool isPowerOfFour(int num) {
            
        double a=log10(num)/log10(4);
        return a==int(a)?true:false;
    }
};

注:简单题,跟326一样的思路。faster than 100%。

343. Integer Break

 

注:中等题,不解释,浪费时间。faster than 100.00%。

344. Reverse String

class Solution {
public:
    string reverseString(string s) {
        
        int first=0,end=s.size()-1;
        while(end>first)
            swap(s[first++],s[end--]);
        return s;
    }
};

注:简单题,双指针法头尾交换。faster than 97.32%。

345. Reverse Vowels of a String

class Solution {
public:
    string reverseVowels(string s) {
        
        int first=0,end=s.size()-1;
        string b=s;
        
        while(1)
        {                                   
            while(first<s.size()&&b[first]!='a'&&b[first]!='e'&&b[first]!='i'&&b[first]!='o'&&b[first]!='u'&&b[first]!='A'&&b[first]!='E'&&b[first]!='I'&&b[first]!='O'&&b[first]!='U')
                first++;
            while(end>=0&&b[end]!='a'&&b[end]!='e'&&b[end]!='i'&&b[end]!='o'&&b[end]!='u'&&b[end]!='A'&&b[end]!='E'&&b[end]!='I'&&b[end]!='O'&&b[end]!='U')
                end--;
            if(first>end)
                break;
            swap(b[first],b[end]);
            first++;
            end--;
        }
        return b;
        
    }
};

注:简单题,跟上题一样的思路。faster than 98.30%。

347. Top K Frequent Elements

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        
        map<int,int> a;
        vector<int> b,c;
        for(int i:nums)
            a[i]++;
        
        for(auto i=a.begin();i!=a.end();i++)
            b.push_back(i->second);
        
        sort(b.begin(),b.end());
        
        for(auto i=a.begin();i!=a.end();i++)
            if(i->second>=b[a.size()-k])
                c.push_back(i->first);        
        return c;  
    }
};

注:中等题,题目要求的时间复杂度就是要快排,所以利用map和sort来做。faster than 45.08%。

349. Intersection of Two Arrays

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        
        set<int> a,b;
        vector<int> c;
        for(int i:nums1)
            a.insert(i);
        for(int i:nums2)
            if(a.find(i)!=a.end())
                b.insert(i); 
        for(auto i=b.begin();i!=b.end();i++)
            c.push_back(*i);
        return c;
    }
};

注:简单题,用集合就好,保证没有重复元素。faster than 76.13%。

350. Intersection of Two Arrays II

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        
        map<int,int> a;
        vector<int> c;
        for(int i:nums1)
            a[i]++;
        for(int i:nums2)
            if(a.find(i)!=a.end()&&a[i]>0)
            {
                c.push_back(i);
                a[i]--;
            }
        return c;
    }
};

注:简单题,跟上一题一样,只不过根据题意换成了map。faster than 85.75%。

357. Count Numbers with Unique Digits

 

注:简单题,跟上一题一样,只不过根据题意换成了map。faster than 85.75%。

363. Max Sum of Rectangle No Larger Than K

 

注:困难题,不解释,浪费时间。faster than 100.00%。

365. Water and Jug Problem

 

注:中等题,不解释,浪费时间。faster than 100.00%。

367. Valid Perfect Square

class Solution {
public:
    bool isPerfectSquare(int num) {
        
        long long low=0,high=num;
        while(low<=high)
        {
            long long mid=(low+high)/2;
            if(mid*mid>num)
                high=mid-1;
            else if(mid*mid<num)
                low=mid+1;
            else return true;
        }
        return false;
    }
};

注:简单题,二分法解决,mid要设置成long long格式。faster than 100.00%。

 

Rrui的Leetcode算法刷题笔记(八)链接如下:

https://blog.csdn.net/Rrui7739/article/details/84111246

 

337. 打家劫舍 III

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int rob(TreeNode* root) {
        if(root == NULL) return 0;
        int left = 0, right = 0, leftSon = 0, rightSon = 0;
        if(root->left){
            left = rob(root->left);
            if(root->left->left)
                leftSon = rob(root->left->left);
            if(root->left->right)
                leftSon += rob(root->left->right);
        }
        if(root->right){
            right = rob(root->right);
            if(root->right->left)
                rightSon = rob(root->right->left);
            if(root->right->right)
                rightSon += rob(root->right->right);
        }
        return max(root->val + leftSon + rightSon, left + right);
    }
};

 

 

 

 

 

343. 整数拆分

class Solution {
public:
    int integerBreak(int n) {
        
        vector<int> a(n+1,-1);
        return interger(n,a);
    }
    
    int interger(int n,vector<int>& a)
    {
        if(a[n]!=-1)
            return a[n];
        
        if(n==1||n==0)
            return n;            

        for(int i=1;i<=n;i++)
            a[n]=max(max(i*interger(n-i,a),a[n]),i*(n-i));
        
        return a[n];
    }
};

 

 

347. 前K个高频元素(另可用最大堆、优先级队列)

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        
        map<int,int> a;
        vector<int> b;
        
        for(int i=0;i<nums.size();i++)
            if(a.find(nums[i])==a.end())
                a.insert(pair<int,int>(nums[i],1));
            else a.find(nums[i])->second++;
        
        for(int j=0;j<k;j++)
        {
            int max=INT_MIN,p;
            for(map<int,int>::iterator i=a.begin();i!=a.end();i++)
                if(i->second>max)
                {
                    p=i->first;
                    max=i->second;
                }

            a.erase(a.find(p));
            b.push_back(p);
        }
        return b;
    }
};

 

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值