LeetCode Week 0
来源:互联网 发布:网店优化 编辑:程序博客网 时间:2024/05/20 23:02
LeetCode Week 0
warm up, 希望可以坚持下来
问题集合
1. Hamming Distance (Easy 461)
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance. Note:
Example:
Input: x = 1, y = 4Output: 2Explanation:1 (0 0 0 1)4 (0 1 0 0) ↑ ↑The above arrows point to positions where the corresponding bits are different.
Solution:
int hammingDistance(int x, int y) { int diff = x^y; int distance = 0; while (diff) { distance += diff & 1; diff >>= 1; } return distance;}
2.Number Complement (Easy 476)
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note:
1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
2. You could assume no leading zero bit in the integer’s binary representation. Example 1:
Input: 5Output: 2Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Example 2:
Input: 1Output: 0Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
Solution:
int findComplement(int num) { int helper = -1; int complement = num ^ helper; int mask = 0; while(num > 0) { mask = (mask << 1) + 1; num >>= 1; } return complement & mask;}
3.Keyboard Row (Easy 500)
Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.
Note:
1. You may use one character in the keyboard more than once.
2. You may assume the input string will only contain letters of alphabet.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]Output: ["Alaska", "Dad"]
Solution:
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */char** findWords(char** words, int wordsSize, int* returnSize) { *returnSize = 0; char** result = NULL; if (wordsSize <= 0) return result; int rowMap[26] = {1, 2, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 0, 2}; int* saveFlags = (int *)malloc(sizeof(int) * wordsSize); int cellSize = 0; for (int i = 0; i < wordsSize; i++) { int lastRow = -1; int save = 1; int len = strlen(words[i]); for (int j = 0; j < len; j++) { int row = rowMap[(words[i][j] < 'a' ? (words[i][j] + 32) : words[i][j]) - 'a']; if (lastRow != -1 && row != lastRow) { save = 0; break; } lastRow = row; } saveFlags[i] = save; if (save) { (*returnSize)++; if (cellSize < len) { cellSize = len; } } } if ((*returnSize) > 0) { // alloc memory result = (char **)malloc(sizeof(char*) * (*returnSize)); for (int i = 0; i < (*returnSize); i++) { result[i] = (char *)malloc(sizeof(char) * (cellSize + 1)); } // copy words int index = 0; for (int i = 0; i < wordsSize; i++) { if (saveFlags[i]) { strcpy(result[index++], words[i]); } } } return result;
- LeetCode Week 0
- LeetCode Week 1
- LeetCode Week 6: IPO
- LeetCode Week 2
- leetcode 300(week 7)
- LeetCode Week 3
- LeetCode Week 4
- leetcode week 10
- leetcode 576(week 12)
- leetcode 8(week 13)
- leetcode 108(week 14)
- [Leetcode] Week One ------ Array
- leetcode Plan For This Week
- [week 18][Leetcode] Clone Graph
- [week 19][Leetcode]Jump Game
- [leetcode Maximum Subarray]week 19
- [Leetcode] Week Two & Three ------ Array
- LeetCode week 3 : 24 Game
- window和linux dir locate命令
- 计161_Problem T: 字母变成其下一字母
- Web入门——HTML代码编写规范
- eclipse ResourcePathLocationType Unable to load annotation processor factory 异常
- Python开发技术详解(原书+视频+源码)
- LeetCode Week 0
- nginx配置使用
- 数学算法:求多个数的最小公倍数
- 基于redis分布式锁实现“秒杀”
- dba_source存储的内容解释
- HDU1874-畅通工程续(Dijkstra)
- 如何让ListCtrl的CheckBox只有一个处于选中状态
- Idea 配置 maven web 工程理解
- 暗黑的字符串-动态规划