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

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        for (int i =0; i<nums.size(); i++) {
            if (nums[i]!=nums[i+1]) {
                return nums[i];
            }else{
                i++;
            }
        }
        return -1;
    }
};
2017/12/8 posted in  leetcode

Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

列举1~n情况,每4个一循环.

class Solution {
public:
    bool canWinNim(int n) {
        return n%4!=0;
    }
};
2017/12/7 posted in  leetcode

Next Greater Element I

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
        vector<int> res;
        for(auto num1:findNums){
            for (int i = 0; i<nums.size(); i++) {
                int log = 0;
                if (nums[i]==num1) {
                    for (int j = i; j<nums.size(); j++) {
                        if (nums[j]>num1) {
                            res.push_back(nums[j]);
                            log = 1;
                            break;
                        }
                    }
                    if (log == 0) res.push_back(-1);
                    break;
                }
            }
        }
        return res;
    }
};
2017/12/7 posted in  leetcode

Island Perimeter

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int squNum = 0 ,log = 0;
        for (int i = 0; i<grid.size(); i++) {
            for (int j = 0; j<grid[0].size(); j++) {
                if (grid[i][j]==1) {
                    squNum++;
                    if (i!=0&&grid[i-1][j]==1) {
                        log+=2;
                    }
                    if (j!=grid[0].size()-1&&grid[i][j+1]==1) {
                        log+=2;
                    }
                }
            }
        }
        return squNum*4-log;
    }
};
2017/12/7 posted in  leetcode