**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. jvm自带的监控机制

    Jdk为我们提供了查看java服务运行时的监控情况 1.如下图所示,打开指定目录下的jconsole.exe应用程序文件. 2.双击后跳出如下界面,可以看到,我们可以监视本地的,也可以监视远程服务.本 ...

  2. flutter: Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.

    import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends State ...

  3. 解决Windows下文件无法删除的问题

    一.文件正在使用,文件已在另一程序中打开 图1已经提示了文件具体在哪个程序打开,在任务管理器结束相应的进程就可以删除文件了. 图2其实才是问题关键,怎样知道文件到底被哪个程序占用的呢? 解: Win+ ...

  4. deep_learning_MNIST数据集

    Code_link:https://pan.baidu.com/s/1dshQt57196fhh67F8nqWow 本文是为既没有机器学习基础也没了解过TensorFlow的码农.序媛们准备的.如果已 ...

  5. SPOJ 1825 经过不超过K个黑点的树上最长路径 点分治

    每一次枚举到重心 按子树中的黑点数SORT一下 启发式合并 #include<cstdio> #include<cstring> #include<algorithm&g ...

  6. Python中关于csv的简单操作

    Python中关于csv的简单操作 CSV操作简单,直接import csv即可, 主要使用reader和pandas 1 reader的简单使用 csv.reader("1.csv&quo ...

  7. python操作mysql代码讲解(及其实用,未来测试工作主要操作数据库的方法)

    pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 1 pip3 install pymysql 使用操作 1.执行SQL 1 2 3 4 ...

  8. java合并数组的几种方法,stream流合并数组

    一.实例代码 package cc.ash; import org.apache.commons.lang3.ArrayUtils; import java.lang.reflect.Array; i ...

  9. 箭头函数 -ES6

    1)函数参数只有一个:可以省略 ( ) var f = a => a     等同于 var f = function (a) { return a } 2)函数内部语句只有一个:可以省略 { ...

  10. 解决power designer 不能自动生成注释 commont 的解决办法只需要3步:

    解决power designer 不能自动生成注释的解决办法只需要3步: 一.快捷键 Ctrl+Shift+X 打开脚本编辑器:(快捷键不能执行的话可以从这个路径执行:Tools --> Exc ...