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 ...
随机推荐
- python为前端提供API
作为一名前端来学习后端语言,有难度啊.这里把第一次尝试的过程做个记录 1.网上看到Python给前端提供API可以使用python的flaskweb框架 #py文件 import json from ...
- 基于Storm的WordCount
Storm WordCount 工作过程 Storm 版本: 1.Spout 从外部数据源中读取数据,随机发送一个元组对象出去: 2.SplitBolt 接收 Spout 中输出的元组对象,将元组中的 ...
- 函数中this的指向
每个函数在被调用时都会自动取得两个特殊变量:this和arguments:内部函数在搜索这两个变量时,只会搜索到其活动对象为止,因此永远不可能直接访问外部函数中的这两个变量. f 1 var name ...
- 一遍文章搞清楚VO、DTO、DO、PO的概念、区别
作者:Cat Qi 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对 ...
- CentOS7安装部署squid服务(透明代理+反向代理)
一.squid服务介绍 Squid是一个高性能的代理缓存服务器,Squid支持FTP.gopher.HTTPS和HTTP协议.和一般的代理缓存软件不同,Squid用一个单独的.非模块化的.I/O驱动的 ...
- 远程桌面MATLAB启动失败问题解决
博客:博客园 | CSDN | blog 远程桌面打开MATLAB会报错,解决办法,打开matlab的licenses路径,如matlab/R2017b/licenses/,路径下存有license文 ...
- 2、手写Unity容器--第一层依赖注入
这个场景跟<手写Unity容器--极致简陋版Unity容器>不同,这里构造AndroidPhone的时候,AndroidPhone依赖于1个IPad 1.IPhone接口 namespac ...
- C#线程学习笔记七:Task详细用法
一.Task类简介: Task类是在.NET Framework 4.0中提供的新功能,主要用于异步操作的控制.它比Thread和ThreadPool提供了更为强大的功能,并且更方便使用. Task和 ...
- sqlserver数据库批量插入-SqlBulkCopy
当想在数据库中插入大量数据时,使用insert 不仅效率低,而且会导致一系列的数据库性能问题 当使用insert语句进行插入数据时.我使用了两种方式: 每次插入数据时,都只插入一条数据库,这个会导致每 ...
- Elasticsearch7.5.0源码编译
环境及工具 JDK12 Gradle5.6.2 GIT 源码及预处理 到github将代码clone下来,可以根据自己的需求来获取版本,例如 git checkout v7.5.0 提前下载gradl ...