1. single-number

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?

给定一个数组,每个元素都出现2次除了其中的一个,找出只出现一次的数字注意:算法必须是线性时间复杂度,可以不使用额外空间实现吗?

//异或运算,不同为1 相同为0
public int singleNumber(int[] A) {
int x = 0;
for (int a : A) {
x = x ^ a;
}
return x;
}

2.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?

除了一个元素外,其它元素都是出现三次,求那个元素?

如果是除了一个元素,其它的都出现两次,则所有的数异或就是结果。

int 数据共有32位,可以用32变量存储 这 N 个元素中各个二进制位上  1  出现的次数,最后 在进行 模三 操作,如果为1,那说明这一位是要找元素二进制表示中为 1 的那一位。

public class Solution{
public int singleNumber(int [] A){
if(A==null || A.length ==0){
return -1;
}
int result=0;
int[] bits=new int[32];
for(int i=0;i<32;i++){
for(int j=0;j<A.length;j++){
bits[i]+=A[j]>>i&1;
bits[i]%=3;
}
result |=(bits[i] << i);
}
return result;
}
}

3.reverse-integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

public int reverse(int x) {
int rev = 0;
while(x != 0){
rev = rev*10 + x%10;
x = x/10;
} return rev;
}
 
public int reverse(int x) {
//flag marks if x is negative
boolean flag = false;
if (x < 0) {
x = 0 - x;
flag = true;
} int res = 0;
int p = x; while (p > 0) {
int mod = p % 10;
p = p / 10;
res = res * 10 + mod;
} if (flag) {
res = 0 - res;
} return res;
}

4.longest-common-prefix

Write a function to find the longest common prefix string amongst an array of strings.

找出所有字符串的最长公共前缀

public class Solution {

    // 1. Method 1, start from the first one, compare prefix with next string, until end;
// 2. Method 2, start from the first char, compare it with all string, and then the second char
// I am using method 1 here
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) {
return "";
}
String prefix = strs[0];
for(int i = 1; i < strs.length; i++) {
int j = 0;
while( j < strs[i].length() && j < prefix.length() && strs[i].charAt(j) == prefix.charAt(j)) {
j++;
}
if( j == 0) {
return "";
}
prefix = prefix.substring(0, j);
}
return prefix;
} }

leetcode: 复杂度的更多相关文章

  1. [LeetCode] Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  2. [LeetCode] Degree of an Array 数组的度

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  3. [LeetCode] Employee Importance 员工重要度

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  4. 名人问题 算法解析与Python 实现 O(n) 复杂度 (以Leetcode 277. Find the Celebrity为例)

    1. 题目描述 Problem Description Leetcode 277. Find the Celebrity Suppose you are at a party with n peopl ...

  5. leetcode 181 Employees Earning More Than Their Managers 不会分析的数据库复杂度

    https://leetcode.com/problems/employees-earning-more-than-their-managers/description/ 老师上课没分析这些的复杂度, ...

  6. [LeetCode] 854. K-Similar Strings 相似度为K的字符串

    Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two ...

  7. [LeetCode] 737. Sentence Similarity II 句子相似度 II

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  8. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  9. [LeetCode] 697. Degree of an Array 数组的度

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

随机推荐

  1. 六度分离 (folyd算法)

    1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为"小世界现象(small world phenomenon)"的著名假说,大意是说,任何2个素不相识的人中间最多只隔着 ...

  2. 封装OkHttp,通过改造Callback接口实现

    1:实现Callback回调接口import android.os.Handler;import android.os.Looper;import android.os.Message; import ...

  3. 傻瓜式Spring教学第二课

    什么是依赖注入 先说什么是依赖 如下: class A{ B b; } class B{ } 则称A依赖B. 依赖:A的某些业务逻辑需要B的参与,如果不对A中的参数b进行实例化,那么A中的某些业务逻辑 ...

  4. js模仿微信语音播放的小功能

    自己写的一个模仿微信语音播放的小功能,实现的主要功能是:点击播放,点击暂停,播放切换,,,  代码如下: <!DOCTYPE html> <html lang="en&qu ...

  5. css 断行省略号,隐藏,fixed定位

    text-overflow(clip | elipsis)(显示省略号| 不显示省略号) white-space:nowrap    强制文字不断行 word-break:break-all;     ...

  6. 转 rac中并行 PARALLEL 的设置

    sample 1: rac中并 行的设置 https://blog.csdn.net/wll_1017/article/details/8285574 我们的生产库一般在节点一上的压力比较大,在节点二 ...

  7. django建表报错

     今天在用pycharm创建数据库的表的时候碰见了一个报错,这还是头一次碰见这种奇怪的错误.这里记住希望能避免错误. 我所创建的表是用的django原有的用户表,做了一下继承. class UserI ...

  8. gitee 搭建自己的代码仓库

    git是什么? 还是科普一下吧:Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理(来自我大百度) 现在越来越多的公司已经使用git来进行项目的版本管理,,使用起来 ...

  9. sublime text2 for mac 实现json格式化

    问题描述: 网上拿下来的一大段json格式的字符串,放到sublime上格式化成json的标准格式 数据截图如下: 解决问题: 网络上搜了一下,大部分都是说要装pretty json插件 先来看看自己 ...

  10. c# 调用matlab

    1.返回一个整数 function [ a ] = testReturn( b ) %UNTITLED Summary of this function goes here % Detailed ex ...