GithubHelp home page GithubHelp logo

algorithms's People

Contributors

yangxiaohei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

algorithms's Issues

2.4.9

这道题A B C D E不止4种堆, A 跟 B有出现在第二层的情况

2-3-15

螺帽-螺丝,如果那第一次找到的螺丝来分螺帽,如果这个螺丝不是最值,还是无法匹配

Practise_3_1_21.java

关于SequentialSearchST的内存使用:

a node:
16(overhead) + 8(key) +8(val) + 8(next node reference) + 8 (inner class overhead) = 48 bytes

For N nodes:
The total memory used should be 48N.

Linked lists. A nested non-static (inner) class such as our Node class requires an extra 8 bytes of overhead (for a reference to the enclosing instance).

3.1.2 delete关于方法建议

    public void delete(K key) {
        if (key == null) return;
        for (int i = 0; i < size; i++) {
            if (entrys[i].key.equals(key)) {
                Entry<K, V> t = entrys[size - 1]; **多余的**
                entrys[size - 1] = entrys[i]; **多余的**
                **entrys[i] = t;**
                entrys[--size] = null;
                if (size > 0 && size == entrys.length >> 2)
                    resize(entrys.length >> 1);
            }
        }
    }

直接把i位置上的元素替换成最后一个元素,然后将最后一个元素置空,再size-1。会不会更好些?
entrys[i] = entrys[size-1];
entrys[--size] = null;

关于练习1.4.3的疑问

你好大神:
看了一下关于1.4.13的答案,有两个疑问:
1.书中提到引用类型变量需要占8个字节,FixedCapacityStackOfString里有一个String数组, 因为数组本身也是对象,所以应该包含这个数组变量对应的8个字节,我的计算结果是:
16(对象头) + 8(String[] 变量) + 4(int 变量) + 4(填充) = 32,如果还要包含String数组实际大小时,应该是 32 + (64 + 2N) * C. 注意,这里的N表示每个字符串的平均长度

  1. Point2D 明明包含如下变量:
  /**
   * Compares two points by x-coordinate.
   */
  public static final Comparator<Point2D> X_ORDER = new XOrder();

  /**
   * Compares two points by y-coordinate.
   */
  public static final Comparator<Point2D> Y_ORDER = new YOrder();

  /**
   * Compares two points by polar radius.
   */
  public static final Comparator<Point2D> R_ORDER = new ROrder();

而且三个变量都是引用类型,每个应该占有8个字节,为什么不统计它们,而单单统计double类型的变量?

1.4.12 会打印出重复的公共元素,1.4.15 计算整数对数量有误

1.4.12:

/**
 * @author ZZY
 */
public class Ex12 {

    /**
     * 使用 hashmap,元素为键。
     * O(N)
     */
    public static void printCommon(int[] a, int[] b) {
        Map<Integer, Integer> valueMap = new HashMap<>(16);
        //先循环一个数组存入 hashmap,所有键对应的值都是 1
        for (Integer i : a
        ) {
            int count = 1;
            valueMap.put(i, count);
        }
        //再循环第二个数组,如果有重复的键,就直接打印
        for (Integer i : b
        ) {
                if (valueMap.containsKey(i)) {
                    StdOut.print(i + " ");
                    //已经打印过的就删除防止二次打印
                    valueMap.remove(i);
                }
        }
    }

