LeetCode:Single Number II
来源:互联网 发布:在淘宝网上怎么买彩票 编辑:程序博客网 时间:2024/05/22 12:40
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?
// Source : https://oj.leetcode.com/problems/single-number-ii/// Author : Chao Zeng// Date : 2014-12-20class Solution{public: int array[32]; void countone(int n) { int k = 0; while (n) { array[k++] += n % 2; n /= 2; } } int singleNumber(int A[], int n) { for (int i = 0; i < 32; i++) array[i] = 0; int num = 0; //难点主要在于将负数转为正数然后求二进制数,然后确定所求数的符号 for (int i = 0; i < n; i++){ if (A[i] < 0){ num ++; A[i] = -A[i]; } countone (A[i]); } for (int i = 0; i < 32; i++) array[i] %= 3; int ans = 0; for (int i = 0; i < 32; i++){ int bonus = 1; if (array[i]) { for (int j = 0; j < i ; j++) bonus = bonus * 2; } else bonus = 0; ans = bonus + ans; } if (num % 3 == 1) ans = -ans; return ans; }};
0 0
- Single Number II - leetcode
- Leetcode: Single Number II
- [LeetCode] Single Number II
- LeetCode: Single Number II
- leetcode -- Single Number II
- [leetcode]Single Number II
- [LeetCode] Single Number II
- 【leetcode】Single Number II
- LeetCode:Single Number II
- Leetcode: Single Number II
- leetcode :Single Number II
- Leetcode Single Number II
- [LeetCode]Single Number II
- leetcode: Single Number (II)
- leetcode Single Number II
- leetcode Single Number II
- [LeetCode],Single Number II
- LeetCode | Single Number II
- Using SMTP, Gmail, and STARTTLS
- GNU C函数嵌套定义及闭包
- spring security3.x学习(20)_初探authorize标签和第四个例子
- UDP:socket服务端和接收端的基本使用实例
- 使用强制性的手段来结束一个进程
- LeetCode:Single Number II
- 算法导论-----------------0-1背包问题dp求解
- 百练 2574(八皇后DFS)
- QT vs2008 配置
- ARM的中断处理
- javascript数组的直接量语言允许有可选的结尾的逗号
- 十七周项目三--胖子伤不起
- 第十七周项目四—日期结构体
- struts.xml配置文件