2007年学习数据结构曾经把北大《数据结构与算法》(张铭、赵海燕、王腾蛟)教材书上的c++实现翻译为Java语言,现在拿来在LeetCode上跑跑,不是很理解。
Java
1 | /** |
Submission Detail
- 68 / 68 test cases passed.
- Runtime: 1 ms
- Your runtime beats 61.91 % of java submissions.
2007年学习数据结构曾经把北大《数据结构与算法》(张铭、赵海燕、王腾蛟)教材书上的c++实现翻译为Java语言,现在拿来在LeetCode上跑跑,不是很理解。
1 | /** |
Although its performance is not good, solution of Binary Tree Level Order Traversal is clear.
1 | /** |
2007年学习数据结构曾经用Java实现过,虽然还是知其然而不知其所以然,北大那本《数据结构与算法》(张铭、赵海燕、王腾蛟)教材上是用C++实现的。
1 | /** |
LeetCode – Divide Two Integers (Java) is the right answer, tested.
This problem can be solved based on the fact that any number can be converted to the format of the following, Power series of 2:
num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n
1 | class Solution { |
The solution of two stack is clear for me.
1 | class MinStack { |
1 | /** |
Simple as it labled, The problem is puzzled for it has problem with unsigned integer, and neith Java nor JavaScript has unsigned type. I’d like to pass it for some time. The code copied from Web passed in LeetCode, but it output wrong result for some input number.
solution from 190. Reverse Bits
There is no unsigned integer type. If you need to work with unsigned values originating outside your program, they must be stored in a larger signed type. For example, unsigned bytes produced by an analog-to-digital converter, can be read into variables of type short. — THE Java™ Programming Language, Fourth Edition, By Ken Arnold, James Gosling, David Holmes
1 | public class Solution { |
solution from 190. Reverse Bits – JavaScript 代码
1 | /** |
Operator | Description |
---|---|
| | Bitwise OR |
& | Bitwise AND |
~ | Bitwise Complement |
^ | Bitwise XOR |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
System.out.printf("%d*%d=%d", 2,8,2<<3);
System.out.printf("%d/2=%d",11,11>>1);
public static boolean isOddNum(int n) {
return (n & 1)==1;
}
public static int powerofTwo(int n) {
return 2<<(n-1);
}
1 | /** |
1 | /** |
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left | shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero fill right | shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
A linked list can be reversed either iteratively or recursively. Could you implement both?
© LeetCode – Reverse Linked List (Java) - Java Solution 1 - Iterative
1 | /** |
1 | /** |
© LeetCode – Reverse Linked List (Java) - Java Solution 2 - Recursive
1 | /** |
1 | /** |
1 | /** |
虽然是Easy的题,自己却想不出来,最后只好参考了这个 Given a string, find its first non-repeating character 的思路,倒也很容易看明白,一点就破的题。
1 | class Solution { |
1 | /** |
Project Object Model(项目对象模型)
在 POM 中,groupId, artifactId, packaging, version 叫作 maven 坐标,它能唯一的确定一个项目。有了 maven 坐标,我们就可以用它来指定我们的项目所依赖的其他项目,插件,或者父项目。一般 maven 坐标写成如下的格式:
groupId:artifactId:packaging:version
目录 | 目的 |
---|---|
${basedir} | 存放 pom.xml和所有的子目录 |
${basedir}/src/main/java | 项目的 java源代码 |
${basedir}/src/main/resources | 项目的资源,比如说 property文件 |
${basedir}/src/test/java | 项目的测试类,比如说 JUnit代码 |
${basedir}/src/test/resources | 测试使用的资源 |
mvn 本身不会做太多的事情,它不知道怎么样编译或者怎么样打包。它把构建的任务交给插件去做。插件定义了常用的构建逻辑,能够被重复利用。这样做的好处是,一旦插件有了更新,那么所有的 maven 用户都能得到更新。
生命周期指项目的构建过程,它包含了一系列的有序的阶段 (phase),而一个阶段就是构建过程中的一个步骤。
maven 能支持不同的生命周期,但是最常用的是默认的Maven生命周期 (default Maven lifecycle )。如果你没有对它进行任何的插件配置或者定制的话,那么上面的命令 mvn package 会依次执行默认生命周期中直到包括 package 阶段前的所有阶段的插件目标:
maven 默认的远程库(http://repo1.maven.org/maven2)
本地库是指 maven 下载了插件或者 jar 文件后存放在本地机器上的拷贝。在 Linux 上,它的位置在 ~/.m2/repository,在 Windows XP 上,在 C:\Documents and Settings\username.m2\repository ,在 Windows 7 上,在 C:\Users\username.m2\repository。
当 maven 查找需要的 jar 文件时,它会先在本地库中寻找,只有在找不到的情况下,才会去远程库中找。
The most commonly used build phases in the default build life cycle are:
Build Phase | Description |
---|---|
validate | Validates that the project is correct and all necessary information is available. This also makes sure the dependencies are downloaded. |
compile | Compiles the source code of the project. |
test | Runs the tests against the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
package | Packs the compiled code in its distributable format, such as a JAR. |
install | Install the package into the local repository, for use as a dependency in other projects locally. |
deploy | Copies the final package to the remote repository for sharing with other developers and projects. |