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. //异或运算,不同为1 相同为0
    public int singleNumber(int[] A) {
  2. int x = 0;
  3. for (int a : A) {
  4. x = x ^ a;
  5. }
  6. return x;
  7. }

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 的那一位。

  1. public class Solution{
  2. public int singleNumber(int [] A){
  3. if(A==null || A.length ==0){
  4. return -1;
  5. }
  6. int result=0;
  7. int[] bits=new int[32];
  8. for(int i=0;i<32;i++){
  9. for(int j=0;j<A.length;j++){
  10. bits[i]+=A[j]>>i&1;
  11. bits[i]%=3;
  12. }
  13. result |=(bits[i] << i);
  14. }
  15. return result;
  16. }
  17. }

3.reverse-integer

Reverse digits of an integer.

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

  1. public int reverse(int x) {
  2. int rev = 0;
  3. while(x != 0){
  4. rev = rev*10 + x%10;
  5. x = x/10;
  6. }
  7.  
  8. return rev;
  9. }
 
  1. public int reverse(int x) {
  2. //flag marks if x is negative
  3. boolean flag = false;
  4. if (x < 0) {
  5. x = 0 - x;
  6. flag = true;
  7. }
  8.  
  9. int res = 0;
  10. int p = x;
  11.  
  12. while (p > 0) {
  13. int mod = p % 10;
  14. p = p / 10;
  15. res = res * 10 + mod;
  16. }
  17.  
  18. if (flag) {
  19. res = 0 - res;
  20. }
  21.  
  22. return res;
  23. }

4.longest-common-prefix

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

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

  1. public class Solution {
  2.  
  3. // 1. Method 1, start from the first one, compare prefix with next string, until end;
  4. // 2. Method 2, start from the first char, compare it with all string, and then the second char
  5. // I am using method 1 here
  6. public String longestCommonPrefix(String[] strs) {
  7. if (strs == null || strs.length == 0) {
  8. return "";
  9. }
  10. String prefix = strs[0];
  11. for(int i = 1; i < strs.length; i++) {
  12. int j = 0;
  13. while( j < strs[i].length() && j < prefix.length() && strs[i].charAt(j) == prefix.charAt(j)) {
  14. j++;
  15. }
  16. if( j == 0) {
  17. return "";
  18. }
  19. prefix = prefix.substring(0, j);
  20. }
  21. return prefix;
  22. }
  23.  
  24. }

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. 你不得不知道的5个神奇的Docker工具

    Docker社区非常活跃,每天都会推出大量有用的工具.要想持续追踪社区中发生的各项创新其实非常困难.为了帮助你,我收集了一些每天在日常工作中使用.令人感兴趣并且十分有用的Docker工具.这些工具消除 ...

  2. JavaScript trim 实现去除字符串首尾指定字符的简单方法

    String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...

  3. 技巧:Python中print打印信息的同时打印文件、行号

    import sys def Log(msg): print('Print Message: '+msg+' ,File: "'+__file__+'", Line '+str(s ...

  4. 如何 将下载离线 nupkg 文件 安装到VS2017

      https://www.cnblogs.com/cncc/articles/8276878.html   --------------------------------------------- ...

  5. 24-----BBS论坛

    BBS论坛(二十四) 24.1.编辑板块 cms/js/banners.js $(function () { $('.edit-board-btn').click(function () { var ...

  6. PlayMaker 操作界面超级详细介绍

    原文:https://www.indienova.com/u/christiantam/blogread/1214

  7. Sqlite操作的一些关键类的官方说明与Intent的startactivityforresult方法

    Intent: 该功能可以用于通过intent来跳转界面时候传递信号给原理的页面,以便做出一些处理: sqlite的使用: 该方法得到的sqlitedatabase可读可写,而getreadabled ...

  8. Kure讲HTML_如何学习HTML

    HTML即是超文本标记语言,它主要是用来构建网页的轮廓的.HTML自身包含了众多的API(应用程序接口:即HTML暴露给Web前端开发者的语言特性,当然作为开发者就应该更多的关注这个.)话不多说,直接 ...

  9. Flume启动错误之:Bootstrap Servers must be specified

    今天测试项目的时候需要启动Flume,然而在启动时遇到了Bootstrap Servers must be specified错误,错误日志如下: [kfk@bigdata-pro01 flume-- ...

  10. linux下文件比对功能

    很想对吧两个文本有什么不同,可linux下有没有那么方便的工具,怎么办?其实也很简单:diff命令,一行搞定. 新建a.txt文件