1. 程式人生 > 其它 >springboot整合mybatis:查詢語句,返回null

springboot整合mybatis:查詢語句,返回null

Java中的位操作

static int Integer.bitCount();           // 統計 1 的數量
static int Integer.highestOneBit();      // 獲得最高位
static String toBinaryString(int i);     // 轉換為二進位制表示的字串

  列出兩個比較使用位運算比較經典的題目(如下)

LeetCode260--陣列中不重複的兩個元素

  兩個不相等的元素在位級表示上必定會有一位存在不同。

  將陣列的所有元素異或得到的結果為不存在重複的兩個元素異或的結果。

  cur&= -cur得到出 cur 最右側不為 0 的位,也就是不存在重複的兩個元素在位級表示上最右側不同的那一位,利用這一位就可以將兩個元素區分開來。

class Solution {
    public int[] singleNumber(int[] nums) {
        int[] res = new int[2];
        int cur = 0;
        for(int num : nums) {
            cur ^= num;
        }
        cur &= -cur;
        for(int num : nums) {
            if((cur & num) == 0) res[0] ^= num;
            else
res[1] ^= num; } return res; } }

LeetCode318--最大單詞長度乘積

  因為本題目中只有26個字母,而int的二進位制是32位的,所以可以使用int來表示。

  使用&,如果兩個字串沒有相同的字母時,words[i] & words[j] = 0

class Solution {
    public int maxProduct(String[] words) {
        int n =words.length;
        int[] nums = new int[n];
        
int[] len = new int[n]; for(int i = 0; i < n; i++) { len[i] = words[i].length(); } for(int i = 0; i < n; i++) { int k = 0; for(char ch : words[i].toCharArray()) { k |= 1 << (ch - 'a'); } nums[i] = k; } int maxx = 0; for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if((nums[i] & nums[j]) == 0) { maxx = Math.max(maxx, len[i] * len[j]); } } } return maxx; } }