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的更多相关文章

  1. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  2. 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 ...

  3. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  4. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  5. LeetCode编程训练 - 位运算(Bit Manipulation)

    位运算基础 说到与(&).或(|).非(~).异或(^).位移等位运算,就得说到位运算的各种奇淫巧技,下面分运算符说明. 1. 与(&) 计算式 a&b,a.b各位中同为 1 ...

  6. [LeetCode] 190. Reverse Bits_Easy tag: Bit Manipulation

    Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 Explana ...

  7. [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 ...

  8. [LeetCode] 476. Number Complement_Easy tag: Bit Manipulation

    这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= ...

  9. Leetcode Tags(13)Bit Manipulation

    一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...

  10. [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 ...

随机推荐

  1. Oracle - 通过dg,完成单实例到rac的迁移

    一.概述 本文将介绍如何给单实例搭建一个rac dg,以及如何对其进行角色转换,完成从单实例到rac的迁移.预先具备的知识(rac搭建,单实例-单实例dg搭建) 二.实验环境介绍 主库(已有数据库实例 ...

  2. ssm整合——Mybatis配置(1)

    mybatis搭建-基于注解 1. 环境准备 1.1 新建maven的webapp项目 1.2 新建必要的目录和文件 1.3 文件配置 pom.xml junit默认创建是4.11,手动改成4.12 ...

  3. CCF-CSP题解 201812-3 CIDR合并

    题目想求与给定前缀列表等价的包含IP前缀数目最少的前缀列表. 首先是怎么存储前缀列表.用一个long long存储IP地址,再存一个前缀长度,封装在一个结构体里\(<ipNum, len> ...

  4. Java String indexOf()方法

    public class Test { public static void main(String[] args) { String s = "xXccxxxXX"; // 从头 ...

  5. 《Java基础知识》Java字符串详解

    本文内容: String类的概述 String类的使用步骤 String类的常用方法 本文目的: 能够使用String类的构造方法创建字符串对象 能够明确String类的构造方法创建对象,和直接赋值创 ...

  6. 从AlexNet(2012)开始

    目录 写在前面 网络结构 创新点 其他有意思的点 参考 博客:blog.shinelee.me | 博客园 | CSDN 写在前面 本文重点在于回顾深度神经网络在CV领域的First Blood--A ...

  7. JS---DOM---总结绑定事件的区别

    总结绑定事件的区别: addEventListener(); attachEvent()   相同点: 都可以为元素绑定事件 不同点: 1.方法名不一样 2.参数个数不一样addEventListen ...

  8. RAID磁盘阵列介绍

    磁盘阵列 RAID介绍 一.简介: 磁盘阵列(Redundant Arrays of Independent Drives,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意. 最初是由加利福尼亚 ...

  9. Cortex-A7处理器算数运算指令和逻辑运算指令

      汇编中也可以进行算术运算, 比如加减乘除,常用的运算指令用法如表所示: 常用运算指令 在嵌入式开发中最常会用的就是加减指令,乘除基本用不到. 我们用 C 语言进行CPU 寄存器配置的时候常常需要用 ...

  10. Java实现抢红包功能

    采用多线程模拟多人同时抢红包.服务端将玩家发出的红包保存在一个队列里,然后用Job定时将红包信息推送给玩家.每一批玩家的抢红包请求,其实操作的都是从队列中弹出的第一个红包元素,但当前的红包数量为空的时 ...