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. 支持100+业务线、累计发布17万次|宜信容器云的A点与B点(分享实录)

    宜信公司从2018年初开始建设容器云,至今,容器云的常用基本功能已经趋于完善,主要包括服务管理.应用商店.Nginx配置.存储管理.CI/CD.权限管理等,支持100+业务线.3500+的容器运行.伴 ...

  2. MySQL如何删除#sql开头的临时表

    1.  现象 巡检时发现服务器磁盘空间不足,通过查看大文件进行筛选是发现有几个#sql开头的文件,且存在超过100G及10G以上的文件. 2. 原因 如果MySQL在一个 ALTER TABLE操作( ...

  3. java面试题干货96-125

    这部分主要是与Java Web和Web Service相关的面试题. 96.阐述Servlet和CGI的区别? 答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式运行 ...

  4. 使用vsCode配合IAR搭建arm开发环境

    众所周知IAR的编辑功能就是个垃圾,但是不得不承认IAR的编译器相当的牛X,经常以稳定可靠而著称,为此我们把VSCODE强大的编辑功能和IAR结合一下来加快我们的开发周期. 一.下载VSCODE并安装 ...

  5. java基础- Java编程规范与注释

    一 前言 java编程规约是指在java代码编写过程中通俗的约定:通常我们是要遵守这些规范:这好比我们在生活中要遵守的道德准则,如果你做的事情超出了道德的底线,那就有可能会受到社会抨击:在java编程 ...

  6. 基于C# WPF框架的贪吃蛇

    游戏开始界面 游戏开始 共有两条蛇,吃到红色食物加1分,吃到绿色毒食物减1分,知道0不减: 碰到墙壁游戏结束,碰到对方游戏结束,碰到自己游戏结束 此游戏通过Canvas画布布局,通过C#代码实现 游戏 ...

  7. CouchDB学习-维护

    官方文档 1 压缩 压缩操作是通过从数据库或者视图索引文件中移除无用的和老的数据减少硬盘使用空间.操作非常简单类似于其他数据库(SQLite等)管理系统. 在压缩目标期间,CouchDB将创建扩展名为 ...

  8. 防止 window.open 被拦截

    window.open('/app/dashbuilder.html?' + group.id, '_blank'); // 一般_self不会被拦截 // 改为 let newTab = windo ...

  9. Mysql 5.7.28离线包下载与配置

    下载链接:https://pan.baidu.com/s/1uPbBknyIebQRDt4k_RA58Q   提取码:14zi 将下载文件进行解压,我解压位置为:D:\Program Files\my ...

  10. sql server的简单分页

    --显示前条数据 select top(4) * from students; --pageSize:每页显示的条数 --pageNow:当前页 )) sno from students); --带条 ...