Happy Number

class Solution {
public:
    int happy(int num){
        int a = 0;
        while (num!=0) {
            a += int(pow(num%10, 2));
            num/=10;
        }
        return a;
    }


    bool isHappy(int n) {
        int slow, fast;
        slow = fast = n;
        do {
            slow = happy(slow);
            fast = happy(fast);
            fast = happy(fast);
        } while(slow != fast);

        return slow==1?1:0;
    }
};
2017/12/8 posted in  leetcode

Find the Difference

class Solution {
public:
    char findTheDifference(string s, string t) {
        char sum = 0;
        for (char c: s) {
            sum^=c;
        }
        for (char c: t) {
            sum^=c;
        }
        return sum;
    }
};
2017/12/8 posted in  leetcode

Distribute Candies

class Solution {
public:
    int distributeCandies(vector<int>& candies) {
        int res = 0;
        unordered_map<int, vector<int>> map;
        for (int i =0; i<candies.size(); i++) {
            map[candies[i]].push_back(candies[i]);
        }
        if (map.size()<=candies.size()/2) {
            return map.size();
        }else{
            return candies.size()/2;
        }
    }
};
2017/12/8 posted in  leetcode

Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> res;
        for (int i=1; i<=n; i++) {
            if (i%3==0&&i%5==0) {
                res.push_back("FizzBuzz");
            }else if (i%5==0){
                res.push_back("Buzz");
            }else if(i%3==0){
                res.push_back("Fizz");
            }else{
                res.push_back(to_string(i));
            }
        }
        return res;
    }
};
2017/12/8 posted in  leetcode

Sum of Two Integers

class Solution {
public:
    int getSum(int a, int b) {
        int sum = a;
        while (b!=0) {
            sum = a^b;
            b = (a&b)<<1;
            a = sum;
        }
        return a;
    }
};
2017/12/8 posted in  leetcode

Employee Importance

/*
// Employee info
class Employee {
public:
    // It's the unique ID of each node.
    // unique id of this employee
    int id;
    // the importance value of this employee
    int importance;
    // the id of direct subordinates
    vector<int> subordinates;
};
*/
class Solution {
public:
    int getImportance(vector<Employee*> employees, int id) {
        unordered_map<int ,Employee*> map;
        for(auto emp:employees){
            map[emp->id] = emp;
        }
        return help(map,id);
    }
    
    int help(unordered_map<int, Employee*>& map, int id){
        int sum = map[id]->importance;
        for(auto element : map[id]->subordinates){
            sum += help(map, element);
        }
        return sum;
    }
};
2017/12/8 posted in  leetcode