leetcode 137 && 360一面算法

题目描述:给一个数组,有一个数出现了两次或者1次,而其他数都出现了三次,找出这个数。其实对应了leetcode 137。

网上的解法多是位运算

public int singleNumber(int[] nums) {    
int ans = 0;   
 for(int i = 0; i < 32; i++) {      
  int sum = 0;        
  for(int j = 0; j < nums.length; j++) {         
    if(((nums[j] >> i) & 1) == 1) {
                sum++;
                sum %= 3;
            }
        }      
          if(sum == 1) {
            ans |= sum << i;
        }        
        if(sum == 2) {
            ans |= sum/2 << i
        }
    }    return ans;
}

利用位运算,求每位1出现的次数,出现3次的最后加起来%3==0. !=0的要么是1次,要么是2次。分情况讨论就行。最后的| 或运算,很强哦。。。

leetcode 137 && 360一面算法

时间: 2024-08-14 04:34:49

leetcode 137 && 360一面算法的相关文章

Leetcode 137 Single Number II 仅出现一次的数字

原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element appears three times except for one. Find that single one. 给出一个整数数组,除了某个元素外所有元素都出现三次.找出仅出现一次的数字. Note: 注意: Your algorithm should have a linear runtime co

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im

LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you

关于Leetcode上二叉树的算法总结

二叉树,结构很简单,只是比单链表复杂了那么一丢丢而已.我们先来看看它们结点上的差异: /* 单链表的结构 */ struct SingleList{ int element; struct SingleList *next; }; /* 二叉树的结构 */ struct BinaryTree{ int element; struct BinaryTree *left; struct BinaryTree *right; }; 根据以上两个结构,我们不难发现,单链表的结点只有一个指向下一结点的指针

[LeetCode]位操作有关的算法设计

在进行位操作算法设计之前,先了解位操作的一些细节知识点: 1. 位操作数据溢出的结果 2. 数据位提升的隐式转换 http://www.cnblogs.com/grandyang/p/4606334.html

LeetCode 137:Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Single Number II 比Single Number要复杂的多,

[LeetCode] Permutations 排列生成算法之字典序法

字典序排序生成算法 字典序法就是按照字典排序的思想逐一产生所有排列. 例如,由1,2,3,4组成的所有排列,从小到大的依次为: 1234, 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431, 3124, 3142, 3214, 3241, 3412, 3421, 4123, 4132, 4213, 4231, 4312, 4321. 分析这种过程,看后一个排列与前一个排列之间有什么关系? 再如,设有排列(p)=276

[LeetCode] 137. 只出现一次的数字 II

题目链接 : https://leetcode-cn.com/problems/single-number-ii/ 题目描述: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例: 示例 1: 输入: [2,2,3,2] 输出: 3 示例 2: 输入: [0,1,0,1,0,1,99] 输出: 99 思路: 占坑, 这道题要重点研究一下! 代码: 原文地址:htt

LeetCode 135 Candy(贪心算法)

135. Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get mo