bjweimengshu / programmerxiaohui Goto Github PK
View Code? Open in Web Editor NEW图书《漫画算法》的全部源代码
图书《漫画算法》的全部源代码
public static TreeNode createBinaryTree(LinkedList inputList){
TreeNode node = null;
if(inputList==null || inputList.isEmpty()){
return null;
}
Integer data = inputList.removeFirst(); //这里会remove inputList
if(data != null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList); //这里会把所有的inputList递归remove完
node.rightChild = createBinaryTree(inputList);//等到执行到这里时,inputList是为空
}
return node;
}
//最终变成只有左节点的树
public void insert(int element,int index){
if(index<0 || index>size){
throw new IndexOutOfBoundsException("下标越界");
}
for (int i = size-1; i >=i; i--) {
array[i+1] = array[index];
}
array[index]=element;
size++;
}
正确应该为:
public void insert(int element,int index){
if(index<0 || index>size){
throw new IndexOutOfBoundsException("下标越界");
}
for (int i = size-1; i >=index; i--) {
array[i+1] = array[i];
}
array[index]=element;
size++;
}
小灰,书本第11页场景2内容有误。应为16cm的面包,每1min吃掉剩余长度一半。
if(data != null){
node = new TreeNode(data);
node.leftChild = createBinaryTree(inputList);
node.rightChild = createBinaryTree(inputList);
}
二叉树这里递归有问题。
应该是每三个元素递归一次,你这里的话左节点会穷尽所有元素。
https://github.com/bjweimengshu/ProgrammerXiaohui/blob/master/src/chapter4/part2/BubbleSort.java#L14
应该是
for(int i = 0; i < array.length-1; i++)
而不是
for(int i = 0; i < array.length; i++)
原文在for循环判断中的条件是i < size - 1;实际上使用这个条件是无法删除最后一个元素的,应改为i <= size - 1;或者直接写i < size
正确算法如下
**
* 删除数组元素
* @param index 被删除元素所在位置
* @return 被删除的元素
*/
public int delete(int index){
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("超出数组实际范围!");
}
int deletedElement = array[index];
for (int i = index; i < size; i++) {
array[i] = array[i + 1];
}
size--;
return deletedElement;
}
/**
* 后续遍历 基于栈遍历元素;
* 还有错误,该方法有问题。
*
* @param rootNode
*/
@Override
public void postOrderWithStack(Node rootNode) {
Stack<Node> stack = new Stack<>();
Node currentNode = rootNode;
/** root 根元素 不为null 并且 栈不为空
* */
while (currentNode != null || !stack.isEmpty()) {
/** 先遍历左子树*/
while (Objects.nonNull(currentNode)) {
stack.push(currentNode);
currentNode = currentNode.getLeftChild();
continue;
}
/** 再遍历右子树*/
if (!stack.isEmpty()) {
Node rightChild = stack.peek().rightChild;
if (Objects.nonNull(rightChild)) {
stack.push(rightChild);
currentNode = rightChild.rightChild;
continue;
}
}
/** 然后输出根节点 */
if (!stack.isEmpty()) {
Node pop = stack.pop();
System.out.println("key = " + pop.getKey() + " value = " + pop.getValue());
}
}
System.out.println("");
}
https://github.com/bjweimengshu/ProgrammerXiaohui/blob/master/src/chapter6/part5/Redpackage.java#L45
int segment = random.nextInt(totalAmount-2) + 1; // 当 segment = 1
int delta = random.nextInt(1)==0 ? 1 : -1; // 当 delta = -1
while(segments.contains(segment) || segment == 0){ // 当 segments已经有一个 1 了
segment = (segment+delta)%totalAmount;
// 第一次进来,(1-1)%totalAmount = 0
// 第二次进来,(0-1)%totalAmount = -1
// 跳出循环
}
这不就出现负数了吗?
MyArray
构造函数中,初始化数组,对应的数组长度是不是需要size也需要设置对应的值
public MyArray(int capacity){
this.array = new int[capacity];
size = 0;
}
第32行,应该是(1L << bitIndex) mod 64;吧。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.