**401. Binary Watch
思路:产生两个list分别代表小时和分钟,然后遍历

public List<String> readBinaryWatch(int num) {
List<String> res = new ArrayList<String>();
int[] hour = {8,4,2,1};
int[] minute = {32,16,8,4,2,1}; for(int i = 0; i <= num; i++){
//i表示时和分各有几个灯亮,产生所有可能的值
List<Integer> list1 = generateDigit(hour,i);
List<Integer> list2 = generateDigit(minute,num - i); for(Integer num1 : list1){
if(num1 >= 12) continue;
for(Integer num2 : list2){
if(num2 >= 60) continue;
res.add(num1 + ":" + (num2 < 10 ? "0" + num2 : num2));
}
}
}
return res;
} public List<Integer> generateDigit(int[] nums,int count){
List<Integer> res = new ArrayList<Integer>();
getResult(res,nums,count,0,0);
return res;
} public void getResult(List<Integer> res,int[] nums,int count,int start,int sum){
if(count == 0){
res.add(sum);
return;
} for(int i = start; i < nums.length; i++){
getResult(res,nums,count - 1,i + 1,sum + nums[i]);
}
}
**393. UTF-8 Validation
思路:判断每个UTF-8首Byte,看后面应该接几个Byte,再依次判断后面Byte的合法性,循环

public boolean validUtf8(int[] data) {
if(data == null || data.length == 0) return false;
for(int i = 0; i < data.length; i++){
int numOfBytes = 0;
if(data[i] > 255) return false;
else if((data[i] & 128) == 0) {//0xxxxxxx
numOfBytes = 1;
}
else if((data[i] & 224) == 192){//110xxxxx
numOfBytes = 2;
}
else if((data[i] & 240) == 224){//1110xxxx
numOfBytes = 3;
}
else if((data[i] & 248) == 240){//11110xxx
numOfBytes = 4;
}
else return false; for(int j = 1; j < numOfBytes; j++){
if(i + j >= data.length) return false;
if((data[i + j] & 192) != 128) return false;
}
//-1是因为上面的循环要+1
i = i + numOfBytes - 1;
}
return true;
}
**211. Add and Search Word - Data structure design
思路:利用TrieTree,回溯
class TrieNode{
TrieNode[] children = new TrieNode[26];
String val = "";
} public class WordDictionary {
TrieNode root = new TrieNode(); // Adds a word into the data structure.
public void addWord(String word) {
TrieNode head = root;
for(int i = 0; i < word.length(); i++){
char c = word.charAt(i);
if(head.children[c - 'a'] == null){
head.children[c - 'a'] = new TrieNode();
}
head = head.children[c - 'a'];
}
head.val = word;
} // Returns if the word is in the data structure. A word could
// contain the dot character '.' to represent any one letter.
public boolean search(String word) {
return match(word,0,root);
} public boolean match(String word,int k,TrieNode root){
if(k == word.length()) return !root.val.equals(""); if(word.charAt(k) != '.'){
int idx = word.charAt(k) - 'a';
return root.children[idx] != null && match(word,k + 1,root.children[idx]);
}
else{
for(int i = 0; i < 26; i++){
if(root.children[i] != null && match(word,k + 1,root.children[i])) return true;
}
return false;
}
}
}
总结
191. Number of 1 Bits:两种方法:1、每次和1作逻辑与,右移统计个数;2、n = n & (n - 1)每次消除一个1
342. Power of Four:两种方法:1、循环判断能不能被4整除,更新直到为1;2、大于0且是2的幂乘且排除是2的幂乘不是4的幂乘的数((num & 0x55555555) != 0)后者不需要循环求解
231. Power of Two:同上,1、循环判断能不能被2整除,更新直到为1;2、return n > 0 && (n & (n - 1)) == 0;或统计1的个数只有1个
371. Sum of Two Integers:return b == 0 ? a : getSum(a ^ b,(a & b) << 1);异或相当于不进位的加法,后面的左移相当于加上进位
461. Hamming Distance:依次比较每一位,不相同则加1
201. Bitwise AND of Numbers Range:m、n同时右移直到相等,然后当前m左移同样的位数
284. Peeking Iterator:利用优先队列,将元素添加进去以后直接求解
训练
**190. Reverse Bits:结果先左移,n再右移
**405. Convert a Number to Hexadecimal:类比二进制,每4位是一个字符,循环拼接后翻转
**338. Counting Bits:动态规划:res[i]表示数字i有几个1bit,res[i] = res[i >>> 1] + (i & 1)
**477. Total Hamming Distance:统计每一位上有几个1,几个0,乘积即为这一位上的距离
397. Integer Replacement:两种方法:1、若是偶数则除2,是奇数则取n - 1和n + 1中bit1少的那个,若是3只能取2;2、递归求解,注意考虑负数和越界。前者效率高
318. Maximum Product of Word Lengths:两种方法:1、利用抽屉法判断两个字符串是否有相同字符,依次更新最大值;2、构造数组存每个字符串对应的整型值,判断整型值作与运算是否为0,更新最大值,后者效率高很多
208. Implement Trie (Prefix Tree):利用字典树,下次训练尝试将类成员变成string
提示
1、统计bits中1的个数:n = n & (n - 1)
2、==的优先级高于逻辑运算
3、当出现一组数思考能不能通过全局观察得到解决方法
4、利用bit manipulation将字符串转化成整型判断有没有相同的字符:val[i] |= (1 << (words[i].charAt(j) - 'a'));

