Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
**Example: **19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
最直观的解法可以使用hash表.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: boolisHappy(int n){ unordered_set<int> s; s.insert(n); int t = n; while(t != 1){ longlong sum = 0; while(t > 0){ sum += ((t % 10) * (t % 10)); t /= 10; } t = sum; if(s.count(t)) returnfalse; else s.insert(t); } returntrue; } };
Consider the string s to be the infinite wraparound string of “abcdefghijklmnopqrstuvwxyz”, so s will look like this: “…zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd…”.
Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.
Note:p consists of only lowercase English letters and the size of p might be over 10000.
Example 1:
1 2 3 4 5
Input: "a" Output: 1
Explanation: Only the substring "a" of string "a" is in the string s.
Example 2:
1 2 3 4
Input: "cac" Output: 2 Explanation: There are two substrings "a", "c" of string "cac" in the string s.
Example 3:
1 2 3
Input: "zab" Output: 6 Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.
classSolution { public: intrangeBitwiseAnd(int m, int n){ int minus = n - m + 1; int re = 0; for(int i = 0; i < 31; i++){ int t = 1 << i; if(minus > t){ re = re & (~t); } else{ if((t & m) & (t & n)){ re = re | t; } else{ re = re & (~t); } } } return re; } };
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.
Example:
1 2 3 4 5 6 7
[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]
Answer: 16 Explanation: The perimeter is the 16 yellow stripes in the image below:
Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
You may assume the array’s length is at most 10,000.
Example:
1 2 3 4 5 6 7 8 9 10
Input: [1,2,3]
Output: 2
Explanation: Only two moves are needed (remember each move increments or decrements one element):
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
翻转一个32位整数的每一个位.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: uint32_treverseBits(uint32_t n){ for(int i = 0; i < 16; i++){ swapIntBit(n, i); } return n; }
voidswapIntBit(uint32_t &n, int i){ uint32_t b1 = n & (1 << i); uint32_t b2 = n & (((unsignedint)0x80000000) >> i); n &= ~(1 << i); n &= ~(((unsignedint)0x80000000) >> i); n |= b1 << (31 - i * 2); n |= b2 >> (31 - i * 2); } };