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 ...
随机推荐
- POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 . 设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...
- day17 isinstance type issubclass 反射
1. issubclass,type,isinstance 1.issubclass 判断xxx是否yyy的子类 例: class Foo: pass class Bar(Foo): pass cla ...
- maven set MAVEN_OPTS
http://juvenshun.iteye.com/blog/240257 https://docs.alfresco.com/5.1/tasks/alfresco-sdk-install-mave ...
- ubuntu同时装有MXNet和Caffe框架
我阐述一下我遇到的问题:因为之前装过caffe,最近装了MXNet.MXNet可以运行,但import caffe就不行了,找不到模块. 那应该怎么处理呢??? 参考了一下这个网站:https://i ...
- ettercap局域网DNS欺骗实现过程
转载:https://www.cnblogs.com/hkleak/p/5043063.html 笔记一:ettercap是什么? 我们在对WEB安全检测的时候都会用到Cain和netfuke这两款工 ...
- 创建第一个vue工程
vue创建项目(npm安装→初始化项目) 第一步npm安装 首先:先从nodejs.org中下载nodejs 图1 双击安装,在安装界面一直Next 图2 图3 图4 直到Finish ...
- java——变量
1.静态变量: 随着类的加载而生成并初始化 随着类的消失而消失 2.成员变量: 随对象的加载而生成并初始化 随对象被回收而消失 3.局部变量: 作用范围由{}决定 随方法调用而创建 随方法的执行完毕而 ...
- leetcode 925. 长按键入
题目描述: 你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的 ...
- Not so Mobile UVA - 839
题目链接:https://vjudge.net/problem/UVA-839 题目大意:输入一个树状天平,根据力矩相等原则,判断是否平衡. 如上图所示,所谓力矩相等,就是Wl*Dl=Wr*Dr. ...
- TouchSlide 触屏滑动特效插件
TouchSlide 是纯javascript打造的触屏滑动特效插件,面向手机.平板电脑等移动终端,能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. 插件开源.体积小.简单实用.功能强大,是 ...