LeetCode---Bit Manipulation && Design
**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的更多相关文章
- 【LeetCode】1166. Design File System 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 目录树 日期 题目地址https://leetc ...
- 【LeetCode】355. Design Twitter 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】641. Design Circular Deque 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/design-ci ...
- 【LeetCode】622. Design Circular Queue 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 用直的代替弯的 数组循环利用 日期 题目地址:htt ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】706. Design HashMap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】705. Design HashSet 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 位图法 数组法 日期 题目地址:https://le ...
- 【Leetcode】355. Design Twitter
题目描述: Design a simplified version of Twitter where users can post tweets, follow/unfollow another us ...
- LeetCode算法题-Design LinkedList(Java实现)
这是悦乐书的第300次更新,第319篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第168题(顺位题号是707).设计链表的实现.您可以选择使用单链表或双链表.单链表中的 ...
随机推荐
- 搭建自己的框架WedeNet(四)
WedeNet2018.Web-UI层:结构如下: 首先,在Controller中定义BaseController,以便加入统一处理逻辑,如下: using log4net; using System ...
- ASE第二次结对编程——Code Search
复现极限模型 codenn 原理 其原理大致是将代码特征映射到一个向量,再将描述文字也映射到一个向量,将其cos距离作为loss训练. 对于代码特征,原论文提取了函数名.调用API序列和token集: ...
- 记录--js中出现的数组排序问题
这是今天在写vue项目时发生的一个小问题,在此记录一下,方便自己的回顾.项目是前后端分离的,前台主要使用了vue-cli3.0 + mintui,是一个移动端的web app包括了后台发布管理的一些功 ...
- linux--mysql 8.0.16--裸机安装
参考: https://www.cnblogs.com/warmsmile/p/10210739.html https://www.cnblogs.com/yg_zhang/p/10424926.ht ...
- LeetCode NO477.汉明距离总和
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量. 计算一个数组中,任意两个数之间汉明距离的总和. 示例: 输入: 4, 14, 2 输出: 6 解释: 在二进制表示中,4表示为010 ...
- LNMP安装与配置之CentOS7的安装。
LNMP与LAMP是我们常见的两种网站服务器架构.LNMP代表的就是Linux系统下Nginx+MySQL+PHP/Python,LAMP代表的则是Linux系统下Apache+MySQL+PHP/P ...
- C++第三次作业:作用域
作用域的定义 作用域是一个标识符在程序正文中有效的区域. 1.函数原型作用域 在函数原型声明时形式参数的作用范围就是函数原型作用域. 它是C++程序中最小的作用域. 例: double area(do ...
- cubase 使用冻结功能 节省电脑资源
- 数据库——Oracle(8)
1 标准SQL外连接(二) 1) 全外连接:查询所有表所有的数据 格式: select 别名1.*/列名,别名2.*/列名 from 表1 别名1 full outer join 表2 别名2 on ...
- java8学习之Stream介绍与操作方式详解
关于默认方法[default method]的思考: 在上一次[http://www.cnblogs.com/webor2006/p/8259057.html]中对接口的默认方法进行了学习,那在Jav ...