    /**
     * 不使用 hashmap,当数组中元素有重复时,就可能重复打印公共元素,例如:
     *
     * 两个数组的元素:
     * 2 5 6 7 16 20 21 28 34 41 47 69 74 84 87 92 96 97 97 98
     * 8 12 19 24 25 26 28 30 33 33 54 58 63 82 84 89 93 93 97 97
     * 公共元素:
     * 28 84 97
     * 28 84 97 97
     *
     * 因为数组 a 中有相邻相等的元素 97,且数组 b 中也有相邻相等的元素 97,结果会打印两次 97
     *
     * O(N)
     */
    public static void printCommon2(int[] a, int[] b) {
        if (a == null || b == null) {
            throw new NullPointerException();
        }
        int i = 0, j = 0;
        while (i < a.length && j < b.length) {
            //此时,b 中索引小于 j 的都不可能是公共元素
            if (a[i] > b[j]) {
                j++;
                //此时,a 中索引小于 i 的都不可能是公共元素
            } else if (a[i] < b[j]) {
                i++;
                //a[i] == b[j] 时,打印元素
            } else {
                StdOut.print(a[i] + " ");
                i++;
                j++;
            }
        }
    }

    public static int[] sourceArr(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = StdRandom.uniform(0, 100);
        }
        Arrays.sort(arr);
        return arr;
    }

    public static void main(String[] args) {
        int[] a = sourceArr(20);
        int[] b = sourceArr(20);
        System.out.println("两个数组的元素:");
        for (int i : a
        ) {
            System.out.print(i + " ");
        }
        System.out.println();
        for (int i : b
        ) {
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println("公共元素:");
        printCommon(a, b);
        System.out.println();
        printCommon2(a, b);
    }
}  

1.4.15:
```java
/**
 * 快速 3-sum,要求:
 * 线性级别,和为 0 的整数对的个数
 * 前提:数组有序
 * <p>
 * 先实现线性级别的 twosum,再实现平方级别的 3-sum
 */
public class Ex15 {

    /**
     * 思路:和为零说明两个数一正一负或者都为零,数组有序,负数在左侧正数在右侧,
     * 两个索引一个从零开始,一个从数组尾开始向中间遍历
     */
    public static int twoSumCount(int[] a) {
        if (a == null || a[0] > 0){
            return 0;
        }
        int first = 0;
        int last = a.length - 1;
        int count = 0;
        while (last > first) {
            if (a[last] + a[first] < 0) {
                first++;
            } else if (a[last] + a[first] > 0) {
                last--;
            } else {
                //相等时,last 向左推进找到所有与 a[last] 相等的正数,计数为 positiveCount
                //first 向右推进找到所有与 a[first] 相等的负数,计数为 negativeCount
                //整数对的个数为 positiveCount * negativeCount
                int preFirst = first;
                int preLast = last;
                int positiveCount;
                int negativeCount;

                //如果该数组中有 0,则当两指针相等且均为 0 时,计算 0 的个数并得到整数对的个数,最后返回 count
                if (a[first] == 0) {
                    int zeroNum = last - first + 1;
                    count += zeroNum * (zeroNum - 1) / 2;
                    return count;
                }
                while (a[first] == a[preFirst]) {
                    first++;
                }
                while (a[last] == a[preLast]) {
                    last--;
                }
                positiveCount = preLast - last;
                negativeCount = first - preFirst;
                count += positiveCount * negativeCount;
            }
        }
        return count;
    }

    /**
     * 原答案
     * */
    public static int count(int[] a) {
        if (a == null || a.length == 0 || a[0] > 0) {
            return 0;
        }
        int cnt = 0, N = a.length, lo = 0, hi = N - 1;
        while (lo <= hi) {
            if 		(a[lo] + a[hi] < 0) {
                lo++;
            } else if (a[lo] + a[hi] > 0) {
                hi--;
            } else 	{ cnt++; lo++; hi--; }
        }
        return cnt;
    }

    public static int[] sourceArr(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = StdRandom.uniform(-10, 10);
        }
        Arrays.sort(arr);
        return arr;
    }
/**
     * -9 -9 -8 -8 -7 -6 -5 -5 -3 -3 -1 0 0 1 3 3 3 4 7 7 
     * 10
     * 5
     * */
    public static void main(String[] args) {
        int[] a = sourceArr(20);
        for (int i : a
        ) {
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println(twoSumCount(a));
        System.out.println();
        System.out.println(count(a));
    }
}  

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.