LeetCode---Bit Manipulation && Design的更多相关文章

  1. 【LeetCode】1166. Design File System 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...

  2. 【LeetCode】355. Design Twitter 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...

  4. 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...

  5. 【LeetCode】707. Design Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】706. Design HashMap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】705. Design HashSet 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...

  8. 【Leetcode】355. Design Twitter

    题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...

  9. LeetCode算法题-Design LinkedList(Java实现)

    这是悦乐书的第300次更新,第319篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第168题(顺位题号是707).设计链表的实现.您可以选择使用单链表或双链表.单链表中的 ...

随机推荐

  1. 题解luoguP2054 BZOJ1965【[AHOI2005]洗牌】

    题目链接: https://www.luogu.org/problemnew/show/P2054 https://www.lydsy.com/JudgeOnline/problem.php?id=1 ...

  2. java八个框架

    在本文中,我只是整理了以下主流框架: 1.阿帕切米纳 项目主页:http://mina.apache.org/ 它为开发高性能和高可用性网络应用提供了一个非常方便的框架,支持基于Java NIO技术的 ...

  3. 解决docker pull 速度慢问题

    解决docker pull 速度慢问题 将docker镜像源修改为国内的: 在 /etc/docker/daemon.json 文件中添加以下参数(没有该文件则新建): { "registr ...

  4. span 如何移除点击事件

    //设置点击事件不可用 $("#verificode").css("pointer-events", "none"); //倒计时完毕,点击 ...

  5. 微服务之Nacos配置中心源码解析(二)

    Nacos配置中心源码解析 源码入口 ConfigFactory.createConfigService ConfigService configService = NacosFactory.crea ...

  6. mysql 中的 tinyint 字段

    只能存储  -128 ~ 127  之间的数字

  7. shell脚本获取传递的参数

    1 脚本编写 #!/bin/bash 2 解释 $n 表示是第几个参数 $0 表示脚本命令本身 3 执行效果

  8. 第二章· MySQL体系结构管理

    一.客户端与服务器模型  1.mysql是一个典型的C/S服务结构 1.1 mysql自带的客户端程序(/application/mysql/bin) mysql mysqladmin mysqld ...

  9. 关于单机部署fastdfs遇到的问题

    查找错误日志显示:/html/group1/M00/00/00/wKjJWFzdF0qAE1pBAACmOw57Lw0520_big.jpg" failed (2: No such file ...

  10. 配置LANMP环境(7)-- 配置nginx反向代理,与配置apache虚拟主机

    一.配置nginx反向代理 1.修改配置文件 vim /etc/nginx/nginx.conf 在35行http下添加一下内容: include /data/nginx/vhosts/*.conf; ...