-------------------------------- 这道题好坑啊,自己说是2*n+1个数字,结果有组测试数据竟然传了个空数组进来... 经典位算法: n^n==0 n^0==n AC代码: public class Solution { /** *@param A : an integer array *return : a integer */ public int singleNumber(int[] A) { if(A.length==0) return 0; int n=A…
Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题目标签:Hash Table 题目给了我们一个 nums array, array 里…
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O(k) 解题 上一题刚介绍过所用的方法,但是这个确实很复杂的 先利用HashMap实现 public class Solution { /** * @param nums: A list of integers * @param k: As described * @return: The major…
题目 丑数 设计一个算法,找出只含素因子3,5,7 的第 k 大的数. 符合条件的数如:3,5,7,9,15...... 样例 如果k=4, 返回 9 挑战 要求时间复杂度为O(nlogn)或者O(n) 解题 法一:直接暴力,逐次判断一个数是不是丑数 下面只对其中的奇数判断是否是丑数,加不加奇数都超时. class Solution { /** * @param k: The number k. * @return: The kth prime number as description. */…
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. Example: Input: low = "50"…
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "50&qu…
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is…
Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Have you met this question in a real interview? Example Given dividend = 100 and divisor = 9, return 11. LeetCode上的原题,请参见我之前的博客Divid…
136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,…
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. public class Solution { /** * @param matrix, a list of lists of integers * @param target, an integer * @return a boolean, indicate whether matrix contains…