Bit Manipulation-leetcode
Bit Manipulation
Find the Difference
/*
* Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
*/
public class Lc389 {
/*
* 思路:置换
*
* 由于t比仅多一位,则将t的最后为假定成不同的字符,然后t和s做差.
*/
public static char findTheDifference(String s, String t) {
char[] charS = s.toCharArray();
char[] charT = t.toCharArray();
// abcd
// abcde
int res = t.charAt(s.length());
for (int i = 0; i < charS.length; i++) {
res -= (int) charS[i];
res += (int) charT[i];
}
return (char) res;
}
public static void main(String[] args) {
String s = "abcd";
String t = "abcde";
System.out.println(findTheDifference(s, t));
}
}
Single Number
import java.util.Arrays;
/*
*
* iven a non-empty array of integers, every element appears twice except for one. Find that single one.
*
*
*Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/
public class Lc136 {
public static int singleNumber(int[] nums) {
if (nums.length == 0) {
return 0;
}
Arrays.sort(nums);
/*
* 三种情况 1 22 33 11 2 33 11 22 3
*/
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] != nums[i + 1] && i == 0) {
return nums[0];
}
if (nums[i] != nums[i + 1] && nums[i + 1] != nums[i + 2]) {
return nums[i + 1];
}
}
return nums[nums.length - 1];
}
public static void main(String[] args) {
int[] nums = { 4, 1, 2, 1, 2 };
System.out.println(singleNumber(nums));
}
}
Maximum Product of Word Lengths
/*
* Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
* 给与一个字符串数组,找到俩个单词长度乘积的最大值,要求,单词不共享相同字母,你可以认为所有的单词都是小写字母,如果没有这样的俩个三次存在则返回0;
*/
public class Lc318 {
/*
* 思路:在计算机中int由32位,小写的字母一个26个可以让后26为对应26个字母,对应位置由该字母则为1,反之则为0;
*
* 对一个单词进行左移以及或操作知道一个单词所有字母都处理完成。一次类推
*
* 比较俩个单词的对应数字进行与操作,(都为则1)如果为0,即俩个单词没有一个字母重复则进行计算
*
* 注意:俩个单词必须完全不相同,没有一个字母重复才可以。
*/
public static int maxProduct(String[] words) {
int[] mask = new int[words.length];
int res = 0;
for (int i = 0; i < words.length; i++) {
for (char c : words[i].toCharArray()) {
mask[i] |= 1 << (c - 'a');
}
// 这里,最大遍历的位置为i-1
for (int j = 0; j < i; j++) {
if ((mask[i] & mask[j]) == 0) {
res = Math.max(res, words[i].length() * words[j].length());
}
}
}
return res;
}
public static void main(String[] args) {
String words[] = { "abcd", "cde" };
System.out.println(maxProduct(words));
}
}
Bit Manipulation-leetcode的更多相关文章
- [LeetCode] All questions numbers conclusion 所有题目题号
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...
- Leetcode: Sum of Two Integers && Summary: Bit Manipulation
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- LeetCode编程训练 - 位运算(Bit Manipulation)
位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...
- [LeetCode] 190. Reverse Bits_Easy tag: Bit Manipulation
Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 Explana ...
- [LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- [LeetCode] 476. Number Complement_Easy tag: Bit Manipulation
这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= ...
- Leetcode Tags(13)Bit Manipulation
一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
随机推荐
- 手动启动Oracle服务的.bat文件
Oracle数据库的基本服务会占用很大的内存,有的程序员会在不用的时候Oracle服务关闭来减少对电脑内存资源的占用. 我在这准备了一个可以开启/关闭Oracle服务的bat文件,希望被采纳!!! 新 ...
- .NET Core环境变量和用户秘钥实现开发中的数据安全
目录 一.注入 IConfiguration 二.从配置文件 appsettings.json 中获取环境变量 三.从项目中获取环境变量 四.用户秘钥设置环境变量 前言:有很多人将秘钥,数据库连接字符 ...
- java设计模式(二)单例模式,一生只爱一人,只争一朝一夕
单例模式:保证一个类在内存中的对象唯一,有且仅能实例化一次.(如多个代码块需要读取配置文件,or开启事务,orjdbc读取数据源就是个经典例子)参考:吟啸且徐行 实现步骤: 私有构造方法.保证唯一的 ...
- python基础知识第九篇(函数)
函数 >>>>>>>>>>> : 使用函数的好处 1.代码重用 2.保持一致性,方便维护 3.可扩展性 定义方法 def test01 ...
- 计算机等级考试真题1(JAVA)
答案: 01-05 C D A A C 06-10 B/D C C C B 11-15 A C A C A 16-20 C B C 21-25 D D C D D 26-30 ...
- [ASP.NET Core 3框架揭秘] 跨平台开发体验: Linux
如果想体验Linux环境下开发.NET Core应用,我们有多种选择.一种就是在一台物理机上安装原生的Linux,我们可以根据自身的喜好选择某种Linux Distribution,目前来说像RHEL ...
- Node.js+Express+MongoDB数据库实现网页注册登入功能
通过 Node.js + Express + MongoDB 实现网页注册账号 和 登入账号的功能 项目准备: 1: 事先准备好项目的页面 (首页页面 index.html)(登入页面 login.h ...
- 面试连环炮系列(三):synchronized怎么用的
synchronized怎么用的? 用过,synchronized是常用的并发控制关键字,简单的说就是访问加锁.它可以修饰静态方法或者一个类的class对象,这叫类锁:可以修饰普通方法或者代码块,这叫 ...
- Python爬虫基础面试题,为2020年初就业做准备
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:qiaoziheng 一.题目部分1.python中常用的数据结构有哪些 ...
- Linux 周期任务
一次性任务 在某个特定的时间,执行一次后被清除 相关命令/进程 at 命令 atd进程 在centos6中,系统服务的名称: /etc/init.d/atd 查看系统上该进程时候启动: [root@e ...