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 ...
随机推荐
- 线段树 区间更新(更新区间[x,y]的值,再求任意区间[x,y]的和)
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- hdu1028 Ignatius and the Princess III(递归、DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 封装OkHttp,通过改造Callback接口实现
1:实现Callback回调接口import android.os.Handler;import android.os.Looper;import android.os.Message; import ...
- vue(1)安装
1.安装node.js(https://nodejs.org/en/),我安装的是 v10.15.1 1).在nodejs安装路径下,新建node_global和node_cache两个文件夹 2). ...
- Java学习笔记day04_数组
1.switch case switch语句中表达式的数据类型是有要求的: JDK 1.0 ~ 1.4 , 数据类型接受byte, short, int, char JDK 1.5 , 数据类型接受b ...
- Dropping Balls UVA - 679(二叉树的遍历)
题目链接:https://vjudge.net/problem/UVA-679 题目大意:t组样例,每组包括D M 层数是D 问第M个小球落在哪个叶子节点? 每个节点有开关 刚开始全都 ...
- iframe高度自适应,自适应子页面高度
html <iframe id="mainFrame" name="mainFrame" scrolling="no" src=&qu ...
- python3+Appium自动化10-日志收集
日志概述 日志作用 日志是定位问题的重要手段 日志级别 级别 何时使用 DEBUG 调试信息,也是最详细的日志信息 INFO 证明事情按预期工作 WARNING 表明发生了一些意外,或者不就的将来(如 ...
- 创建weblogic domain
做成了PDF文档 这里可下载可以看 config.sh 命令目录 weblogic/Oracle/Middleware/wlserver_10.3/common/bin├── commEnv.sh├─ ...
- jq案例中遇到的知识点总结(会飞的小鸟和三级联动)
1.会飞的小鸟 ,按键盘的上下左右键,小鸟会上下左右的飞 知识点:1.keyCode 键盘按键对应的数字 比如 左上右下键 对应 37 38 39 40: 2.小鸟的位置:var bBird=$(&q ...