leetcode: 复杂度
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: 复杂度的更多相关文章
- [LeetCode] Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [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 ...
- [LeetCode] Employee Importance 员工重要度
You are given a data structure of employee information, which includes the employee's unique id, his ...
- 名人问题 算法解析与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 ...
- leetcode 181 Employees Earning More Than Their Managers 不会分析的数据库复杂度
https://leetcode.com/problems/employees-earning-more-than-their-managers/description/ 老师上课没分析这些的复杂度, ...
- [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 ...
- [LeetCode] 737. Sentence Similarity II 句子相似度 II
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [LeetCode] 734. Sentence Similarity 句子相似度
Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...
- [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 ...
随机推荐
- Experimental Educational Round: VolBIT Formulas Blitz J
Description IT City company developing computer games invented a new way to reward its employees. Af ...
- clip-path(css)
概述 clip-path属性可以防止部分元素通过定义的剪切区域来显示,仅通过显示的特殊区域.剪切区域是被URL定义的路径代替行内或者外部svg,或者定义路线的方法. [注意] IE浏览器不支持,且低 ...
- 【Effective Java】阅读
Java写了很多年,很惭愧,直到最近才读了这本经典之作<Effective Java>,按自己的理解总结下,有些可能还不够深刻 一.Creating and Destroying Obje ...
- asp.net AD 域验证
1.获取环境变量 string strAuthUser = request.ServerVariables["AUTH_USER"] 以上这行代码是用来获取当前用户的登录名 2.I ...
- 基于Visual Studio .NET2015的单元测试 OpenCover
https://www.cnblogs.com/XiaoRuLiang/p/10095723.html 基于Visual Studio .NET2015的单元测试 1. 在Visual Stud ...
- JS之scrollTop、offsetHeight和offsetTop等属性用法详解和拖拽div
标题中的几个相关相关属性在网页中有这大量的应用,尤其是在运动框架中,但是由于有些属性相互之间的概念比较混杂或者浏览器兼容性问题,导致掌握起来比较有难度,下面就介绍一下相关属性的用法.先来看一张比较经典 ...
- Storm与Spark区别
Storm擅长于动态处理大量实时生产的小数据块,概念上是将小数据量的数据源源不断传给过程: Spark擅长对现有的数据全集做处理,概念是将过程传给大数据量的数据. 二者设计思路相反.Storm侧重于处 ...
- Win7 IIS 局域网中无法访问网页
安装好iis后,在局域网中无法浏览网页一,关闭防火墙即可 或者建立入站规则 打开控制面板——window防火墙——高级设置 在入站规则上右键新建入站规则,选择端口然后下一步 选择tcp和特定端口在端口 ...
- free -m命令输出详解
free -m输出有3行: Mem:表示物理内存 -/+ buffers/cached:表示物理内存缓存 Swap:表示硬盘交换分区 其中Mem中的total.used.free.shared.buf ...
- [转] javascript另类方法高效实现htmlencode()与htmldecode()函数
本文转自:http://blog.csdn.net/cuixiping/article/details/7846806 最常见的做法是采用正则表达式替换的方法,将特殊字符如 < > &am ...