CTCI读书笔记

Chapter 1: Arrays and Strings.

1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

public class Solution {
   public boolean uniqueChar (String input) {
       // Assume we are talking about ASCII set.
       // Use an array of 8 integers to act as a "visited" set.
       if (input == null || input.length() == 0) {
           return true;
       }
       int[] visited = new int[8];
       for (int i = 0; i < input.length(); i++) {
           // check if the character is visited.
           if ((visited[input.charAt(i) / 32] >>> input.charAt(i) % 32) & 1 == 1) {
                 return false;
           } else {
               visited[input.charAt(i) / 32] |= (1 << input.charAt(i) % 32);
           }
       }
       return true;
   }
}

Time: O(n)

Space: O(1)


Alternative Solutions:

Use a boolean[], time O(n), space O(256)

Brute force, time O(n^2), space O(1)

Sort and linear scan, time O(nlogn), space O(1)

1.2 Write code to reverse a C-Style string.

public class Solution {
   public String reverseCStyle(String input) {
       // A C-Style string end with a null char, which is '\0'
       if (input == null || input.length() <= 2) {
           return input;
       }
       char[] charArr = input.toCharArray();
       int left = 0, right = input.length() - 2;
       while (left < right) {
           swap(charArr, left++, right--);
       }
       return new String(charArr);
   }
   private void swap(char[] charArr, int i, int j) {
       char tmp = charArr[i];
       charArr[i] = charArr[j];
       charArr[j] = tmp;
   }
}

Time: O(n)

Space: O(n)


Discussion:

【Java】数组不能通过toString方法转为字符串
  java里,所有的类,不管是java库里面的类,或者是你自己创建的类,全部是从object这个类继承的。object里有一个方法就是toString(),那么所有的类创建的时候,都有一个toString的方法。

  这个方法是干什么的呢?

  首先我们得了解,java输出用的函数print();是不接受对象直接输出的,只接受字符串或者数字之类的输出。那么你想把一个创建好的对象拿来输出怎么办?例如:

package com.spring.h3;

public class Test2 {
   public static void main(String[] args) {
       System.out.println("new Test2()==="+new Test2());
       //输出结果为:new Test2()===com.spring.h3.Test2@18a992f
   }
}
  按照print接受的类型来说,s1是不能直接输出的,那么是否代表这个是不能编译运行的呢?当然不是。因为当print检测到输出的是一个对象而不是字符或者数字时,那么它会去调用这个对象类里面的toString 方法,输出结果为[类型@哈希值]。Object类中的toString()方法的源代码如下:

/**
* Returns a string representation of the object. In general, the
* <code>toString</code> method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* <p>
* The <code>toString</code> method for class <code>Object</code>
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `<code>@</code>', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return  a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
  而数组类中并没有对此方法重写(override),仅仅是重载(overload)为类的静态方法(参见java.util.Arrays)。所以,数组直接使用toString()的结果也是[类型@哈希值]。

  所以数组转为字符串应写成:

Arrays.toString(a) 
  这种方法的toString()是带格式的,也就是说输出的是[a, b, c],如果仅仅想输出abc则需用以下两种方法:

  方法1:直接在构造String时转换。

char[] data = {'a', 'b', 'c'};
String str = new String(data);
  方法2:调用String类的方法转换。

String.valueOf(char[] ch)

猜你喜欢

转载自www.cnblogs.com/juanqiuxiaoxiao/p/9271673.html
今日推荐