LeetCode: 继续easy题4

Rotate Array

"""Cyclic Replacements,这个思路还是挺难写对的==
O(n)
"""
class Solution {
public:
    void rotate(vector<int>& nums, int k) {
        int current = 0;
        int next;
        int buffer, buffer_next;
        int n = nums.size();


        int count = 0;

        for(int i=0; count<nums.size(); i++){
            current = i;
            buffer = nums[current];
            do {
                next = (current + k) % n;
                buffer_next = nums[next];
                nums[next] = buffer;

                current = next;
                buffer = buffer_next;
                count ++;
            } 
            while(current != i);
        }
    }
};

Toeplitz Matrix
https://leetcode.com/contest/weekly-contest-68/problems/toeplitz-matrix/

"""
"""
class Solution {
public:
    bool isToeplitzMatrix(vector<vector<int>>& matrix) {
        int cols = matrix[0].size()-1;
        int num = matrix.size()-1;
        int temp;

        for(int i=0; i<=40; i++)
        {
            bool flag = true;
            for(int j=0; j<=i; j++){
                if(num-(i-j)>=0 && j<= cols){
                    int current = matrix[num-(i-j)][j];
                    if(flag){
                        temp = current;
                        flag = false;
                    }
                    if (current == temp)
                        ;
                    else
                        return false;
                }
            }
        }      
        return true;
    }
};

Reverse Bits

"""简单,5 ms。
另外注意一些移位的操作,不过编译器等应该会对乘除法进行优化,位运算未必有优势。
"""
class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        int m = 0;

        for(int i=0; i<32; i++){
            if(n!=0){
                int res = n % 2;
                m = m*2 + res;
                n = n / 2;
            }
            else
                m *= 2;

        }

        return m;
    }
};

Number of 1 Bits

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int res = 0, sum = 0;

        while(n){
            res = n % 2;
            if (res == 1)
                sum += 1;
            n /= 2;
        }
        return sum;
    }
};

House Robber

"""DP, 2ms
"""
class Solution {
public:
    int rob(vector<int>& nums) {
        int end1=0, end2 = 0, end=0;
        for(int i=0; i<nums.size(); i++){
            end = max(end1, end2+nums[i]);
            end2 = end1;
            end1 = end;
        }

        return end;
    }
};

Jewels and Stones

"""hash map
"""
class Solution {
public:
    int numJewelsInStones(string J, string S) {
        unordered_map<char, int> map;
        for (int i=0; i<J.size(); i++)
            map.insert(make_pair(J[i],0));

        int num = 0;
        for(int j=0; j<S.size(); j++){
            if(map.find(S[j])!=map.end())
                num++;
        }
        return num;
    }
};

Global and Local Inversions

"""https://www.geeksforgeeks.org/counting-inversions-using-set-in-c-stl/
"""
class Solution {
public:
    int getCount(vector<int>& arr){
        set<int> set1;
        set1.insert(arr[0]);

        int count = 0; 

        set<int>::iterator itset1; 

        for (int i=1; i<arr.size(); i++)
        {
            set1.insert(arr[i]);
            itset1 = set1.upper_bound(arr[i]);
            count += distance(itset1, set1.end());
        }

        return count;
    }

    bool isIdealPermutation(vector<int>& A) {
        int num = getCount(A);
        int num2 = 0;
        for(int i=0;i<A.size()-1;i++){
            if(A[i]>A[i+1])
                num2 ++;
        }
        return num == num2;
    }

};

Happy Number

"""
"""
class Solution {
public:
    int digitSum(int n){
        int temp = 0;
        while(n){
            temp += pow(n%10,2);
            n /= 10;
        }
        return temp;
    }

    bool isHappy(int n) {
        int slow=n, fast=n;
        while(true){
            slow = digitSum(slow);
            fast = digitSum(fast);
            fast = digitSum(fast);

            if(slow == 1)
                return true;
            if(slow == fast)
                return false;
        }
    }
};

Remove Linked List Elements

"""
"""
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* front = head;
        ListNode* back = head;

        if(head == NULL)
            return head;

        while(front!= NULL && front->val == val)
            front = front->next;
        head = front;
        if(head == NULL)
               return head;

        while(1){   
            while(front->next != NULL && front->next->val != val)
                front = front->next;
            if(front->next == NULL)
                return head;

            back = front->next;
            while(back!=NULL && back->val == val)
                back = back->next;

            front->next = back;

        }
    }
};

Count Primes

"""埃拉托色尼筛选法
time O(n log log n)
space O(n)
"""
class Solution {
public:
    int countPrimes(int n) {
        if(n==1)
            return 0;

        vector<int> nums;
        for(int i=0;i<n;i++)
            nums.push_back(1);

        int temp = 0;
        for(int i=2; i<=floor(sqrt(n));i++){
            temp = i*i;
            if(temp>n)
                break;
            if(nums[temp-1]==1)
                while(temp<=n){
                    nums[temp-1] = 0;
                    temp += i;
                }
        }

        int sum = 0;
        for(int i=1; i<n-1;i++)
            if(nums[i]==1)
                sum++;
        return sum;
    }
};

Isomorphic Strings

"""
"""
class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char,char> map, map2;

        for(int i=0;i<t.size();i++)
        {
            if(map.find(t[i])!=map.end() && map[t[i]]!=s[i])
                return false;

            if(map2.find(s[i])!=map2.end() && map2[s[i]]!=t[i])
                return false;

            map.insert(make_pair(t[i],s[i]));
            map2.insert(make_pair(s[i],t[i]));

        }

        return true;
    }
};

Reverse Linked List

"""
recursively,简单
n
n
"""
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if( head==NULL||head->next == NULL )
            return head;

        ListNode* temp0 = head;
        ListNode* last = head->next;

        head = reverseList(head->next);

        temp0->next = NULL;
        last->next = temp0;

        return head;

    }
};
"""
iteratively
n
1
"""
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL||head->next==NULL)
            return head;

        ListNode* temp_head = head->next;
        ListNode* temp;
        ListNode* head0=head;
        while(temp_head!=NULL){
            temp = temp_head->next;
            temp_head->next = head;


            if(temp==NULL){
                head0->next = NULL;
                return temp_head;
            }

            head = temp_head;
            temp_head = temp;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值