cur = mIterators.get(mCurIdx);
-
- if (cur.hasNext()) {
- return true;
- }
- mIterators.remove(mCurIdx);
- }
- return false;
- }
-}
diff --git a/gradle.properties b/gradle.properties
deleted file mode 100644
index aac7c9b..0000000
--- a/gradle.properties
+++ /dev/null
@@ -1,17 +0,0 @@
-# Project-wide Gradle settings.
-
-# IDE (e.g. Android Studio) users:
-# Gradle settings configured through the IDE *will override*
-# any settings specified in this file.
-
-# For more details on how to configure your build environment visit
-# http://www.gradle.org/docs/current/userguide/build_environment.html
-
-# Specifies the JVM arguments used for the daemon process.
-# The setting is particularly useful for tweaking memory settings.
-org.gradle.jvmargs=-Xmx1536m
-
-# When configured, Gradle will run in incubating parallel mode.
-# This option should only be used with decoupled projects. More details, visit
-# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
-# org.gradle.parallel=true
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
deleted file mode 100644
index 13372ae..0000000
Binary files a/gradle/wrapper/gradle-wrapper.jar and /dev/null differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
deleted file mode 100644
index c7a4e33..0000000
--- a/gradle/wrapper/gradle-wrapper.properties
+++ /dev/null
@@ -1,6 +0,0 @@
-#Mon Mar 20 10:01:35 CST 2017
-distributionBase=GRADLE_USER_HOME
-distributionPath=wrapper/dists
-zipStoreBase=GRADLE_USER_HOME
-zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-all.zip
diff --git a/gradlew b/gradlew
deleted file mode 100644
index 9d82f78..0000000
--- a/gradlew
+++ /dev/null
@@ -1,160 +0,0 @@
-#!/usr/bin/env bash
-
-##############################################################################
-##
-## Gradle start up script for UN*X
-##
-##############################################################################
-
-# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-DEFAULT_JVM_OPTS=""
-
-APP_NAME="Gradle"
-APP_BASE_NAME=`basename "$0"`
-
-# Use the maximum available, or set MAX_FD != -1 to use that value.
-MAX_FD="maximum"
-
-warn ( ) {
- echo "$*"
-}
-
-die ( ) {
- echo
- echo "$*"
- echo
- exit 1
-}
-
-# OS specific support (must be 'true' or 'false').
-cygwin=false
-msys=false
-darwin=false
-case "`uname`" in
- CYGWIN* )
- cygwin=true
- ;;
- Darwin* )
- darwin=true
- ;;
- MINGW* )
- msys=true
- ;;
-esac
-
-# Attempt to set APP_HOME
-# Resolve links: $0 may be a link
-PRG="$0"
-# Need this for relative symlinks.
-while [ -h "$PRG" ] ; do
- ls=`ls -ld "$PRG"`
- link=`expr "$ls" : '.*-> \(.*\)$'`
- if expr "$link" : '/.*' > /dev/null; then
- PRG="$link"
- else
- PRG=`dirname "$PRG"`"/$link"
- fi
-done
-SAVED="`pwd`"
-cd "`dirname \"$PRG\"`/" >/dev/null
-APP_HOME="`pwd -P`"
-cd "$SAVED" >/dev/null
-
-CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
-
-# Determine the Java command to use to start the JVM.
-if [ -n "$JAVA_HOME" ] ; then
- if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
- # IBM's JDK on AIX uses strange locations for the executables
- JAVACMD="$JAVA_HOME/jre/sh/java"
- else
- JAVACMD="$JAVA_HOME/bin/java"
- fi
- if [ ! -x "$JAVACMD" ] ; then
- die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
-
-Please set the JAVA_HOME variable in your environment to match the
-location of your Java installation."
- fi
-else
- JAVACMD="java"
- which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
-
-Please set the JAVA_HOME variable in your environment to match the
-location of your Java installation."
-fi
-
-# Increase the maximum file descriptors if we can.
-if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
- MAX_FD_LIMIT=`ulimit -H -n`
- if [ $? -eq 0 ] ; then
- if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
- MAX_FD="$MAX_FD_LIMIT"
- fi
- ulimit -n $MAX_FD
- if [ $? -ne 0 ] ; then
- warn "Could not set maximum file descriptor limit: $MAX_FD"
- fi
- else
- warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
- fi
-fi
-
-# For Darwin, add options to specify how the application appears in the dock
-if $darwin; then
- GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
-fi
-
-# For Cygwin, switch paths to Windows format before running java
-if $cygwin ; then
- APP_HOME=`cygpath --path --mixed "$APP_HOME"`
- CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
- JAVACMD=`cygpath --unix "$JAVACMD"`
-
- # We build the pattern for arguments to be converted via cygpath
- ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
- SEP=""
- for dir in $ROOTDIRSRAW ; do
- ROOTDIRS="$ROOTDIRS$SEP$dir"
- SEP="|"
- done
- OURCYGPATTERN="(^($ROOTDIRS))"
- # Add a user-defined pattern to the cygpath arguments
- if [ "$GRADLE_CYGPATTERN" != "" ] ; then
- OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
- fi
- # Now convert the arguments - kludge to limit ourselves to /bin/sh
- i=0
- for arg in "$@" ; do
- CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
- CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
-
- if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
- eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
- else
- eval `echo args$i`="\"$arg\""
- fi
- i=$((i+1))
- done
- case $i in
- (0) set -- ;;
- (1) set -- "$args0" ;;
- (2) set -- "$args0" "$args1" ;;
- (3) set -- "$args0" "$args1" "$args2" ;;
- (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
- (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
- (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
- (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
- (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
- (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
- esac
-fi
-
-# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
-function splitJvmOpts() {
- JVM_OPTS=("$@")
-}
-eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
-JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
-
-exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
diff --git a/gradlew.bat b/gradlew.bat
deleted file mode 100644
index 8a0b282..0000000
--- a/gradlew.bat
+++ /dev/null
@@ -1,90 +0,0 @@
-@if "%DEBUG%" == "" @echo off
-@rem ##########################################################################
-@rem
-@rem Gradle startup script for Windows
-@rem
-@rem ##########################################################################
-
-@rem Set local scope for the variables with windows NT shell
-if "%OS%"=="Windows_NT" setlocal
-
-@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
-set DEFAULT_JVM_OPTS=
-
-set DIRNAME=%~dp0
-if "%DIRNAME%" == "" set DIRNAME=.
-set APP_BASE_NAME=%~n0
-set APP_HOME=%DIRNAME%
-
-@rem Find java.exe
-if defined JAVA_HOME goto findJavaFromJavaHome
-
-set JAVA_EXE=java.exe
-%JAVA_EXE% -version >NUL 2>&1
-if "%ERRORLEVEL%" == "0" goto init
-
-echo.
-echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
-echo.
-echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
-
-goto fail
-
-:findJavaFromJavaHome
-set JAVA_HOME=%JAVA_HOME:"=%
-set JAVA_EXE=%JAVA_HOME%/bin/java.exe
-
-if exist "%JAVA_EXE%" goto init
-
-echo.
-echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
-echo.
-echo Please set the JAVA_HOME variable in your environment to match the
-echo location of your Java installation.
-
-goto fail
-
-:init
-@rem Get command-line arguments, handling Windowz variants
-
-if not "%OS%" == "Windows_NT" goto win9xME_args
-if "%@eval[2+2]" == "4" goto 4NT_args
-
-:win9xME_args
-@rem Slurp the command line arguments.
-set CMD_LINE_ARGS=
-set _SKIP=2
-
-:win9xME_args_slurp
-if "x%~1" == "x" goto execute
-
-set CMD_LINE_ARGS=%*
-goto execute
-
-:4NT_args
-@rem Get arguments from the 4NT Shell from JP Software
-set CMD_LINE_ARGS=%$
-
-:execute
-@rem Setup the command line
-
-set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
-
-@rem Execute Gradle
-"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
-
-:end
-@rem End local scope for the variables with windows NT shell
-if "%ERRORLEVEL%"=="0" goto mainEnd
-
-:fail
-rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
-rem the _cmd.exe /c_ return code!
-if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
-exit /b 1
-
-:mainEnd
-if "%OS%"=="Windows_NT" endlocal
-
-:omega
diff --git a/leetcode.iml b/leetcode.iml
deleted file mode 100644
index ce86a2c..0000000
--- a/leetcode.iml
+++ /dev/null
@@ -1,19 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/leetcode/common/common.iml b/leetcode/common/common.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/leetcode/common/common.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/library/src/main/java/com/leetcode/library/CommonUtils.java b/leetcode/common/src/CommonUtils.java
similarity index 77%
rename from library/src/main/java/com/leetcode/library/CommonUtils.java
rename to leetcode/common/src/CommonUtils.java
index f40173c..4e25df5 100644
--- a/library/src/main/java/com/leetcode/library/CommonUtils.java
+++ b/leetcode/common/src/CommonUtils.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 2016/12/12.
- */
-
public class CommonUtils {
diff --git a/library/src/main/java/com/leetcode/library/Interval.java b/leetcode/common/src/Interval.java
similarity index 76%
rename from library/src/main/java/com/leetcode/library/Interval.java
rename to leetcode/common/src/Interval.java
index b1e3dd6..749239e 100644
--- a/library/src/main/java/com/leetcode/library/Interval.java
+++ b/leetcode/common/src/Interval.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
/**
* Definition for an interval.
*/
diff --git a/library/src/main/java/com/leetcode/library/ListNode.java b/leetcode/common/src/ListNode.java
similarity index 83%
rename from library/src/main/java/com/leetcode/library/ListNode.java
rename to leetcode/common/src/ListNode.java
index 7902529..02be290 100644
--- a/library/src/main/java/com/leetcode/library/ListNode.java
+++ b/leetcode/common/src/ListNode.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
-
public class ListNode {
public int val;
diff --git a/library/src/main/java/com/leetcode/library/NestedInteger.java b/leetcode/common/src/NestedInteger.java
similarity index 87%
rename from library/src/main/java/com/leetcode/library/NestedInteger.java
rename to leetcode/common/src/NestedInteger.java
index ce8f1d3..39cb29e 100644
--- a/library/src/main/java/com/leetcode/library/NestedInteger.java
+++ b/leetcode/common/src/NestedInteger.java
@@ -1,11 +1,5 @@
-package com.leetcode.library;
-
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/11/22.
- */
-
public interface NestedInteger {
// @return true if this NestedInteger holds a single integer, rather than a nested list.
diff --git a/library/src/main/java/com/leetcode/library/Point.java b/leetcode/common/src/Point.java
similarity index 87%
rename from library/src/main/java/com/leetcode/library/Point.java
rename to leetcode/common/src/Point.java
index aeda913..991a20a 100644
--- a/library/src/main/java/com/leetcode/library/Point.java
+++ b/leetcode/common/src/Point.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 16/12/7.
- */
-
class Point {
int x;
int y;
diff --git a/library/src/main/java/com/leetcode/library/RandomListNode.java b/leetcode/common/src/RandomListNode.java
similarity index 68%
rename from library/src/main/java/com/leetcode/library/RandomListNode.java
rename to leetcode/common/src/RandomListNode.java
index e3eb267..8816baa 100644
--- a/library/src/main/java/com/leetcode/library/RandomListNode.java
+++ b/leetcode/common/src/RandomListNode.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by liwentian on 17/8/9.
- */
-
public class RandomListNode {
public int label;
public RandomListNode next, random;
diff --git a/library/src/main/java/com/leetcode/library/TreeLinkNode.java b/leetcode/common/src/TreeLinkNode.java
similarity index 72%
rename from library/src/main/java/com/leetcode/library/TreeLinkNode.java
rename to leetcode/common/src/TreeLinkNode.java
index d1122b3..0b21dca 100644
--- a/library/src/main/java/com/leetcode/library/TreeLinkNode.java
+++ b/leetcode/common/src/TreeLinkNode.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
-
/**
* Definition for binary tree with next pointer.
*/
diff --git a/library/src/main/java/com/leetcode/library/TreeNode.java b/leetcode/common/src/TreeNode.java
similarity index 93%
rename from library/src/main/java/com/leetcode/library/TreeNode.java
rename to leetcode/common/src/TreeNode.java
index e028f26..6a1f777 100644
--- a/library/src/main/java/com/leetcode/library/TreeNode.java
+++ b/leetcode/common/src/TreeNode.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
-
public class TreeNode {
public int val;
diff --git a/library/src/main/java/com/leetcode/library/TrieNode.java b/leetcode/common/src/TrieNode.java
similarity index 64%
rename from library/src/main/java/com/leetcode/library/TrieNode.java
rename to leetcode/common/src/TrieNode.java
index 5053139..58a80df 100644
--- a/library/src/main/java/com/leetcode/library/TrieNode.java
+++ b/leetcode/common/src/TrieNode.java
@@ -1,9 +1,3 @@
-package com.leetcode.library;
-
-/**
- * Created by dingjikerbo on 2016/11/22.
- */
-
public class TrieNode {
public TrieNode[] nodes;
diff --git a/library/src/main/java/com/leetcode/library/UndirectedGraphNode.java b/leetcode/common/src/UndirectedGraphNode.java
similarity index 78%
rename from library/src/main/java/com/leetcode/library/UndirectedGraphNode.java
rename to leetcode/common/src/UndirectedGraphNode.java
index 068b8c6..5193303 100644
--- a/library/src/main/java/com/leetcode/library/UndirectedGraphNode.java
+++ b/leetcode/common/src/UndirectedGraphNode.java
@@ -1,12 +1,6 @@
-package com.leetcode.library;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/19.
- */
-
public class UndirectedGraphNode {
public int label;
diff --git a/leetcode/common/src/Utils.java b/leetcode/common/src/Utils.java
new file mode 100644
index 0000000..fdedf69
--- /dev/null
+++ b/leetcode/common/src/Utils.java
@@ -0,0 +1,10 @@
+public class Utils {
+
+ public static int max(int... val) {
+ int max = Integer.MIN_VALUE;
+ for (int k : val) {
+ max = Math.max(max, k);
+ }
+ return max;
+ }
+}
diff --git a/leetcode/leetcode.iml b/leetcode/leetcode.iml
new file mode 100644
index 0000000..da061d4
--- /dev/null
+++ b/leetcode/leetcode.iml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/leetcode/solution/solution.iml b/leetcode/solution/solution.iml
new file mode 100644
index 0000000..99e828d
--- /dev/null
+++ b/leetcode/solution/solution.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/leetcode/solution/src/AddBinary.java b/leetcode/solution/src/AddBinary.java
new file mode 100644
index 0000000..b755a6c
--- /dev/null
+++ b/leetcode/solution/src/AddBinary.java
@@ -0,0 +1,14 @@
+public class AddBinary {
+
+ public String addBinary(String a, String b) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = a.length() - 1, j = b.length() - 1, flag = 0; i >= 0 || j >= 0 || flag > 0; i--, j--) {
+ int k1 = i >= 0 ? a.charAt(i) - '0' : 0;
+ int k2 = j >= 0 ? b.charAt(j) - '0' : 0;
+ int sum = k1 + k2 + flag;
+ sb.append(sum % 2);
+ flag = sum / 2;
+ }
+ return sb.reverse().toString();
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/AddTwoNumber.java b/leetcode/solution/src/AddTwoNumber.java
similarity index 83%
rename from solution/src/main/java/com/inuker/solution/AddTwoNumber.java
rename to leetcode/solution/src/AddTwoNumber.java
index 42c3576..9469ff4 100644
--- a/solution/src/main/java/com/inuker/solution/AddTwoNumber.java
+++ b/leetcode/solution/src/AddTwoNumber.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class AddTwoNumber {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
diff --git a/solution/src/main/java/com/inuker/solution/AddTwoNumbersII.java b/leetcode/solution/src/AddTwoNumbersII.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/AddTwoNumbersII.java
rename to leetcode/solution/src/AddTwoNumbersII.java
index 7331e93..08ca684 100644
--- a/solution/src/main/java/com/inuker/solution/AddTwoNumbersII.java
+++ b/leetcode/solution/src/AddTwoNumbersII.java
@@ -1,13 +1,5 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
import java.util.Stack;
-/**
- * Created by liwentian on 2017/9/11.
- */
-
public class AddTwoNumbersII {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
diff --git a/solution/src/main/java/com/inuker/solution/AlienDictionary.java b/leetcode/solution/src/AlienDictionary.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/AlienDictionary.java
rename to leetcode/solution/src/AlienDictionary.java
index 8c45184..6fd8f05 100644
--- a/solution/src/main/java/com/inuker/solution/AlienDictionary.java
+++ b/leetcode/solution/src/AlienDictionary.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.Queue;
-import java.util.Set;
+import java.util.*;
/**
- * Created by dingjikerbo on 16/12/17.
*
* TestCases
* "z", "z" => "z"
diff --git a/solution/src/main/java/com/inuker/solution/AndroidUnlockPatterns.java b/leetcode/solution/src/AndroidUnlockPatterns.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/AndroidUnlockPatterns.java
rename to leetcode/solution/src/AndroidUnlockPatterns.java
index e3790fb..6912537 100644
--- a/solution/src/main/java/com/inuker/solution/AndroidUnlockPatterns.java
+++ b/leetcode/solution/src/AndroidUnlockPatterns.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/6.
- */
-
/**
* https://leetcode.com/articles/android-unlock-patterns/
*/
diff --git a/solution/src/main/java/com/inuker/solution/AverageOfLevelsInBinaryTree.java b/leetcode/solution/src/AverageOfLevelsInBinaryTree.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/AverageOfLevelsInBinaryTree.java
rename to leetcode/solution/src/AverageOfLevelsInBinaryTree.java
index ff9b246..1324566 100644
--- a/solution/src/main/java/com/inuker/solution/AverageOfLevelsInBinaryTree.java
+++ b/leetcode/solution/src/AverageOfLevelsInBinaryTree.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 2017/7/22.
- */
-
public class AverageOfLevelsInBinaryTree {
public List averageOfLevels(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/BSTIterator.java b/leetcode/solution/src/BSTIterator.java
similarity index 55%
rename from solution/src/main/java/com/inuker/solution/BSTIterator.java
rename to leetcode/solution/src/BSTIterator.java
index a3dc689..1b6519d 100644
--- a/solution/src/main/java/com/inuker/solution/BSTIterator.java
+++ b/leetcode/solution/src/BSTIterator.java
@@ -1,25 +1,13 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/11/21.
- */
-
public class BSTIterator {
private Stack mStack;
private TreeNode mCurNode;
public BSTIterator(TreeNode root) {
- mStack = new Stack();
+ mStack = new Stack<>();
mCurNode = root;
-
-// for (mCurNode = root; mCurNode != null; mCurNode = mCurNode.left) {
-// mStack.push(mCurNode);
-// }
}
/**
@@ -49,19 +37,4 @@ public int next() {
return result;
}
-
- /** 这样写也好,不过要注意在构造函数中初始化如上注释部分
- public int next() {
- if (mCurNode == null) {
- mCurNode = mStack.pop();
- }
-
- int val = mCurNode.val;
-
- for (mCurNode = mCurNode.right; mCurNode != null; mCurNode = mCurNode.left) {
- mStack.push(mCurNode);
- }
-
- return val;
- }*/
}
diff --git a/solution/src/main/java/com/inuker/solution/BalancedBinaryTree.java b/leetcode/solution/src/BalancedBinaryTree.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/BalancedBinaryTree.java
rename to leetcode/solution/src/BalancedBinaryTree.java
index 525fe8a..0e88812 100644
--- a/solution/src/main/java/com/inuker/solution/BalancedBinaryTree.java
+++ b/leetcode/solution/src/BalancedBinaryTree.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/28.
- */
-
-import com.leetcode.library.TreeNode;
-
/**
* 平衡二叉树条件是左边是平衡,右边是平衡,且两边高度差相差不超过1
* 树的高度是所有子树的最大高度
diff --git a/solution/src/main/java/com/inuker/solution/BasicCalculator.java b/leetcode/solution/src/BasicCalculator.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/BasicCalculator.java
rename to leetcode/solution/src/BasicCalculator.java
index 51194d3..a246d72 100644
--- a/solution/src/main/java/com/inuker/solution/BasicCalculator.java
+++ b/leetcode/solution/src/BasicCalculator.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Stack;
-/**
- * Created by liwentian on 2017/9/12.
- */
-
public class BasicCalculator {
public int calculate(String s) {
diff --git a/solution/src/main/java/com/inuker/solution/BeautifulArrangement.java b/leetcode/solution/src/BeautifulArrangement.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/BeautifulArrangement.java
rename to leetcode/solution/src/BeautifulArrangement.java
index de49e8b..8c7ebb0 100644
--- a/solution/src/main/java/com/inuker/solution/BeautifulArrangement.java
+++ b/leetcode/solution/src/BeautifulArrangement.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/6.
- */
-
/**
* https://leetcode.com/articles/beautiful-arrangement/
*/
diff --git a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStock.java b/leetcode/solution/src/BestTimeBuySellStock.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/BestTimeBuySellStock.java
rename to leetcode/solution/src/BestTimeBuySellStock.java
index 19fd88f..8661ea7 100644
--- a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStock.java
+++ b/leetcode/solution/src/BestTimeBuySellStock.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
/**
* https://leetcode.com/articles/best-time-buy-and-sell-stock/
*/
diff --git a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockII.java b/leetcode/solution/src/BestTimeBuySellStockII.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/BestTimeBuySellStockII.java
rename to leetcode/solution/src/BestTimeBuySellStockII.java
index c26588f..4428037 100644
--- a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockII.java
+++ b/leetcode/solution/src/BestTimeBuySellStockII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
/**
* https://leetcode.com/articles/best-time-buy-and-sell-stock-ii/
*/
diff --git a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIII.java b/leetcode/solution/src/BestTimeBuySellStockIII.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIII.java
rename to leetcode/solution/src/BestTimeBuySellStockIII.java
index 7505fe0..f97d430 100644
--- a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIII.java
+++ b/leetcode/solution/src/BestTimeBuySellStockIII.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
-/**
- */
public class BestTimeBuySellStockIII {
public int maxProfit(int[] prices) {
diff --git a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIV.java b/leetcode/solution/src/BestTimeBuySellStockIV.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIV.java
rename to leetcode/solution/src/BestTimeBuySellStockIV.java
index 62733db..72e784e 100644
--- a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockIV.java
+++ b/leetcode/solution/src/BestTimeBuySellStockIV.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
-/**
- */
public class BestTimeBuySellStockIV {
public int maxProfit(int k, int[] prices) {
diff --git a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockWithCoolDown.java b/leetcode/solution/src/BestTimeBuySellStockWithCoolDown.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/BestTimeBuySellStockWithCoolDown.java
rename to leetcode/solution/src/BestTimeBuySellStockWithCoolDown.java
index ad799c9..e0acc92 100644
--- a/solution/src/main/java/com/inuker/solution/BestTimeBuySellStockWithCoolDown.java
+++ b/leetcode/solution/src/BestTimeBuySellStockWithCoolDown.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
-/**
- */
public class BestTimeBuySellStockWithCoolDown {
public int maxProfit(int[] prices) {
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeInorderTraversal.java b/leetcode/solution/src/BinaryTreeInorderTraversal.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeInorderTraversal.java
rename to leetcode/solution/src/BinaryTreeInorderTraversal.java
index 4b65895..f7cc6c1 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeInorderTraversal.java
+++ b/leetcode/solution/src/BinaryTreeInorderTraversal.java
@@ -1,14 +1,10 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
- * Created by dingjikerbo on 16/12/1.
+ * https://leetcode.com/articles/binary-tree-inorder-traversal/
*/
public class BinaryTreeInorderTraversal {
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeLevelOrderTraversal.java b/leetcode/solution/src/BinaryTreeLevelOrderTraversal.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeLevelOrderTraversal.java
rename to leetcode/solution/src/BinaryTreeLevelOrderTraversal.java
index 4b8e235..f132a20 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeLevelOrderTraversal.java
+++ b/leetcode/solution/src/BinaryTreeLevelOrderTraversal.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class BinaryTreeLevelOrderTraversal {
// 耗时2ms
diff --git a/amazon/src/main/java/com/leetcode/amazon/BinaryTreeLevelOrderTraversal.java b/leetcode/solution/src/BinaryTreeLevelOrderTraversalII.java
similarity index 66%
rename from amazon/src/main/java/com/leetcode/amazon/BinaryTreeLevelOrderTraversal.java
rename to leetcode/solution/src/BinaryTreeLevelOrderTraversalII.java
index 470d700..f14eb4c 100644
--- a/amazon/src/main/java/com/leetcode/amazon/BinaryTreeLevelOrderTraversal.java
+++ b/leetcode/solution/src/BinaryTreeLevelOrderTraversalII.java
@@ -1,18 +1,12 @@
-package com.leetcode.amazon;
-
-import com.leetcode.library.TreeNode;
-
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by liwentian on 17/8/12.
- */
-
-public class BinaryTreeLevelOrderTraversal {
+public class BinaryTreeLevelOrderTraversalII {
- public List> levelOrder(TreeNode root) {
+ // 耗时2ms,和I一样,只不过加到result时添加到头
+ public List> levelOrderBottom(TreeNode root) {
List> result = new LinkedList<>();
if (root == null) {
@@ -22,36 +16,33 @@ public List> levelOrder(TreeNode root) {
Queue queue = new LinkedList<>();
Queue next = new LinkedList<>();
- queue.add(root);
-
List list = null;
+ queue.offer(root);
+
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (list == null) {
- list = new LinkedList<>();
+ list = new ArrayList<>();
}
list.add(node.val);
if (node.left != null) {
- next.add(node.left);
+ next.offer(node.left);
}
-
if (node.right != null) {
- next.add(node.right);
+ next.offer(node.right);
}
if (queue.isEmpty()) {
- result.add(list);
- list = null;
-
queue.addAll(next);
next.clear();
+ result.add(0, list);
+ list = null;
}
}
-
return result;
}
}
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeLongestConsecutiveSequence.java b/leetcode/solution/src/BinaryTreeLongestConsecutiveSequence.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeLongestConsecutiveSequence.java
rename to leetcode/solution/src/BinaryTreeLongestConsecutiveSequence.java
index a766b34..611244b 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeLongestConsecutiveSequence.java
+++ b/leetcode/solution/src/BinaryTreeLongestConsecutiveSequence.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/2.
- */
-
-import com.leetcode.library.TreeNode;
-
/**
* https://leetcode.com/articles/binary-tree-longest-consecutive-sequence/
*/
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeMaximumPathSum.java b/leetcode/solution/src/BinaryTreeMaximumPathSum.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeMaximumPathSum.java
rename to leetcode/solution/src/BinaryTreeMaximumPathSum.java
index e67097c..d7159b2 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeMaximumPathSum.java
+++ b/leetcode/solution/src/BinaryTreeMaximumPathSum.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/28.
- */
-
-import com.leetcode.library.TreeNode;
-
/**
* 单边的或者双边的,或只包含根节点的
*/
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreePaths.java b/leetcode/solution/src/BinaryTreePaths.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/BinaryTreePaths.java
rename to leetcode/solution/src/BinaryTreePaths.java
index 1a919b5..a09e484 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreePaths.java
+++ b/leetcode/solution/src/BinaryTreePaths.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
public class BinaryTreePaths {
// 耗时2ms
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreePostorderTraversal.java b/leetcode/solution/src/BinaryTreePostorderTraversal.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/BinaryTreePostorderTraversal.java
rename to leetcode/solution/src/BinaryTreePostorderTraversal.java
index 019889b..2d9558b 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreePostorderTraversal.java
+++ b/leetcode/solution/src/BinaryTreePostorderTraversal.java
@@ -1,16 +1,8 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/1.
- */
-
public class BinaryTreePostorderTraversal {
/**
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreePreorderTraversal.java b/leetcode/solution/src/BinaryTreePreorderTraversal.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/BinaryTreePreorderTraversal.java
rename to leetcode/solution/src/BinaryTreePreorderTraversal.java
index e88e4be..891c8e3 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreePreorderTraversal.java
+++ b/leetcode/solution/src/BinaryTreePreorderTraversal.java
@@ -1,16 +1,8 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/1.
- */
-
public class BinaryTreePreorderTraversal {
public List preorderTraversal(TreeNode root) {
diff --git a/amazon/src/main/java/com/leetcode/amazon/BinaryTreeRightSideView.java b/leetcode/solution/src/BinaryTreeRightSideView.java
similarity index 72%
rename from amazon/src/main/java/com/leetcode/amazon/BinaryTreeRightSideView.java
rename to leetcode/solution/src/BinaryTreeRightSideView.java
index 62ff01d..4a8dcc8 100644
--- a/amazon/src/main/java/com/leetcode/amazon/BinaryTreeRightSideView.java
+++ b/leetcode/solution/src/BinaryTreeRightSideView.java
@@ -1,31 +1,28 @@
-package com.leetcode.amazon;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by liwentian on 17/8/12.
- */
-
public class BinaryTreeRightSideView {
public List rightSideView(TreeNode root) {
- List list = new LinkedList<>();
+ List result = new LinkedList<>();
if (root == null) {
- return list;
+ return result;
}
Queue queue = new LinkedList<>();
Queue next = new LinkedList<>();
- queue.add(root);
+
+ TreeNode last = null;
+
+ queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
+ last = node;
+
if (node.left != null) {
next.offer(node.left);
}
@@ -35,13 +32,12 @@ public List rightSideView(TreeNode root) {
}
if (queue.isEmpty()) {
- list.add(node.val);
-
queue.addAll(next);
next.clear();
+ result.add(last.val);
}
}
- return list;
+ return result;
}
}
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeUpsideDown.java b/leetcode/solution/src/BinaryTreeUpsideDown.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeUpsideDown.java
rename to leetcode/solution/src/BinaryTreeUpsideDown.java
index 6dedc73..f86829c 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeUpsideDown.java
+++ b/leetcode/solution/src/BinaryTreeUpsideDown.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/12/2.
- */
-
public class BinaryTreeUpsideDown {
/**
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeVerticalOrderTraversal.java b/leetcode/solution/src/BinaryTreeVerticalOrderTraversal.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeVerticalOrderTraversal.java
rename to leetcode/solution/src/BinaryTreeVerticalOrderTraversal.java
index 7440106..79365f5 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeVerticalOrderTraversal.java
+++ b/leetcode/solution/src/BinaryTreeVerticalOrderTraversal.java
@@ -1,16 +1,8 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 2016/11/19.
- */
-
public class BinaryTreeVerticalOrderTraversal {
private int minKey = 0, maxKey = 0;
diff --git a/solution/src/main/java/com/inuker/solution/BinaryTreeZigzagLevelOrderTraversal.java b/leetcode/solution/src/BinaryTreeZigzagLevelOrderTraversal.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/BinaryTreeZigzagLevelOrderTraversal.java
rename to leetcode/solution/src/BinaryTreeZigzagLevelOrderTraversal.java
index 5175477..8641f80 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryTreeZigzagLevelOrderTraversal.java
+++ b/leetcode/solution/src/BinaryTreeZigzagLevelOrderTraversal.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/2.
- */
-
public class BinaryTreeZigzagLevelOrderTraversal {
public List> zigzagLevelOrder(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/BinaryWatch.java b/leetcode/solution/src/BinaryWatch.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/BinaryWatch.java
rename to leetcode/solution/src/BinaryWatch.java
index 4c08f08..d23e246 100644
--- a/solution/src/main/java/com/inuker/solution/BinaryWatch.java
+++ b/leetcode/solution/src/BinaryWatch.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 2016/12/29.
- */
-
public class BinaryWatch {
public List readBinaryWatch(int num) {
diff --git a/solution/src/main/java/com/inuker/solution/BombEnemy.java b/leetcode/solution/src/BombEnemy.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/BombEnemy.java
rename to leetcode/solution/src/BombEnemy.java
index 2c66aef..0f75d72 100644
--- a/solution/src/main/java/com/inuker/solution/BombEnemy.java
+++ b/leetcode/solution/src/BombEnemy.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/11.
- */
-
public class BombEnemy {
public int maxKilledEnemies(char[][] grid) {
diff --git a/solution/src/main/java/com/inuker/solution/BulbSwitcher.java b/leetcode/solution/src/BulbSwitcher.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/BulbSwitcher.java
rename to leetcode/solution/src/BulbSwitcher.java
index 499c847..ba9d281 100644
--- a/solution/src/main/java/com/inuker/solution/BulbSwitcher.java
+++ b/leetcode/solution/src/BulbSwitcher.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/8.
- */
-
public class BulbSwitcher {
/**
* https://discuss.leetcode.com/topic/39558/share-my-o-1-solution-with-explanation/2
diff --git a/solution/src/main/java/com/inuker/solution/BurstBalloons.java b/leetcode/solution/src/BurstBalloons.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/BurstBalloons.java
rename to leetcode/solution/src/BurstBalloons.java
index dc1f2e8..c05a99a 100644
--- a/solution/src/main/java/com/inuker/solution/BurstBalloons.java
+++ b/leetcode/solution/src/BurstBalloons.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/21.
- */
-
/**
* 这题用的闭区间DP,dp[start][end]表示区间start,end内所有气球爆掉的最大coin
* 假设最后爆第i个气球,start <= i <= end,则对应的coin为
diff --git a/solution/src/main/java/com/inuker/solution/CanArrangeWords.java b/leetcode/solution/src/CanArrangeWords.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/CanArrangeWords.java
rename to leetcode/solution/src/CanArrangeWords.java
index f14128c..a48632a 100644
--- a/solution/src/main/java/com/inuker/solution/CanArrangeWords.java
+++ b/leetcode/solution/src/CanArrangeWords.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.HashSet;
-/**
- * Created by dingjikerbo on 2017/9/29.
- */
-
/**
* 单词接龙,前一个单词的尾巴和后一个单词的头相同就算接上了
* 如果要返回接龙的结果,可以给HashSet改成List
diff --git a/solution/src/main/java/com/inuker/solution/CloneGraph.java b/leetcode/solution/src/CloneGraph.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/CloneGraph.java
rename to leetcode/solution/src/CloneGraph.java
index bb03431..087d010 100644
--- a/solution/src/main/java/com/inuker/solution/CloneGraph.java
+++ b/leetcode/solution/src/CloneGraph.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.UndirectedGraphNode;
-
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
-/**
- * Created by dingjikerbo on 2016/12/21.
- */
-
public class CloneGraph {
/**
diff --git a/solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValue.java b/leetcode/solution/src/ClosestBinarySearchTreeValue.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValue.java
rename to leetcode/solution/src/ClosestBinarySearchTreeValue.java
index 4335590..3a2adf6 100644
--- a/solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValue.java
+++ b/leetcode/solution/src/ClosestBinarySearchTreeValue.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 2016/12/2.
- */
-
public class ClosestBinarySearchTreeValue {
public int closestValue(TreeNode root, double target) {
diff --git a/solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValueII.java b/leetcode/solution/src/ClosestBinarySearchTreeValueII.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValueII.java
rename to leetcode/solution/src/ClosestBinarySearchTreeValueII.java
index 2c9c360..41edbf3 100644
--- a/solution/src/main/java/com/inuker/solution/ClosestBinarySearchTreeValueII.java
+++ b/leetcode/solution/src/ClosestBinarySearchTreeValueII.java
@@ -1,16 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
-/**
- * Created by dingjikerbo on 2016/12/2.
- */
-
/**
* 有三种解法:
* 2,中序遍历两遍二叉树,不过每次都会中途退出,构成双栈,一个是大于target,一个是小于target,然后依次出栈满k为止,复杂度O(n + k)
diff --git a/solution/src/main/java/com/inuker/solution/Codec.java b/leetcode/solution/src/Codec.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/Codec.java
rename to leetcode/solution/src/Codec.java
index d1d7d90..588c86e 100644
--- a/solution/src/main/java/com/inuker/solution/Codec.java
+++ b/leetcode/solution/src/Codec.java
@@ -1,16 +1,4 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-import java.util.Arrays;
-import java.util.Deque;
-import java.util.LinkedList;
-import java.util.Queue;
-import java.util.Stack;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
+import java.util.*;
/**
* 类似题
diff --git a/leetcode/solution/src/CoinChange.java b/leetcode/solution/src/CoinChange.java
new file mode 100644
index 0000000..66423ce
--- /dev/null
+++ b/leetcode/solution/src/CoinChange.java
@@ -0,0 +1,20 @@
+import java.util.Arrays;
+
+public class CoinChange {
+
+ public int coinChange(int[] coins, int amount) {
+ Arrays.sort(coins);
+ int[] dp = new int[amount + 1];
+ Arrays.fill(dp, -1);
+ dp[0] = 0;
+ for (int i = 1; i <= amount; i++) {
+ for (int coin: coins) {
+ if (i - coin >= 0 && dp[i - coin] >= 0) {
+ int k = dp[i - coin] + 1;
+ dp[i] = dp[i] > 0 ? Math.min(dp[i], k) : k;
+ }
+ }
+ }
+ return dp[amount];
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/CombinationSum.java b/leetcode/solution/src/CombinationSum.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/CombinationSum.java
rename to leetcode/solution/src/CombinationSum.java
index e5e881d..824a65a 100644
--- a/solution/src/main/java/com/inuker/solution/CombinationSum.java
+++ b/leetcode/solution/src/CombinationSum.java
@@ -1,13 +1,6 @@
-package com.inuker.solution;
-
-import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class CombinationSum {
// 耗时23ms
diff --git a/solution/src/main/java/com/inuker/solution/CombinationSumII.java b/leetcode/solution/src/CombinationSumII.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/CombinationSumII.java
rename to leetcode/solution/src/CombinationSumII.java
index eb22a9e..2ab9acd 100644
--- a/solution/src/main/java/com/inuker/solution/CombinationSumII.java
+++ b/leetcode/solution/src/CombinationSumII.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class CombinationSumII {
// 耗时29ms
diff --git a/solution/src/main/java/com/inuker/solution/CombinationSumIII.java b/leetcode/solution/src/CombinationSumIII.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/CombinationSumIII.java
rename to leetcode/solution/src/CombinationSumIII.java
index 4e4eccc..c64c203 100644
--- a/solution/src/main/java/com/inuker/solution/CombinationSumIII.java
+++ b/leetcode/solution/src/CombinationSumIII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
/**
* 这题不允许重复数字
*/
diff --git a/solution/src/main/java/com/inuker/solution/CombinationSumIV.java b/leetcode/solution/src/CombinationSumIV.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/CombinationSumIV.java
rename to leetcode/solution/src/CombinationSumIV.java
index 8f45652..3920298 100644
--- a/solution/src/main/java/com/inuker/solution/CombinationSumIV.java
+++ b/leetcode/solution/src/CombinationSumIV.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Arrays;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class CombinationSumIV {
/**
diff --git a/solution/src/main/java/com/inuker/solution/Combinations.java b/leetcode/solution/src/Combinations.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/Combinations.java
rename to leetcode/solution/src/Combinations.java
index f2673a0..f4091d3 100644
--- a/solution/src/main/java/com/inuker/solution/Combinations.java
+++ b/leetcode/solution/src/Combinations.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class Combinations {
public List> combine(int n, int k) {
diff --git a/solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromInorderAndPostorderTraversal.java b/leetcode/solution/src/ConstructBinaryTreeFromInorderAndPostorderTraversal.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromInorderAndPostorderTraversal.java
rename to leetcode/solution/src/ConstructBinaryTreeFromInorderAndPostorderTraversal.java
index f841314..7610466 100644
--- a/solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromInorderAndPostorderTraversal.java
+++ b/leetcode/solution/src/ConstructBinaryTreeFromInorderAndPostorderTraversal.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 2016/12/2.
- */
-
public class ConstructBinaryTreeFromInorderAndPostorderTraversal {
public TreeNode buildTree(int[] inorder, int[] postorder) {
diff --git a/solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromPreorderInorderTraversal.java b/leetcode/solution/src/ConstructBinaryTreeFromPreorderInorderTraversal.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromPreorderInorderTraversal.java
rename to leetcode/solution/src/ConstructBinaryTreeFromPreorderInorderTraversal.java
index d3fb909..58477f7 100644
--- a/solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromPreorderInorderTraversal.java
+++ b/leetcode/solution/src/ConstructBinaryTreeFromPreorderInorderTraversal.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/11/30.
- */
-
public class ConstructBinaryTreeFromPreorderInorderTraversal {
public TreeNode buildTree(int[] preorder, int[] inorder) {
diff --git a/leetcode/solution/src/ConstructStringFromBinaryTree.java b/leetcode/solution/src/ConstructStringFromBinaryTree.java
new file mode 100644
index 0000000..96747ce
--- /dev/null
+++ b/leetcode/solution/src/ConstructStringFromBinaryTree.java
@@ -0,0 +1,21 @@
+/**
+ * https://leetcode.com/articles/construct-string-from-binary-tree/
+ */
+public class ConstructStringFromBinaryTree {
+
+ /**
+ * 左子树如果为空'()'是不能省略的
+ * 右子树如果为空可以省略
+ */
+ public String tree2str(TreeNode t) {
+ if (t == null) {
+ return "";
+ }
+ if (t.left == null && t.right == null) {
+ return t.val + "";
+ }
+ String left = "(" + tree2str(t.left) + ")";
+ String right = t.right != null ? "(" + tree2str(t.right) + ")" : "";
+ return t.val + left + right;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/ContainerWithMostWater.java b/leetcode/solution/src/ContainerWithMostWater.java
similarity index 83%
rename from solution/src/main/java/com/inuker/solution/ContainerWithMostWater.java
rename to leetcode/solution/src/ContainerWithMostWater.java
index dce553d..75232b4 100644
--- a/solution/src/main/java/com/inuker/solution/ContainerWithMostWater.java
+++ b/leetcode/solution/src/ContainerWithMostWater.java
@@ -1,14 +1,9 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/5/2.
- */
-
/**
* 题目的意思是选定两块板子组成一个桶,使得装水量最大。注意其余的板子都忽略。
* 一种O(n)的方法是对于区间[left, right],假如height[left] < height[right],则我们可以认定[left, right - 1],[left, right - 2] ...
* 都不会比[left, right]装的水更多。原因是木桶的短板没变,所以水平面不会变,但是宽度减小了。
* 所以我们要继续尝试的方向是从较短的一方推进,寄希望于其之后的板子能高一点
+ * 如果两边一样高,那随便推进哪一边都行,因为假如之后还能装更多水,一定不会包含这两边任意一块。如果包含了,水量不会超过当前。
*/
public class ContainerWithMostWater {
diff --git a/solution/src/main/java/com/inuker/solution/ContainsDuplicate.java b/leetcode/solution/src/ContainsDuplicate.java
similarity index 76%
rename from solution/src/main/java/com/inuker/solution/ContainsDuplicate.java
rename to leetcode/solution/src/ContainsDuplicate.java
index c642c0c..3a56afa 100644
--- a/solution/src/main/java/com/inuker/solution/ContainsDuplicate.java
+++ b/leetcode/solution/src/ContainsDuplicate.java
@@ -1,13 +1,5 @@
-package com.inuker.solution;
-
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
-import java.util.TreeSet;
-
-/**
- * Created by dingjikerbo on 16/12/8.
- */
/**
* https://leetcode.com/articles/contains-duplicate/
diff --git a/solution/src/main/java/com/inuker/solution/ContainsDuplicateII.java b/leetcode/solution/src/ContainsDuplicateII.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/ContainsDuplicateII.java
rename to leetcode/solution/src/ContainsDuplicateII.java
index a2225ef..0e0ac92 100644
--- a/solution/src/main/java/com/inuker/solution/ContainsDuplicateII.java
+++ b/leetcode/solution/src/ContainsDuplicateII.java
@@ -1,12 +1,5 @@
-package com.inuker.solution;
-
import java.util.HashMap;
import java.util.HashSet;
-import java.util.Set;
-
-/**
- * Created by dingjikerbo on 16/12/8.
- */
/**
* https://leetcode.com/articles/contains-duplicate-ii/
diff --git a/solution/src/main/java/com/inuker/solution/ContainsDuplicateIII.java b/leetcode/solution/src/ContainsDuplicateIII.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/ContainsDuplicateIII.java
rename to leetcode/solution/src/ContainsDuplicateIII.java
index e7ca4b8..ca6dc6f 100644
--- a/solution/src/main/java/com/inuker/solution/ContainsDuplicateIII.java
+++ b/leetcode/solution/src/ContainsDuplicateIII.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
-/**
- * Created by dingjikerbo on 16/12/8.
- */
-
/**
* https://leetcode.com/articles/contains-duplicate-iii/
*/
diff --git a/leetcode/solution/src/ConvertBSTToGreaterTree.java b/leetcode/solution/src/ConvertBSTToGreaterTree.java
new file mode 100644
index 0000000..f9fe841
--- /dev/null
+++ b/leetcode/solution/src/ConvertBSTToGreaterTree.java
@@ -0,0 +1,26 @@
+import java.util.Stack;
+
+/**
+ * https://leetcode.com/articles/convert-bst-to-greater-tree/
+ */
+public class ConvertBSTToGreaterTree {
+
+ public TreeNode convertBST(TreeNode root) {
+ Stack stack = new Stack<>();
+ int sum = 0;
+ TreeNode node = root;
+ while (!stack.isEmpty() || node != null) {
+ if (node != null) {
+ stack.push(node);
+ node = node.right;
+ } else {
+ node = stack.pop();
+ int s = sum;
+ sum += node.val;
+ node.val += s;
+ node = node.left;
+ }
+ }
+ return root;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/ConvertSortedArrayToBinarySearchTree.java b/leetcode/solution/src/ConvertSortedArrayToBinarySearchTree.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/ConvertSortedArrayToBinarySearchTree.java
rename to leetcode/solution/src/ConvertSortedArrayToBinarySearchTree.java
index 6e9d784..a81ae81 100644
--- a/solution/src/main/java/com/inuker/solution/ConvertSortedArrayToBinarySearchTree.java
+++ b/leetcode/solution/src/ConvertSortedArrayToBinarySearchTree.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/11/28.
- */
-
public class ConvertSortedArrayToBinarySearchTree {
public TreeNode sortedArrayToBST(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/ConvertSortedListToBinarySearchTree.java b/leetcode/solution/src/ConvertSortedListToBinarySearchTree.java
similarity index 80%
rename from solution/src/main/java/com/inuker/solution/ConvertSortedListToBinarySearchTree.java
rename to leetcode/solution/src/ConvertSortedListToBinarySearchTree.java
index eea3e9d..6723b13 100644
--- a/solution/src/main/java/com/inuker/solution/ConvertSortedListToBinarySearchTree.java
+++ b/leetcode/solution/src/ConvertSortedListToBinarySearchTree.java
@@ -1,12 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by liwentian on 2017/9/11.
- */
-
public class ConvertSortedListToBinarySearchTree {
public TreeNode sortedListToBST(ListNode head) {
diff --git a/leetcode/solution/src/CopyListWithRandomPointer.java b/leetcode/solution/src/CopyListWithRandomPointer.java
new file mode 100644
index 0000000..19c0f75
--- /dev/null
+++ b/leetcode/solution/src/CopyListWithRandomPointer.java
@@ -0,0 +1,32 @@
+/**
+ * 易错的地方在于random要判空
+ */
+public class CopyListWithRandomPointer {
+
+
+ public RandomListNode copyRandomList(RandomListNode head) {
+ for (RandomListNode p = head; p != null; ) {
+ RandomListNode next = p.next;
+ RandomListNode temp = new RandomListNode(p.label);
+ temp.next = p.next;
+ p.next = temp;
+ p = next;
+ }
+ for (RandomListNode p = head; p != null; ) {
+ if (p.random != null) {
+ p.next.random = p.random.next;
+ }
+ p = p.next.next;
+ }
+ RandomListNode dummy = new RandomListNode(0), cur = dummy;
+ for (RandomListNode p = head; p != null; ) {
+ cur.next = p.next;
+ cur = cur.next;
+ p.next = p.next.next;
+ p = p.next;
+ }
+ return dummy.next;
+ }
+}
+
+
diff --git a/solution/src/main/java/com/inuker/solution/CountAndSay.java b/leetcode/solution/src/CountAndSay.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/CountAndSay.java
rename to leetcode/solution/src/CountAndSay.java
index 2e9c9af..ec3f9b2 100644
--- a/solution/src/main/java/com/inuker/solution/CountAndSay.java
+++ b/leetcode/solution/src/CountAndSay.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class CountAndSay {
public String countAndSay(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/CountCompleteTreeNodes.java b/leetcode/solution/src/CountCompleteTreeNodes.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/CountCompleteTreeNodes.java
rename to leetcode/solution/src/CountCompleteTreeNodes.java
index 5650df6..0dfa900 100644
--- a/solution/src/main/java/com/inuker/solution/CountCompleteTreeNodes.java
+++ b/leetcode/solution/src/CountCompleteTreeNodes.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 2016/11/29.
- */
-
public class CountCompleteTreeNodes {
/** 适合通用的二叉树,但是对于完全二叉树会超时
@@ -34,7 +26,7 @@ public int countNodes(TreeNode root) {
return count;
}*/
- // 119ms,最差复杂度是O(n),最好是O(lgn)
+ // 119ms,最差复杂度是O((lgn)^2),最好是O(lgn)
public int countNodes(TreeNode root) {
int left = 0, right = 0;
for (TreeNode node = root; node != null; node = node.left, left++);
diff --git a/solution/src/main/java/com/inuker/solution/CountNumberWithUniqueDigits.java b/leetcode/solution/src/CountNumberWithUniqueDigits.java
similarity index 80%
rename from solution/src/main/java/com/inuker/solution/CountNumberWithUniqueDigits.java
rename to leetcode/solution/src/CountNumberWithUniqueDigits.java
index 4fab953..50283e3 100644
--- a/solution/src/main/java/com/inuker/solution/CountNumberWithUniqueDigits.java
+++ b/leetcode/solution/src/CountNumberWithUniqueDigits.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2016/12/29.
- */
-
public class CountNumberWithUniqueDigits {
public int countNumbersWithUniqueDigits(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/CountOfSmallerNumbersAfterSelf.java b/leetcode/solution/src/CountOfSmallerNumbersAfterSelf.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/CountOfSmallerNumbersAfterSelf.java
rename to leetcode/solution/src/CountOfSmallerNumbersAfterSelf.java
index 4fab12b..4aa9c80 100644
--- a/solution/src/main/java/com/inuker/solution/CountOfSmallerNumbersAfterSelf.java
+++ b/leetcode/solution/src/CountOfSmallerNumbersAfterSelf.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/21.
- */
-
/**
* 这题理解了好久才搞明白
* 建立一棵二叉树,从数组末尾开始遍历,这里每个树的节点要记录sum和dup
diff --git a/solution/src/main/java/com/inuker/solution/CountPrimes.java b/leetcode/solution/src/CountPrimes.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/CountPrimes.java
rename to leetcode/solution/src/CountPrimes.java
index d14a019..e0ddfd0 100644
--- a/solution/src/main/java/com/inuker/solution/CountPrimes.java
+++ b/leetcode/solution/src/CountPrimes.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import java.util.Arrays;
-
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
/**
* 如果一个数是另一个数的倍数,那这个数肯定不是素数。
* 利用这个性质,我们可以建立一个素数数组,从2开始将素数的倍数都标注为不是素数。
diff --git a/solution/src/main/java/com/inuker/solution/CountUnivalueSubtrees.java b/leetcode/solution/src/CountUnivalueSubtrees.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/CountUnivalueSubtrees.java
rename to leetcode/solution/src/CountUnivalueSubtrees.java
index c7933ff..b0071b2 100644
--- a/solution/src/main/java/com/inuker/solution/CountUnivalueSubtrees.java
+++ b/leetcode/solution/src/CountUnivalueSubtrees.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/12/1.
- */
-
public class CountUnivalueSubtrees {
public int countUnivalSubtrees(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/CourseSchedule.java b/leetcode/solution/src/CourseSchedule.java
similarity index 81%
rename from solution/src/main/java/com/inuker/solution/CourseSchedule.java
rename to leetcode/solution/src/CourseSchedule.java
index 21bd7da..12a928d 100644
--- a/solution/src/main/java/com/inuker/solution/CourseSchedule.java
+++ b/leetcode/solution/src/CourseSchedule.java
@@ -1,15 +1,4 @@
-package com.inuker.solution;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Queue;
-import java.util.Set;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
+import java.util.*;
public class CourseSchedule {
diff --git a/solution/src/main/java/com/inuker/solution/CourseScheduleII.java b/leetcode/solution/src/CourseScheduleII.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/CourseScheduleII.java
rename to leetcode/solution/src/CourseScheduleII.java
index 203bc7e..95fc851 100644
--- a/solution/src/main/java/com/inuker/solution/CourseScheduleII.java
+++ b/leetcode/solution/src/CourseScheduleII.java
@@ -1,15 +1,8 @@
-package com.inuker.solution;
-
-import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class CourseScheduleII {
public int[] findOrder(int numCourses, int[][] prerequisites) {
diff --git a/solution/src/main/java/com/inuker/solution/DecodeString.java b/leetcode/solution/src/DecodeString.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/DecodeString.java
rename to leetcode/solution/src/DecodeString.java
index a316137..48315d9 100644
--- a/solution/src/main/java/com/inuker/solution/DecodeString.java
+++ b/leetcode/solution/src/DecodeString.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import java.util.Stack;
-
-/**
- * Created by dingjikerbo on 2016/12/11.
- */
-
public class DecodeString {
/**
diff --git a/leetcode/solution/src/DecodeWays.java b/leetcode/solution/src/DecodeWays.java
new file mode 100644
index 0000000..f204f7f
--- /dev/null
+++ b/leetcode/solution/src/DecodeWays.java
@@ -0,0 +1,24 @@
+/**
+ * TestCases
+ * ""
+ * "1"
+ * "1787897759966261825913315262377298132516969578441236833255596967132573482281598412163216914566534565"
+ * "7893749912342187894921836847319981199844151766195952528631828655978178193192959793156142441128167383"
+ */
+public class DecodeWays {
+
+ // DP,耗时1ms
+ public int numDecodings(String s) {
+ int len = s.length();
+ int[] dp = new int[len];
+ for (int i = 0; i < len; i++) {
+ if (s.charAt(i) != '0') {
+ dp[i] = i > 0 ? dp[i - 1] : 1;
+ }
+ if (i > 0 && (s.charAt(i - 1) == '1' || (s.charAt(i - 1) == '2' && s.charAt(i) <= '6'))) {
+ dp[i] += i > 1 ? dp[i - 2] : 1;
+ }
+ }
+ return dp[len - 1];
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/DeleteNodeInALinkedList.java b/leetcode/solution/src/DeleteNodeInALinkedList.java
similarity index 66%
rename from solution/src/main/java/com/inuker/solution/DeleteNodeInALinkedList.java
rename to leetcode/solution/src/DeleteNodeInALinkedList.java
index 9648546..d7d9aa0 100644
--- a/solution/src/main/java/com/inuker/solution/DeleteNodeInALinkedList.java
+++ b/leetcode/solution/src/DeleteNodeInALinkedList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/11.
- */
-
-import com.leetcode.library.ListNode;
-
/**
* https://leetcode.com/articles/delete-node-linked-list/
*/
diff --git a/solution/src/main/java/com/inuker/solution/DeleteNodeInBST.java b/leetcode/solution/src/DeleteNodeInBST.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/DeleteNodeInBST.java
rename to leetcode/solution/src/DeleteNodeInBST.java
index f40bd3a..b33cc58 100644
--- a/solution/src/main/java/com/inuker/solution/DeleteNodeInBST.java
+++ b/leetcode/solution/src/DeleteNodeInBST.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 17/1/2.
- */
-
public class DeleteNodeInBST {
public TreeNode deleteNode(TreeNode root, int key) {
diff --git a/leetcode/solution/src/DiameterOfBinaryTree.java b/leetcode/solution/src/DiameterOfBinaryTree.java
new file mode 100644
index 0000000..ca696c4
--- /dev/null
+++ b/leetcode/solution/src/DiameterOfBinaryTree.java
@@ -0,0 +1,24 @@
+public class DiameterOfBinaryTree {
+
+ /**
+ * 这题和 124. Binary Tree Maximum Path Sum比较像
+ * 都是分两种情况,一种是必须包括root的,带上必须包括left和right的单边结果;
+ * 还有不是必须包括root的,对比left和right的双边结果
+ * flag为true表示必须包括root,false表示不是必须包括root
+ */
+ public int diameterOfBinaryTree(TreeNode root) {
+ if (root == null) {
+ return 0;
+ }
+ return dfs(root, false);
+ }
+
+ public int dfs(TreeNode root, boolean flag) {
+ if (root == null) {
+ return -1;
+ }
+ return flag ? Math.max(dfs(root.left, true) + 1, dfs(root.right, true) + 1) :
+ Math.max(Math.max(dfs(root.left, false), dfs(root.right, false)),
+ dfs(root.left, true) + 2 + dfs(root.right, true));
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/DifferenceWaysToAddParentheses.java b/leetcode/solution/src/DifferenceWaysToAddParentheses.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/DifferenceWaysToAddParentheses.java
rename to leetcode/solution/src/DifferenceWaysToAddParentheses.java
index 4ae23c5..a176668 100644
--- a/solution/src/main/java/com/inuker/solution/DifferenceWaysToAddParentheses.java
+++ b/leetcode/solution/src/DifferenceWaysToAddParentheses.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class DifferenceWaysToAddParentheses {
public List diffWaysToCompute(String input) {
diff --git a/solution/src/main/java/com/inuker/solution/DivideTwoIntegers.java b/leetcode/solution/src/DivideTwoIntegers.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/DivideTwoIntegers.java
rename to leetcode/solution/src/DivideTwoIntegers.java
index fa64bc0..855a504 100644
--- a/solution/src/main/java/com/inuker/solution/DivideTwoIntegers.java
+++ b/leetcode/solution/src/DivideTwoIntegers.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/1/12.
- */
-
public class DivideTwoIntegers {
/**
diff --git a/solution/src/main/java/com/inuker/solution/EditDistance.java b/leetcode/solution/src/EditDistance.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/EditDistance.java
rename to leetcode/solution/src/EditDistance.java
index 6aec8e1..95d8798 100644
--- a/solution/src/main/java/com/inuker/solution/EditDistance.java
+++ b/leetcode/solution/src/EditDistance.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/15.
- */
-
/**
* 这题非常重要的是对f的初始化,千万不能漏
* 所有DP问题都要注意初始化边界条件
diff --git a/solution/src/main/java/com/inuker/solution/EncodeAndDecodeStrings.java b/leetcode/solution/src/EncodeAndDecodeStrings.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/EncodeAndDecodeStrings.java
rename to leetcode/solution/src/EncodeAndDecodeStrings.java
index 9b88a71..f5b1bd2 100644
--- a/solution/src/main/java/com/inuker/solution/EncodeAndDecodeStrings.java
+++ b/leetcode/solution/src/EncodeAndDecodeStrings.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/11.
- */
-
public class EncodeAndDecodeStrings {
/**
diff --git a/solution/src/main/java/com/inuker/solution/EncodeAndDecodeTinyURL.java b/leetcode/solution/src/EncodeAndDecodeTinyURL.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/EncodeAndDecodeTinyURL.java
rename to leetcode/solution/src/EncodeAndDecodeTinyURL.java
index 64e5d99..386e4fa 100644
--- a/solution/src/main/java/com/inuker/solution/EncodeAndDecodeTinyURL.java
+++ b/leetcode/solution/src/EncodeAndDecodeTinyURL.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.HashMap;
import java.util.Random;
-/**
- * Created by dingjikerbo on 2017/8/20.
- */
-
/**
* https://leetcode.com/articles/encode-and-decode-tinyurl/
*/
diff --git a/leetcode/solution/src/EvaluateDivision.java b/leetcode/solution/src/EvaluateDivision.java
new file mode 100644
index 0000000..674b27b
--- /dev/null
+++ b/leetcode/solution/src/EvaluateDivision.java
@@ -0,0 +1,67 @@
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+
+/**
+ * 1, 首先给除的结果都存起来,建立有向图
+ * 2,用DFS遍历路径
+ */
+public class EvaluateDivision {
+
+ public double[] calcEquation(String[][] equations, double[] values, String[][] queries) {
+ HashMap> table = new HashMap<>();
+ for (int i = 0; i < equations.length; i++) {
+ add(table, equations[i], values[i]);
+ }
+ double[] result = new double[queries.length];
+ HashSet visited = new HashSet<>();
+ for (int i = 0; i < queries.length; i++) {
+ visited.clear();
+ result[i] = calc(table, visited, queries[i][0], queries[i][1]);
+ }
+ return result;
+ }
+
+ private double calc(HashMap> table, HashSet visited, String a, String b) {
+ HashMap map = table.get(a);
+ if (map == null) {
+ return -1.0;
+ }
+ if (a.equals(b)) {
+ return 1.0;
+ }
+ for (Map.Entry entry : map.entrySet()) {
+ String key = entry.getKey();
+ Double value = entry.getValue();
+
+ if (visited.contains(key)) {
+ continue;
+ }
+
+ visited.add(key);
+
+ double t = calc(table, visited, key, b);
+ if (t != -1.0) {
+ return value * t;
+ }
+
+ visited.remove(key);
+ }
+ return -1.0;
+ }
+
+ private void add(HashMap> table, String[] equation, double value) {
+ HashMap map0 = table.get(equation[0]);
+ HashMap map1 = table.get(equation[1]);
+ if (map0 == null) {
+ map0 = new HashMap<>();
+ table.put(equation[0], map0);
+ }
+ if (map1 == null) {
+ map1 = new HashMap<>();
+ table.put(equation[1], map1);
+ }
+ map0.put(equation[1], value);
+ map1.put(equation[0], 1 / value);
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/EvaluateReversePolishNotation.java b/leetcode/solution/src/EvaluateReversePolishNotation.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/EvaluateReversePolishNotation.java
rename to leetcode/solution/src/EvaluateReversePolishNotation.java
index bd85961..0099e69 100644
--- a/solution/src/main/java/com/inuker/solution/EvaluateReversePolishNotation.java
+++ b/leetcode/solution/src/EvaluateReversePolishNotation.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Stack;
-/**
- * Created by liwentian on 2017/9/12.
- */
-
public class EvaluateReversePolishNotation {
public int evalRPN(String[] tokens) {
diff --git a/solution/src/main/java/com/inuker/solution/ExcelSheetColumnNumber.java b/leetcode/solution/src/ExcelSheetColumnNumber.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/ExcelSheetColumnNumber.java
rename to leetcode/solution/src/ExcelSheetColumnNumber.java
index 26fb5cd..4d8a79f 100644
--- a/solution/src/main/java/com/inuker/solution/ExcelSheetColumnNumber.java
+++ b/leetcode/solution/src/ExcelSheetColumnNumber.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class ExcelSheetColumnNumber {
public List binaryTreePaths(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/ExcelSheetColumnTitle.java b/leetcode/solution/src/ExcelSheetColumnTitle.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/ExcelSheetColumnTitle.java
rename to leetcode/solution/src/ExcelSheetColumnTitle.java
index a5ea8eb..88fcd30 100644
--- a/solution/src/main/java/com/inuker/solution/ExcelSheetColumnTitle.java
+++ b/leetcode/solution/src/ExcelSheetColumnTitle.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class ExcelSheetColumnTitle {
/**
diff --git a/solution/src/main/java/com/inuker/solution/ExpressionAddOperators.java b/leetcode/solution/src/ExpressionAddOperators.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/ExpressionAddOperators.java
rename to leetcode/solution/src/ExpressionAddOperators.java
index 58e79c1..da1008c 100644
--- a/solution/src/main/java/com/inuker/solution/ExpressionAddOperators.java
+++ b/leetcode/solution/src/ExpressionAddOperators.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/21.
- */
-
public class ExpressionAddOperators {
/**
diff --git a/solution/src/main/java/com/inuker/solution/FactorCombinations.java b/leetcode/solution/src/FactorCombinations.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/FactorCombinations.java
rename to leetcode/solution/src/FactorCombinations.java
index c6ac34c..b16a400 100644
--- a/solution/src/main/java/com/inuker/solution/FactorCombinations.java
+++ b/leetcode/solution/src/FactorCombinations.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/6.
- */
-
public class FactorCombinations {
public List> getFactors(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/FactorialTrailingZeroes.java b/leetcode/solution/src/FactorialTrailingZeroes.java
similarity index 69%
rename from solution/src/main/java/com/inuker/solution/FactorialTrailingZeroes.java
rename to leetcode/solution/src/FactorialTrailingZeroes.java
index d0beb44..a816916 100644
--- a/solution/src/main/java/com/inuker/solution/FactorialTrailingZeroes.java
+++ b/leetcode/solution/src/FactorialTrailingZeroes.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/7.
- */
-
public class FactorialTrailingZeroes {
public int trailingZeroes(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/FindAllAnagramsInString.java b/leetcode/solution/src/FindAllAnagramsInString.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/FindAllAnagramsInString.java
rename to leetcode/solution/src/FindAllAnagramsInString.java
index 63c7aa4..98a9d5b 100644
--- a/solution/src/main/java/com/inuker/solution/FindAllAnagramsInString.java
+++ b/leetcode/solution/src/FindAllAnagramsInString.java
@@ -1,14 +1,7 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class FindAllAnagramsInString {
// 耗时16ms,复杂度O(n)
diff --git a/solution/src/main/java/com/inuker/solution/FindAllDuplicatesInAnArray.java b/leetcode/solution/src/FindAllDuplicatesInAnArray.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/FindAllDuplicatesInAnArray.java
rename to leetcode/solution/src/FindAllDuplicatesInAnArray.java
index 85961b4..5233bf3 100644
--- a/solution/src/main/java/com/inuker/solution/FindAllDuplicatesInAnArray.java
+++ b/leetcode/solution/src/FindAllDuplicatesInAnArray.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class FindAllDuplicatesInAnArray {
public List findDuplicates(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/FindAllNumbersDisappearedInAnArray.java b/leetcode/solution/src/FindAllNumbersDisappearedInAnArray.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/FindAllNumbersDisappearedInAnArray.java
rename to leetcode/solution/src/FindAllNumbersDisappearedInAnArray.java
index 0269e8d..159043c 100644
--- a/solution/src/main/java/com/inuker/solution/FindAllNumbersDisappearedInAnArray.java
+++ b/leetcode/solution/src/FindAllNumbersDisappearedInAnArray.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class FindAllNumbersDisappearedInAnArray {
//4,3,2,7,8,2,3,1
diff --git a/solution/src/main/java/com/inuker/solution/FindBottomLeftTreeValue.java b/leetcode/solution/src/FindBottomLeftTreeValue.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/FindBottomLeftTreeValue.java
rename to leetcode/solution/src/FindBottomLeftTreeValue.java
index dc12267..a177a75 100644
--- a/solution/src/main/java/com/inuker/solution/FindBottomLeftTreeValue.java
+++ b/leetcode/solution/src/FindBottomLeftTreeValue.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.Queue;
-/**
- * Created by liwentian on 17/8/3.
- */
-
public class FindBottomLeftTreeValue {
public int findBottomLeftValue(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/FindCelebrity.java b/leetcode/solution/src/FindCelebrity.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/FindCelebrity.java
rename to leetcode/solution/src/FindCelebrity.java
index 18ebcf7..09e1a45 100644
--- a/solution/src/main/java/com/inuker/solution/FindCelebrity.java
+++ b/leetcode/solution/src/FindCelebrity.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
/**
* 先用排除法扫一轮,剩下的是唯一的候选人,然后再严格判断
*/
diff --git a/solution/src/main/java/com/inuker/solution/FindDuplicateSubtrees.java b/leetcode/solution/src/FindDuplicateSubtrees.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/FindDuplicateSubtrees.java
rename to leetcode/solution/src/FindDuplicateSubtrees.java
index e353480..7036b91 100644
--- a/solution/src/main/java/com/inuker/solution/FindDuplicateSubtrees.java
+++ b/leetcode/solution/src/FindDuplicateSubtrees.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 17/8/3.
- */
-
public class FindDuplicateSubtrees {
/**
diff --git a/solution/src/main/java/com/inuker/solution/FindKPairsWithSmallestSums.java b/leetcode/solution/src/FindKPairsWithSmallestSums.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/FindKPairsWithSmallestSums.java
rename to leetcode/solution/src/FindKPairsWithSmallestSums.java
index dad9523..0513bd1 100644
--- a/solution/src/main/java/com/inuker/solution/FindKPairsWithSmallestSums.java
+++ b/leetcode/solution/src/FindKPairsWithSmallestSums.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
-/**
- * Created by liwentian on 2017/9/22.
- */
-
public class FindKPairsWithSmallestSums {
public List kSmallestPairs(int[] nums1, int[] nums2, int k) {
diff --git a/solution/src/main/java/com/inuker/solution/FindLargestValueInEachTreeRow.java b/leetcode/solution/src/FindLargestValueInEachTreeRow.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/FindLargestValueInEachTreeRow.java
rename to leetcode/solution/src/FindLargestValueInEachTreeRow.java
index 4fd1361..2c2bd6f 100644
--- a/solution/src/main/java/com/inuker/solution/FindLargestValueInEachTreeRow.java
+++ b/leetcode/solution/src/FindLargestValueInEachTreeRow.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
-/**
- * Created by liwentian on 17/7/25.
- */
-
public class FindLargestValueInEachTreeRow {
public List largestValues(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/FindLeavesOfBinaryTree.java b/leetcode/solution/src/FindLeavesOfBinaryTree.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/FindLeavesOfBinaryTree.java
rename to leetcode/solution/src/FindLeavesOfBinaryTree.java
index b14a9df..d0fc777 100644
--- a/solution/src/main/java/com/inuker/solution/FindLeavesOfBinaryTree.java
+++ b/leetcode/solution/src/FindLeavesOfBinaryTree.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/3.
- */
-
public class FindLeavesOfBinaryTree {
public List> findLeaves(TreeNode root) {
diff --git a/solution/src/main/java/com/inuker/solution/FindMedianFromDataStream.java b/leetcode/solution/src/FindMedianFromDataStream.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/FindMedianFromDataStream.java
rename to leetcode/solution/src/FindMedianFromDataStream.java
index 75b5ec5..9169ed3 100644
--- a/solution/src/main/java/com/inuker/solution/FindMedianFromDataStream.java
+++ b/leetcode/solution/src/FindMedianFromDataStream.java
@@ -1,13 +1,6 @@
-package com.inuker.solution;
-
-import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
-/**
- * Created by dingjikerbo on 17/1/2.
- */
-
public class FindMedianFromDataStream {
/**
diff --git a/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArray.java b/leetcode/solution/src/FindMinimumInRotatedSortedArray.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArray.java
rename to leetcode/solution/src/FindMinimumInRotatedSortedArray.java
index 98145ac..ed1c5aa 100644
--- a/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArray.java
+++ b/leetcode/solution/src/FindMinimumInRotatedSortedArray.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class FindMinimumInRotatedSortedArray {
public int findMin(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArrayII.java b/leetcode/solution/src/FindMinimumInRotatedSortedArrayII.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArrayII.java
rename to leetcode/solution/src/FindMinimumInRotatedSortedArrayII.java
index 7944bfe..36b1176 100644
--- a/solution/src/main/java/com/inuker/solution/FindMinimumInRotatedSortedArrayII.java
+++ b/leetcode/solution/src/FindMinimumInRotatedSortedArrayII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class FindMinimumInRotatedSortedArrayII {
public int findMin(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/FindModeInBinarySearchTree.java b/leetcode/solution/src/FindModeInBinarySearchTree.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/FindModeInBinarySearchTree.java
rename to leetcode/solution/src/FindModeInBinarySearchTree.java
index 9cca839..be9319f 100644
--- a/solution/src/main/java/com/inuker/solution/FindModeInBinarySearchTree.java
+++ b/leetcode/solution/src/FindModeInBinarySearchTree.java
@@ -1,15 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 17/8/3.
- */
-
/**
* 这题是要找BST中出现次数最多的节点集合,这里允许有重复节点
* 思路很简单,中序遍历,会按升序排列,再统计重复的值
diff --git a/solution/src/main/java/com/inuker/solution/FindPeakElement.java b/leetcode/solution/src/FindPeakElement.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/FindPeakElement.java
rename to leetcode/solution/src/FindPeakElement.java
index c868529..8b78217 100644
--- a/solution/src/main/java/com/inuker/solution/FindPeakElement.java
+++ b/leetcode/solution/src/FindPeakElement.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/10.
- */
-
/**
* https://leetcode.com/articles/find-peak-element/
*/
diff --git a/solution/src/main/java/com/inuker/solution/FindRightInterval.java b/leetcode/solution/src/FindRightInterval.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/FindRightInterval.java
rename to leetcode/solution/src/FindRightInterval.java
index 8f2ee9a..bc914df 100644
--- a/solution/src/main/java/com/inuker/solution/FindRightInterval.java
+++ b/leetcode/solution/src/FindRightInterval.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.Interval;
-
import java.util.Map;
import java.util.TreeMap;
-/**
- * Created by liwentian on 2017/9/22.
- */
-
public class FindRightInterval {
public int[] findRightInterval(Interval[] intervals) {
diff --git a/leetcode/solution/src/FindTheClosestPalindrome.java b/leetcode/solution/src/FindTheClosestPalindrome.java
new file mode 100644
index 0000000..dc3156f
--- /dev/null
+++ b/leetcode/solution/src/FindTheClosestPalindrome.java
@@ -0,0 +1,28 @@
+public class FindTheClosestPalindrome {
+
+ /**
+ * 此题只给出暴力法
+ * 此种题目太math了,一般不会问
+ */
+ public String nearestPalindromic(String n) {
+ long val = Long.parseLong(n);
+ for (int i = 0; ; i++) {
+ long k1 = val - i, k2 = val + i;
+ if (isPalindrome(k1)) {
+ return String.valueOf(k1);
+ }
+ if (isPalindrome(k2)) {
+ return String.valueOf(k2);
+ }
+ }
+ }
+
+ private boolean isPalindrome(long k) {
+ long x = k, rev = 0;
+ for ( ; k > 0; ) {
+ rev = rev * 10 + k % 10;
+ k /= 10;
+ }
+ return rev == x;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java b/leetcode/solution/src/FindTheDuplicateNumber.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java
rename to leetcode/solution/src/FindTheDuplicateNumber.java
index 72ad9e1..d6ac374 100644
--- a/solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java
+++ b/leetcode/solution/src/FindTheDuplicateNumber.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
/**
* 题目的意思是有n+1个数,这些数的范围限定在[1,n],其中只有一个重复数,重复的次数不限
*/
diff --git a/solution/src/main/java/com/inuker/solution/FirstBadVersion.java b/leetcode/solution/src/FirstBadVersion.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/FirstBadVersion.java
rename to leetcode/solution/src/FirstBadVersion.java
index d42389e..aca392a 100644
--- a/solution/src/main/java/com/inuker/solution/FirstBadVersion.java
+++ b/leetcode/solution/src/FirstBadVersion.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/19.
- */
-
/**
* https://leetcode.com/articles/first-bad-version/
*/
diff --git a/solution/src/main/java/com/inuker/solution/FirstMissingPositive.java b/leetcode/solution/src/FirstMissingPositive.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/FirstMissingPositive.java
rename to leetcode/solution/src/FirstMissingPositive.java
index 4a72ea4..8c04de3 100644
--- a/solution/src/main/java/com/inuker/solution/FirstMissingPositive.java
+++ b/leetcode/solution/src/FirstMissingPositive.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/5/11.
- */
-
/**
* 这里要注意的是nums[nums[i] - 1] != nums[i]这个条件,意思是目标坑和当前坑的值不等,此时才能swap
* 倘若换成nums[i] - 1 != i是不行的,这表示目标坑和当前坑不是一个坑就swap,会死循环
@@ -12,7 +6,7 @@ public class FirstMissingPositive {
public int firstMissingPositive(int[] nums) {
for (int i = 0; i < nums.length; i++) {
- while (nums[i] >= 1 && nums[i] <= nums.length && nums[nums[i] - 1] != nums[i]) {
+ while (nums[i] - 1 >= 0 && nums[i] - 1 < nums.length && nums[nums[i] - 1] != nums[i]) {
swap(nums, i, nums[i] - 1);
}
}
diff --git a/amazon/src/main/java/com/leetcode/amazon/FirstUniqueCharacterInAString.java b/leetcode/solution/src/FirstUniqueCharacterInAString.java
similarity index 83%
rename from amazon/src/main/java/com/leetcode/amazon/FirstUniqueCharacterInAString.java
rename to leetcode/solution/src/FirstUniqueCharacterInAString.java
index 206cba8..c0966a8 100644
--- a/amazon/src/main/java/com/leetcode/amazon/FirstUniqueCharacterInAString.java
+++ b/leetcode/solution/src/FirstUniqueCharacterInAString.java
@@ -1,11 +1,6 @@
-package com.leetcode.amazon;
-
-/**
- * Created by liwentian on 17/8/10.
- */
-
public class FirstUniqueCharacterInAString {
+ // 耗时21ms,O(2n)
public int firstUniqChar(String s) {
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
diff --git a/solution/src/main/java/com/inuker/solution/FizzBuzz.java b/leetcode/solution/src/FizzBuzz.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/FizzBuzz.java
rename to leetcode/solution/src/FizzBuzz.java
index 34cb0b6..776b9bf 100644
--- a/solution/src/main/java/com/inuker/solution/FizzBuzz.java
+++ b/leetcode/solution/src/FizzBuzz.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/7.
- */
-
public class FizzBuzz {
public List fizzBuzz(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/FlattenBinaryTreeToLinkedList.java b/leetcode/solution/src/FlattenBinaryTreeToLinkedList.java
similarity index 72%
rename from solution/src/main/java/com/inuker/solution/FlattenBinaryTreeToLinkedList.java
rename to leetcode/solution/src/FlattenBinaryTreeToLinkedList.java
index 53b2985..60028c4 100644
--- a/solution/src/main/java/com/inuker/solution/FlattenBinaryTreeToLinkedList.java
+++ b/leetcode/solution/src/FlattenBinaryTreeToLinkedList.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/11/30.
- */
-
public class FlattenBinaryTreeToLinkedList {
/**
@@ -40,22 +32,25 @@ public void flatten2(TreeNode root) {
helper(root);
}
+ /**
+ * 这题要注意ltail和rtail都为null的情况
+ */
public TreeNode helper(TreeNode root) {
if (root == null) {
- return null;
+ return root;
}
+
+ TreeNode ltail = helper(root.left);
+ TreeNode rtail = helper(root.right);
+
TreeNode right = root.right;
- TreeNode leftTail = null, rightTail = null;
- if (root.left != null) {
- leftTail = helper(root.left);
+ if (ltail != null) {
root.right = root.left;
root.left = null;
- leftTail.right = right;
+ ltail.right = right;
}
- rightTail = helper(right);
-
- return rightTail != null ? rightTail : leftTail;
+ return rtail != null ? rtail : ltail != null ? ltail : root;
}
}
diff --git a/solution/src/main/java/com/inuker/solution/FlattenNestedListIterator.java b/leetcode/solution/src/FlattenNestedListIterator.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/FlattenNestedListIterator.java
rename to leetcode/solution/src/FlattenNestedListIterator.java
index 56c4bff..eb7d611 100644
--- a/solution/src/main/java/com/inuker/solution/FlattenNestedListIterator.java
+++ b/leetcode/solution/src/FlattenNestedListIterator.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.NestedInteger;
-
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
-/**
- * Created by dingjikerbo on 2016/11/22.
- */
-
public abstract class FlattenNestedListIterator implements Iterator {
private Stack stack;
diff --git a/solution/src/main/java/com/inuker/solution/FlipGame.java b/leetcode/solution/src/FlipGame.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/FlipGame.java
rename to leetcode/solution/src/FlipGame.java
index 472b455..030671f 100644
--- a/solution/src/main/java/com/inuker/solution/FlipGame.java
+++ b/leetcode/solution/src/FlipGame.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/6.
- */
-
public class FlipGame {
public List generatePossibleNextMoves(String s) {
diff --git a/solution/src/main/java/com/inuker/solution/FlipGameII.java b/leetcode/solution/src/FlipGameII.java
similarity index 78%
rename from solution/src/main/java/com/inuker/solution/FlipGameII.java
rename to leetcode/solution/src/FlipGameII.java
index 97f56a7..29ead0e 100644
--- a/solution/src/main/java/com/inuker/solution/FlipGameII.java
+++ b/leetcode/solution/src/FlipGameII.java
@@ -1,12 +1,3 @@
-package com.inuker.solution;
-
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * Created by liwentian on 2017/9/6.
- */
-
public class FlipGameII {
public boolean canWin(String s) {
diff --git a/solution/src/main/java/com/inuker/solution/FourSum.java b/leetcode/solution/src/FourSum.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/FourSum.java
rename to leetcode/solution/src/FourSum.java
index 2225598..0f9f8f4 100644
--- a/solution/src/main/java/com/inuker/solution/FourSum.java
+++ b/leetcode/solution/src/FourSum.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 17/5/5.
- */
-
/**
* 本题要注意的一个是溢出,一个是查重
*/
diff --git a/leetcode/solution/src/FriendCircles.java b/leetcode/solution/src/FriendCircles.java
new file mode 100644
index 0000000..56a5144
--- /dev/null
+++ b/leetcode/solution/src/FriendCircles.java
@@ -0,0 +1,69 @@
+public class FriendCircles {
+
+ public int findCircleNum(int[][] M) {
+ int num = 0;
+ for (int i = 0; i < M.length; i++) {
+ if (M[i][i] == 1) {
+ dfs(M, i);
+ num++;
+ }
+ }
+ return num;
+ }
+
+ /**
+ * M[i][i] = 0表示第i个人我们已经访问过了
+ * 访问过的人无需重复访问
+ */
+ private void dfs(int[][] M, int i) {
+ M[i][i] = 0;
+ for (int j = 0; j < M.length; j++) {
+ if (M[j][j] != 0 && M[i][j] == 1) {
+ dfs(M, j);
+ }
+ }
+ }
+
+ public int findCircleNum2(int[][] M) {
+ int n = M.length;
+ UnionFind uf = new UnionFind(n);
+ for (int i = 0; i < n; i++) {
+ for (int j = i + 1; j < n; j++) {
+ if (M[i][j] == 1) {
+ uf.union(i, j);
+ }
+ }
+ }
+ return uf.count();
+ }
+
+ class UnionFind {
+ int[] path;
+ int count;
+
+ public UnionFind(int n) {
+ path = new int[n];
+ count = n;
+ for (int i = 0; i < n; i++) {
+ path[i] = i;
+ }
+ }
+
+ public int find(int i) {
+ while (i != path[i]) i = path[i];
+ return i;
+ }
+
+ public void union(int i, int j) {
+ int rootI = find(i);
+ int rootJ = find(j);
+ if (rootI == rootJ) return;
+ path[rootI] = rootJ;
+ count--;
+ }
+
+ public int count() {
+ return count;
+ }
+ }
+}
diff --git a/leetcode/solution/src/FruitIntoBaskets.java b/leetcode/solution/src/FruitIntoBaskets.java
new file mode 100644
index 0000000..e193da3
--- /dev/null
+++ b/leetcode/solution/src/FruitIntoBaskets.java
@@ -0,0 +1,31 @@
+import java.util.HashMap;
+
+/**
+ * 这题和sliding window较类似
+ * 意思是给定一个数组,找出其中的一个最长的连续子数组,只能有两个不同的数
+ */
+public class FruitIntoBaskets {
+
+
+ public static int totalFruit(int[] tree) {
+ int max = 0;
+ HashMap map = new HashMap<>();
+ for (int i = 0, j = 0; j < tree.length; j++) {
+ int value = tree[j];
+
+ map.put(value, map.getOrDefault(value, 0) + 1);
+
+ for ( ; map.size() > 2; i++) {
+ int val = tree[i], cnt = map.get(val);
+ if (cnt == 1) {
+ map.remove(val);
+ } else {
+ map.put(val, cnt - 1);
+ }
+ }
+ max = Math.max(max, j - i + 1);
+ }
+
+ return max;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/GameOfLife.java b/leetcode/solution/src/GameOfLife.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/GameOfLife.java
rename to leetcode/solution/src/GameOfLife.java
index ebaca62..3d5744c 100644
--- a/solution/src/main/java/com/inuker/solution/GameOfLife.java
+++ b/leetcode/solution/src/GameOfLife.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class GameOfLife {
public void gameOfLife(int[][] board) {
diff --git a/solution/src/main/java/com/inuker/solution/GeneralizedAbbreviation.java b/leetcode/solution/src/GeneralizedAbbreviation.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/GeneralizedAbbreviation.java
rename to leetcode/solution/src/GeneralizedAbbreviation.java
index ccc62bc..dca0bfb 100644
--- a/solution/src/main/java/com/inuker/solution/GeneralizedAbbreviation.java
+++ b/leetcode/solution/src/GeneralizedAbbreviation.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 2016/12/29.
- */
-
/**
* https://leetcode.com/articles/generalized-abbreviation/
* 思路就是back tracking,对于每个字母,到底是选择留下还是缩写
diff --git a/solution/src/main/java/com/inuker/solution/GenerateParentheses.java b/leetcode/solution/src/GenerateParentheses.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/GenerateParentheses.java
rename to leetcode/solution/src/GenerateParentheses.java
index a40c139..92acd47 100644
--- a/solution/src/main/java/com/inuker/solution/GenerateParentheses.java
+++ b/leetcode/solution/src/GenerateParentheses.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/12/8.
- */
-
public class GenerateParentheses {
// 耗时4ms
diff --git a/solution/src/main/java/com/inuker/solution/GraphValidTree.java b/leetcode/solution/src/GraphValidTree.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/GraphValidTree.java
rename to leetcode/solution/src/GraphValidTree.java
index ab388a9..43912a4 100644
--- a/solution/src/main/java/com/inuker/solution/GraphValidTree.java
+++ b/leetcode/solution/src/GraphValidTree.java
@@ -1,7 +1,4 @@
-package com.inuker.solution;
-
/**
- * Created by dingjikerbo on 17/1/2.
*
* 判断无向图是否带环,可采用UF, DFS, BFS。
* UF实现简单,性能很好
@@ -16,12 +13,7 @@
* 题目中已声明不会有重复的边,类似[0,1]和[1,0]认为是重复的,也不会同时存在
*/
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Queue;
-import java.util.Set;
+import java.util.*;
/**
* 这题就是给了一堆边,看这些边构成的无向图会不会有环,另外这些边是不是都连在一起的
diff --git a/solution/src/main/java/com/inuker/solution/GrayCode.java b/leetcode/solution/src/GrayCode.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/GrayCode.java
rename to leetcode/solution/src/GrayCode.java
index c5a1b6e..8947b74 100644
--- a/solution/src/main/java/com/inuker/solution/GrayCode.java
+++ b/leetcode/solution/src/GrayCode.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/29.
- */
-
/**
* 关键公式整数n的格雷码为n ^ (n / 2)
*/
diff --git a/amazon/src/main/java/com/leetcode/amazon/GroupAnagrams.java b/leetcode/solution/src/GroupAnagrams.java
similarity index 89%
rename from amazon/src/main/java/com/leetcode/amazon/GroupAnagrams.java
rename to leetcode/solution/src/GroupAnagrams.java
index 7b068c2..f96d3e4 100644
--- a/amazon/src/main/java/com/leetcode/amazon/GroupAnagrams.java
+++ b/leetcode/solution/src/GroupAnagrams.java
@@ -1,16 +1,11 @@
-package com.leetcode.amazon;
-
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 17/8/13.
- */
-
public class GroupAnagrams {
+ // 12ms
public List> groupAnagrams(String[] strs) {
HashMap> map = new HashMap<>();
@@ -31,4 +26,5 @@ public List> groupAnagrams(String[] strs) {
return new LinkedList<>(map.values());
}
+
}
diff --git a/solution/src/main/java/com/inuker/solution/GroupShiftedStrings.java b/leetcode/solution/src/GroupShiftedStrings.java
similarity index 82%
rename from solution/src/main/java/com/inuker/solution/GroupShiftedStrings.java
rename to leetcode/solution/src/GroupShiftedStrings.java
index 3d2877e..00a3249 100644
--- a/solution/src/main/java/com/inuker/solution/GroupShiftedStrings.java
+++ b/leetcode/solution/src/GroupShiftedStrings.java
@@ -1,14 +1,4 @@
-package com.inuker.solution;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Created by liwentian on 2017/12/9.
- */
+import java.util.*;
public class GroupShiftedStrings {
/**
diff --git a/solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLower.java b/leetcode/solution/src/GuessNumberHigherOrLower.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLower.java
rename to leetcode/solution/src/GuessNumberHigherOrLower.java
index 8902f74..da86cd6 100644
--- a/solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLower.java
+++ b/leetcode/solution/src/GuessNumberHigherOrLower.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
/**
* 可参考官方文档
* https://leetcode.com/articles/guess-number-higher-or-lower/
diff --git a/solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLowerII.java b/leetcode/solution/src/GuessNumberHigherOrLowerII.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLowerII.java
rename to leetcode/solution/src/GuessNumberHigherOrLowerII.java
index 1757aed..7b0275c 100644
--- a/solution/src/main/java/com/inuker/solution/GuessNumberHigherOrLowerII.java
+++ b/leetcode/solution/src/GuessNumberHigherOrLowerII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
/**
* 可参考官方文档
* https://leetcode.com/articles/guess-number-higher-or-lower-ii/
diff --git a/solution/src/main/java/com/inuker/solution/HIndex.java b/leetcode/solution/src/HIndex.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/HIndex.java
rename to leetcode/solution/src/HIndex.java
index c519f2d..771e95e 100644
--- a/solution/src/main/java/com/inuker/solution/HIndex.java
+++ b/leetcode/solution/src/HIndex.java
@@ -1,10 +1,5 @@
-package com.inuker.solution;
-
import java.util.Arrays;
-/**
- * Created by dingjikerbo on 16/11/23.
- */
/**
* https://leetcode.com/articles/h-index/
*/
diff --git a/solution/src/main/java/com/inuker/solution/HIndexII.java b/leetcode/solution/src/HIndexII.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/HIndexII.java
rename to leetcode/solution/src/HIndexII.java
index 994e1a5..e9c5918 100644
--- a/solution/src/main/java/com/inuker/solution/HIndexII.java
+++ b/leetcode/solution/src/HIndexII.java
@@ -1,8 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
public class HIndexII {
diff --git a/solution/src/main/java/com/inuker/solution/HammingDistance.java b/leetcode/solution/src/HammingDistance.java
similarity index 70%
rename from solution/src/main/java/com/inuker/solution/HammingDistance.java
rename to leetcode/solution/src/HammingDistance.java
index 1803ee6..eb2900a 100644
--- a/solution/src/main/java/com/inuker/solution/HammingDistance.java
+++ b/leetcode/solution/src/HammingDistance.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/1/7.
- */
-
public class HammingDistance {
public int hammingDistance(int x, int y) {
diff --git a/solution/src/main/java/com/inuker/solution/HouseRobber.java b/leetcode/solution/src/HouseRobber.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/HouseRobber.java
rename to leetcode/solution/src/HouseRobber.java
index 58ccaf9..9ae674e 100644
--- a/solution/src/main/java/com/inuker/solution/HouseRobber.java
+++ b/leetcode/solution/src/HouseRobber.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/1.
- */
-
public class HouseRobber {
/**
diff --git a/solution/src/main/java/com/inuker/solution/HouseRobberII.java b/leetcode/solution/src/HouseRobberII.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/HouseRobberII.java
rename to leetcode/solution/src/HouseRobberII.java
index 479a6e3..0865864 100644
--- a/solution/src/main/java/com/inuker/solution/HouseRobberII.java
+++ b/leetcode/solution/src/HouseRobberII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
public class HouseRobberII {
public int rob(int[] nums) {
diff --git a/leetcode/solution/src/HouseRobberIII.java b/leetcode/solution/src/HouseRobberIII.java
new file mode 100644
index 0000000..6e0cc3d
--- /dev/null
+++ b/leetcode/solution/src/HouseRobberIII.java
@@ -0,0 +1,46 @@
+public class HouseRobberIII {
+
+ /**
+ * 效率堪忧,耗时1000ms
+ */
+ public int rob(TreeNode root) {
+ return rob(root, true);
+ }
+
+ /**
+ * @param rob true表示不定,false表示不选root
+ */
+ private int rob(TreeNode root, boolean rob) {
+ if (root == null) {
+ return 0;
+ }
+
+ if (rob) {
+ return Math.max(root.val + rob(root.left, false) + rob(root.right, false),
+ rob(root.left, true) + rob(root.right, true));
+ } else {
+ return rob(root.left, true) + rob(root.right, true);
+ }
+ }
+
+ /**
+ * 优化写法如下,耗时1ms
+ */
+ public int rob2(TreeNode root) {
+ int[] val = helper(root);
+ return Math.max(val[0], val[1]);
+ }
+
+ private int[] helper(TreeNode node) {
+ if (node == null) {
+ return new int[]{0,0};
+ }
+ int[] left = helper(node.left);
+ int[] right = helper(node.right);
+ int[] value = new int[2];
+ value[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
+ value[1] = node.val + left[0] + right[0];
+ return value;
+ }
+
+}
diff --git a/solution/src/main/java/com/inuker/solution/IncreasingTripletSubsequence.java b/leetcode/solution/src/IncreasingTripletSubsequence.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/IncreasingTripletSubsequence.java
rename to leetcode/solution/src/IncreasingTripletSubsequence.java
index f2e6528..8801679 100644
--- a/solution/src/main/java/com/inuker/solution/IncreasingTripletSubsequence.java
+++ b/leetcode/solution/src/IncreasingTripletSubsequence.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
-
// 是longest increasing subsequence的简化版
public class IncreasingTripletSubsequence {
diff --git a/solution/src/main/java/com/inuker/solution/InorderSuccessorInBST.java b/leetcode/solution/src/InorderSuccessorInBST.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/InorderSuccessorInBST.java
rename to leetcode/solution/src/InorderSuccessorInBST.java
index af0cff9..dc0e56d 100644
--- a/solution/src/main/java/com/inuker/solution/InorderSuccessorInBST.java
+++ b/leetcode/solution/src/InorderSuccessorInBST.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
/**
* 有两种方法,用栈做普通的中序遍历,这种没有充分利用BST的特点
* 第二种方法比较巧妙,首先遍历到p,然后继续遍历找到p的右子树的最小值
@@ -37,15 +29,33 @@ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
return null;
}
+ // 耗时2ms,简单的递归写法,更容易理解
/**
- * p的下一个节点一定是比p大的,所以这里遍历时当p的值小于当前节点,则当前节点
- * 可作为备选,同时往左走。如果在遍历过程中遇到仍然比p大的,说明更接近p,则更新备选。
- *
- * 有两点要注意,
- * 1, 首先res初始要为null,一个节点时,或root为null时,或p为最大节点时,res都没机会赋值
- * 2, 当root迭代到等于p时,走哪个分支呢,为什么选root = root.right,假如root.right为空,则之前的res即可,否则
- * 下一个迭代肯定走到root.val > p.val分支中,更新res。
- */
+ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
+ if (root == null) {
+ return null;
+ }
+ if (root.val > p.val) {
+ TreeNode node = inorderSuccessor(root.left, p);
+ return node != null ? node : root;
+ } else {
+ return inorderSuccessor(root.right, p);
+ }
+ }
+
+ public TreeNode predecessor(TreeNode root, TreeNode p) {
+ if (root == null)
+ return null;
+
+ if (root.val >= p.val) {
+ return predecessor(root.left, p);
+ } else {
+ TreeNode right = predecessor(root.right, p);
+ return (right != null) ? right : root;
+ }
+ }*/
+
+ // 给上面的递归换成迭代写法
// 耗时4ms
public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
TreeNode res = null;
@@ -65,6 +75,8 @@ public TreeNode inorderSuccessor2(TreeNode root, TreeNode p) {
* 给定Node,求其successor,步骤如下:
* 1, 如果Node.right != null,则在Node.right中找最小的那个节点,即从Node.right开始,最左下角的节点
* 2, 如果Node.right == null,则不断往parent走,直到当前节点是其parent的左节点为止,其parent即为给定Node的successor
+ */
+ /*
private TreeNode inOrderSuccessor(TreeNode root, TreeNode node) {
if (node.right != null) {
return minValue(node.right);
diff --git a/leetcode/solution/src/InsertDeleteGetRandom.java b/leetcode/solution/src/InsertDeleteGetRandom.java
new file mode 100644
index 0000000..3be393b
--- /dev/null
+++ b/leetcode/solution/src/InsertDeleteGetRandom.java
@@ -0,0 +1,56 @@
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * 有几点要注意:
+ * remove时要判断删除的是不是最后一个,另外交换了结尾后要更新结尾数的idx
+ */
+// 耗时111ms
+public class InsertDeleteGetRandom {
+
+ ArrayList list;
+ HashMap map;
+ Random random;
+
+ /** Initialize your data structure here. */
+ public InsertDeleteGetRandom() {
+ map = new HashMap<>();
+ list = new ArrayList<>();
+ random = new Random();
+ }
+
+ /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
+ public boolean insert(int val) {
+ if (map.containsKey(val)) {
+ return false;
+ }
+ list.add(val);
+ map.put(val, list.size() - 1);
+ return true;
+ }
+
+ /** Removes a value from the set. Returns true if the set contained the specified element. */
+ public boolean remove(int val) {
+ int index = map.getOrDefault(val, -1);
+ if (index < 0) {
+ return false;
+ }
+ if (index != list.size() - 1) {
+ int tail = list.get(list.size() - 1);
+ list.set(index, tail);
+ map.put(tail, index);
+ }
+ map.remove(val);
+ list.remove(list.size() - 1);
+ return true;
+ }
+
+ /** Get a random element from the set. */
+ public int getRandom() {
+ int index = random.nextInt(list.size());
+ index = (index >= 0 ? index : -index);
+ return list.get(index);
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/InsertDeleteGetRandomII.java b/leetcode/solution/src/InsertDeleteGetRandomII.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/InsertDeleteGetRandomII.java
rename to leetcode/solution/src/InsertDeleteGetRandomII.java
index c3bf840..ec6f23d 100644
--- a/solution/src/main/java/com/inuker/solution/InsertDeleteGetRandomII.java
+++ b/leetcode/solution/src/InsertDeleteGetRandomII.java
@@ -1,15 +1,4 @@
-package com.inuker.solution;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Random;
-import java.util.Vector;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
+import java.util.*;
/**
* 这题的区别在于允许数字重复,那么保存index需要用set,删除的时候从set中随便删一个就行了
diff --git a/solution/src/main/java/com/inuker/solution/InsertInterval.java b/leetcode/solution/src/InsertInterval.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/InsertInterval.java
rename to leetcode/solution/src/InsertInterval.java
index 3b46189..5369066 100644
--- a/solution/src/main/java/com/inuker/solution/InsertInterval.java
+++ b/leetcode/solution/src/InsertInterval.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.Interval;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class InsertInterval {
public List insert(List intervals, Interval newInterval) {
diff --git a/solution/src/main/java/com/inuker/solution/InsertionSortList.java b/leetcode/solution/src/InsertionSortList.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/InsertionSortList.java
rename to leetcode/solution/src/InsertionSortList.java
index 4aab3b9..4090938 100644
--- a/solution/src/main/java/com/inuker/solution/InsertionSortList.java
+++ b/leetcode/solution/src/InsertionSortList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by liwentian on 2017/9/11.
- */
-
public class InsertionSortList {
/**
diff --git a/solution/src/main/java/com/inuker/solution/IntegerToEnglishWords.java b/leetcode/solution/src/IntegerToEnglishWords.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/IntegerToEnglishWords.java
rename to leetcode/solution/src/IntegerToEnglishWords.java
index 9d892b2..74375a4 100644
--- a/solution/src/main/java/com/inuker/solution/IntegerToEnglishWords.java
+++ b/leetcode/solution/src/IntegerToEnglishWords.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/19.
- */
-
/**
* TestCase
* 0
diff --git a/solution/src/main/java/com/inuker/solution/IntegerToRoman.java b/leetcode/solution/src/IntegerToRoman.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/IntegerToRoman.java
rename to leetcode/solution/src/IntegerToRoman.java
index 27d661e..3e814cf 100644
--- a/solution/src/main/java/com/inuker/solution/IntegerToRoman.java
+++ b/leetcode/solution/src/IntegerToRoman.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class IntegerToRoman {
public String intToRoman(int num) {
diff --git a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoArrays.java b/leetcode/solution/src/IntersectionOfTwoArrays.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/IntersectionOfTwoArrays.java
rename to leetcode/solution/src/IntersectionOfTwoArrays.java
index 0721cf4..2a14c72 100644
--- a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoArrays.java
+++ b/leetcode/solution/src/IntersectionOfTwoArrays.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
-/**
- * Created by liwentian on 2017/9/22.
- */
-
public class IntersectionOfTwoArrays {
public int[] intersection(int[] nums1, int[] nums2) {
diff --git a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoArraysII.java b/leetcode/solution/src/IntersectionOfTwoArraysII.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/IntersectionOfTwoArraysII.java
rename to leetcode/solution/src/IntersectionOfTwoArraysII.java
index 3dda626..5d812cb 100644
--- a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoArraysII.java
+++ b/leetcode/solution/src/IntersectionOfTwoArraysII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.HashMap;
-/**
- * Created by liwentian on 2017/9/22.
- */
-
public class IntersectionOfTwoArraysII {
public int[] intersect(int[] nums1, int[] nums2) {
diff --git a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoLinkedLists.java b/leetcode/solution/src/IntersectionOfTwoLinkedLists.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/IntersectionOfTwoLinkedLists.java
rename to leetcode/solution/src/IntersectionOfTwoLinkedLists.java
index 2ad7edb..f1f5661 100644
--- a/solution/src/main/java/com/inuker/solution/IntersectionOfTwoLinkedLists.java
+++ b/leetcode/solution/src/IntersectionOfTwoLinkedLists.java
@@ -1,7 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
/**
* Created by dingjiekrbo on 2016/11/17.
* https://leetcode.com/articles/intersection-two-linked-lists/
diff --git a/solution/src/main/java/com/inuker/solution/InvertBinaryTree.java b/leetcode/solution/src/InvertBinaryTree.java
similarity index 72%
rename from solution/src/main/java/com/inuker/solution/InvertBinaryTree.java
rename to leetcode/solution/src/InvertBinaryTree.java
index 0e763f1..6efd2c9 100644
--- a/solution/src/main/java/com/inuker/solution/InvertBinaryTree.java
+++ b/leetcode/solution/src/InvertBinaryTree.java
@@ -1,30 +1,12 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.LinkedList;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 16/11/28.
- */
-
/**
* 给非递归法也要会写
+ * 核心思路就是访问所有的节点,将其左右节点交换,可以采用递归,也可以BFS
*/
public class InvertBinaryTree {
- /** 错误的写法,写顺手了
- public TreeNode invertTree(TreeNode root) {
- if (root == null) {
- return null;
- }
-
- root.right = invertTree(root.left);
- root.left = invertTree(root.right);
- return root;
- }*/
-
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
diff --git a/leetcode/solution/src/JewelsAndStones.java b/leetcode/solution/src/JewelsAndStones.java
new file mode 100644
index 0000000..75d2068
--- /dev/null
+++ b/leetcode/solution/src/JewelsAndStones.java
@@ -0,0 +1,16 @@
+public class JewelsAndStones {
+
+ public int numJewelsInStones(String J, String S) {
+ int character[] = new int[256];
+ for (char c : J.toCharArray()) {
+ character[c]++;
+ }
+ int count = 0;
+ for (char c : S.toCharArray()) {
+ if (character[c] > 0) {
+ count++;
+ }
+ }
+ return count;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/JumpGame.java b/leetcode/solution/src/JumpGame.java
similarity index 76%
rename from solution/src/main/java/com/inuker/solution/JumpGame.java
rename to leetcode/solution/src/JumpGame.java
index a91d60b..beb0863 100644
--- a/solution/src/main/java/com/inuker/solution/JumpGame.java
+++ b/leetcode/solution/src/JumpGame.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/5/11.
- */
-
public class JumpGame {
public boolean canJump(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/JumpGameII.java b/leetcode/solution/src/JumpGameII.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/JumpGameII.java
rename to leetcode/solution/src/JumpGameII.java
index c2213d7..03b00b0 100644
--- a/solution/src/main/java/com/inuker/solution/JumpGameII.java
+++ b/leetcode/solution/src/JumpGameII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2017/5/20.
- */
-
public class JumpGameII {
/**
diff --git a/leetcode/solution/src/KthLargestElementInAStream.java b/leetcode/solution/src/KthLargestElementInAStream.java
new file mode 100644
index 0000000..27cff7b
--- /dev/null
+++ b/leetcode/solution/src/KthLargestElementInAStream.java
@@ -0,0 +1,26 @@
+import java.util.PriorityQueue;
+import java.util.Queue;
+
+public class KthLargestElementInAStream {
+
+ class KthLargest {
+
+ Queue queue = new PriorityQueue<>();
+ int capacity;
+
+ public KthLargest(int k, int[] nums) {
+ capacity = k;
+ for (int n : nums) {
+ queue.offer(n);
+ }
+ }
+
+ public int add(int val) {
+ queue.offer(val);
+ while (queue.size() > capacity) {
+ queue.poll();
+ }
+ return queue.peek();
+ }
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/KthLargestElementInArray.java b/leetcode/solution/src/KthLargestElementInArray.java
similarity index 78%
rename from solution/src/main/java/com/inuker/solution/KthLargestElementInArray.java
rename to leetcode/solution/src/KthLargestElementInArray.java
index 288d2dc..6a55336 100644
--- a/solution/src/main/java/com/inuker/solution/KthLargestElementInArray.java
+++ b/leetcode/solution/src/KthLargestElementInArray.java
@@ -1,13 +1,10 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.PriorityQueue;
-import java.util.Queue;
+import java.util.Random;
/**
- * Created by dingjikerbo on 2016/11/17.
+ * 注意本题不是返回第k大的distinct number
*/
-
public class KthLargestElementInArray {
// 耗时15ms,时间复杂度O(nlgk),空间复杂度O(k)
@@ -33,11 +30,23 @@ public int findKthLargest3(int[] nums, int k) {
// T(n) = T(n / 2) + n = O(2n)
// 对比快速排序T(n) = 2T(n / 2) + n = O(nlgn)
// 区别在于这个被pivot分隔后,只用处理其中的一半,而快排两边都要处理
- // 耗时18ms
+
+ /**
+ * shuffle非常有必要,性能提升很大
+ */
public int findKthLargest(int[] nums, int k) {
+ shuffle(nums);
return findKthLargest(nums, 0, nums.length - 1, k);
}
+ private void shuffle(int[] nums) {
+ Random rnd = new Random(System.currentTimeMillis());
+ for (int i = nums.length - 1; i > 1; i--) {
+ int index = rnd.nextInt(i + 1);
+ swap(nums, index, i);
+ }
+ }
+
public int findKthLargest(int[] nums, int start, int end, int k) {
int pivot = partition(nums, start, end);
@@ -52,6 +61,10 @@ public int findKthLargest(int[] nums, int start, int end, int k) {
}
}
+ /**
+ * 这里两端都是闭区间
+ * 这里将数组分为两部分,左边小于pivot,右边大于pivot,中间可能等于pivot
+ */
public int partition(int[] nums, int start, int end) {
int pivot = nums[end], left = start, right = end - 1;
diff --git a/solution/src/main/java/com/inuker/solution/KthSmallestElementInASortedMatrix.java b/leetcode/solution/src/KthSmallestElementInASortedMatrix.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/KthSmallestElementInASortedMatrix.java
rename to leetcode/solution/src/KthSmallestElementInASortedMatrix.java
index e176d07..5d7e85b 100644
--- a/solution/src/main/java/com/inuker/solution/KthSmallestElementInASortedMatrix.java
+++ b/leetcode/solution/src/KthSmallestElementInASortedMatrix.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.PriorityQueue;
-/**
- * Created by liwentian on 2017/9/12.
- */
-
public class KthSmallestElementInASortedMatrix {
public int kthSmallest(int[][] matrix, int k) {
diff --git a/solution/src/main/java/com/inuker/solution/KthSmallestElementInBST.java b/leetcode/solution/src/KthSmallestElementInBST.java
similarity index 74%
rename from solution/src/main/java/com/inuker/solution/KthSmallestElementInBST.java
rename to leetcode/solution/src/KthSmallestElementInBST.java
index 488a1ff..31e34d0 100644
--- a/solution/src/main/java/com/inuker/solution/KthSmallestElementInBST.java
+++ b/leetcode/solution/src/KthSmallestElementInBST.java
@@ -1,13 +1,5 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/11/30.
- */
-
/**
* 这题关键是follow up
* 如果更新频繁,则通常的做法每次都是O(n),更优的做法是O(lgn)
@@ -22,30 +14,6 @@
public class KthSmallestElementInBST {
public int kthSmallest(TreeNode root, int k) {
- int[] kth = new int[1];
- inorderTraversal(root, k, kth);
- return kth[0];
- }
-
- private int inorderTraversal(TreeNode root, int k, int[] kth) {
- if (root == null) {
- return k;
- }
-
- k = inorderTraversal(root.left, k, kth);
-
- if (--k == 0) {
- kth[0] = root.val;
- return 0;
- }
-
- return inorderTraversal(root.right, k, kth);
- }
-
- /**
- * 非递归法
- */
- public int kthSmallest2(TreeNode root, int k) {
Stack stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
@@ -67,7 +35,7 @@ public int kthSmallest2(TreeNode root, int k) {
/**
* Morris
*/
- private int kthSmallest3(TreeNode root, int k) {
+ private int kthSmallest2(TreeNode root, int k) {
TreeNode temp;
while (root != null) {
diff --git a/solution/src/main/java/com/inuker/solution/LFUCache.java b/leetcode/solution/src/LFUCache.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/LFUCache.java
rename to leetcode/solution/src/LFUCache.java
index 797cff6..0878c1a 100644
--- a/solution/src/main/java/com/inuker/solution/LFUCache.java
+++ b/leetcode/solution/src/LFUCache.java
@@ -1,11 +1,6 @@
-package com.inuker.solution;
-
import java.util.HashMap;
import java.util.LinkedHashSet;
-/**
- * Created by dingjikerbo on 17/1/2.
- */
/**
* 和LRU的区别是
* LRU,即便历史频率再高,如果最近用的少了一样踢掉
diff --git a/leetcode/solution/src/LRUCache.java b/leetcode/solution/src/LRUCache.java
new file mode 100644
index 0000000..69b9695
--- /dev/null
+++ b/leetcode/solution/src/LRUCache.java
@@ -0,0 +1,64 @@
+import java.util.HashMap;
+
+public class LRUCache {
+
+ private Node mHead;
+ private HashMap mMap;
+ private int mCapacity;
+
+ public LRUCache(int capacity) {
+ mMap = new HashMap<>();
+ mCapacity = capacity;
+ mHead = new Node(0, 0);
+ }
+
+ public int get(int key) {
+ if (!mMap.containsKey(key)) {
+ return -1;
+ }
+ Node node = mMap.get(key);
+ node.remove();
+ node.add(mHead);
+ return node.val;
+ }
+
+ public void put(int key, int value) {
+ Node node = mMap.get(key);
+ if (node == null) {
+ node = new Node(key, value);
+ mMap.put(key, node);
+ } else {
+ node.val = value;
+ node.remove();
+ }
+ node.add(mHead);
+ if (mMap.size() > mCapacity) {
+ Node prev = mHead.prev;
+ prev.remove();
+ mMap.remove(prev.key);
+ }
+ }
+
+ class Node {
+ Node prev, next;
+ int key, val;
+
+ Node(int key, int val) {
+ this.key = key;
+ this.val = val;
+ prev = next = this;
+ }
+
+ void remove() {
+ prev.next = next;
+ next.prev = prev;
+ }
+
+ void add(Node head) {
+ next = head.next;
+ head.next.prev = this;
+ prev = head;
+ head.next = this;
+ }
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java b/leetcode/solution/src/LargestBSTSubtree.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java
rename to leetcode/solution/src/LargestBSTSubtree.java
index 3df7a17..d7b906a 100644
--- a/solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java
+++ b/leetcode/solution/src/LargestBSTSubtree.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/12/3.
- */
-
public class LargestBSTSubtree {
class Result {
diff --git a/solution/src/main/java/com/inuker/solution/LargestNumber.java b/leetcode/solution/src/LargestNumber.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/LargestNumber.java
rename to leetcode/solution/src/LargestNumber.java
index 1e0f9f6..cce7008 100644
--- a/solution/src/main/java/com/inuker/solution/LargestNumber.java
+++ b/leetcode/solution/src/LargestNumber.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.Comparator;
-/**
- * Created by dingjikerbo on 16/12/8.
- */
-
public class LargestNumber {
public String largestNumber(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/LargestRectangleInHistogram.java b/leetcode/solution/src/LargestRectangleInHistogram.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/LargestRectangleInHistogram.java
rename to leetcode/solution/src/LargestRectangleInHistogram.java
index 47f3d94..bedb6aa 100644
--- a/solution/src/main/java/com/inuker/solution/LargestRectangleInHistogram.java
+++ b/leetcode/solution/src/LargestRectangleInHistogram.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
-
import java.util.Stack;
/**
@@ -29,7 +23,12 @@ public int largestRectangleArea(int[] heights) {
return max;
}
- /** 注意栈中的是index,不是高度
+ /**
+ * 核心思路是用栈保存一个高度递增的index,当出现一个局部最高点时,计算以该最高点为高度的最大面积
+ * 该局部最高点的形成可能是新加的柱子较矮,也可能是随着旧的高点不断出栈导致的
+ * 该局部高点的特点是"相邻的柱子都比他高",这里相邻的意思是左边延伸到下一个出栈的index,右边延伸到新加的柱子
+ *
+ * 注意栈中的是index,不是高度
* 栈保持递增,当出现小于栈顶的元素时,意味着可以计算栈顶可以延伸的矩形面积了
* 其左边界是次栈顶,右边界是当前元素
*/
diff --git a/leetcode/solution/src/LetterCombinationOfPhoneNumber.java b/leetcode/solution/src/LetterCombinationOfPhoneNumber.java
new file mode 100644
index 0000000..853afbc
--- /dev/null
+++ b/leetcode/solution/src/LetterCombinationOfPhoneNumber.java
@@ -0,0 +1,39 @@
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+
+public class LetterCombinationOfPhoneNumber {
+
+ /**
+ * leetcode的测试用例中不包括包含"0"或"1"的情况
+ */
+
+ private static final String[] ARR = {
+ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
+ };
+
+ // 耗时2ms
+ public List letterCombinations(String digits) {
+ List res = new ArrayList<>();
+ if (digits.length() == 0) {
+ return res;
+ }
+ dfs(digits, new StringBuilder(), res, 0);
+ return res;
+ }
+
+ private void dfs(String digits, StringBuilder sb, List res, int start) {
+ if (start >= digits.length()) {
+ res.add(sb.toString());
+ return;
+ }
+
+ int n = digits.charAt(start) - '0';
+ for (char c : ARR[n].toCharArray()) {
+ sb.append(c);
+ dfs(digits, sb, res, start + 1);
+ sb.setLength(sb.length() - 1);
+ }
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/LicenseKeyFormatting.java b/leetcode/solution/src/LicenseKeyFormatting.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/LicenseKeyFormatting.java
rename to leetcode/solution/src/LicenseKeyFormatting.java
index 6b721b8..bbe9cc8 100644
--- a/solution/src/main/java/com/inuker/solution/LicenseKeyFormatting.java
+++ b/leetcode/solution/src/LicenseKeyFormatting.java
@@ -1,18 +1,10 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 17/8/26.
- */
-
/**
* 将字符串s按长度k为一组,从右往左重排,组之间用"-"分隔,还要转成大写
* 注意别在最前面多加一个"-",即下面判断i != 0
*/
-
-
public class LicenseKeyFormatting {
- // 耗时22ms
+ // 耗时13ms
public String licenseKeyFormatting(String S, int K) {
StringBuilder sb = new StringBuilder();
@@ -20,7 +12,6 @@ public String licenseKeyFormatting(String S, int K) {
if (S.charAt(i) != '-') {
if (j % K == 0 && sb.length() > 0) {
sb.append("-");
- j = 0;
}
sb.append(Character.toUpperCase(S.charAt(i)));
diff --git a/solution/src/main/java/com/inuker/solution/LinkedListCycle.java b/leetcode/solution/src/LinkedListCycle.java
similarity index 66%
rename from solution/src/main/java/com/inuker/solution/LinkedListCycle.java
rename to leetcode/solution/src/LinkedListCycle.java
index e1cee81..2b6d823 100644
--- a/solution/src/main/java/com/inuker/solution/LinkedListCycle.java
+++ b/leetcode/solution/src/LinkedListCycle.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- * https://leetcode.com/articles/linked-list-cycle/
- */
-
public class LinkedListCycle {
+ // 注意判空
public boolean hasCycle(ListNode head) {
for (ListNode fast = head, slow = head; fast != null && fast.next != null; ) {
slow = slow.next;
diff --git a/solution/src/main/java/com/inuker/solution/LinkedListCycleII.java b/leetcode/solution/src/LinkedListCycleII.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/LinkedListCycleII.java
rename to leetcode/solution/src/LinkedListCycleII.java
index 8fe19d7..5c16094 100644
--- a/solution/src/main/java/com/inuker/solution/LinkedListCycleII.java
+++ b/leetcode/solution/src/LinkedListCycleII.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by liwentian on 2017/9/11.
- */
-
public class LinkedListCycleII {
/**
* 这里有个问题,如果链表是a,b,然后又回到a
diff --git a/solution/src/main/java/com/inuker/solution/LinkedListRandomNode.java b/leetcode/solution/src/LinkedListRandomNode.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/LinkedListRandomNode.java
rename to leetcode/solution/src/LinkedListRandomNode.java
index 02d1fcb..3b7573b 100644
--- a/solution/src/main/java/com/inuker/solution/LinkedListRandomNode.java
+++ b/leetcode/solution/src/LinkedListRandomNode.java
@@ -1,13 +1,5 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
import java.util.Random;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class LinkedListRandomNode {
private Random mRandom;
diff --git a/solution/src/main/java/com/inuker/solution/LoggerRateLimiter.java b/leetcode/solution/src/LoggerRateLimiter.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/LoggerRateLimiter.java
rename to leetcode/solution/src/LoggerRateLimiter.java
index 8aab161..db6a6e5 100644
--- a/solution/src/main/java/com/inuker/solution/LoggerRateLimiter.java
+++ b/leetcode/solution/src/LoggerRateLimiter.java
@@ -1,14 +1,8 @@
-package com.inuker.solution;
-
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Queue;
-/**
- * Created by liwentian on 2017/8/31.
- */
-
/**
* 如果要考虑到空间消耗,则要另外定义一个List,每次新来一条日志,都要遍历
* list中10s外的poll了,同时从map中remove
diff --git a/solution/src/main/java/com/inuker/solution/LongestAbsoluteFilePath.java b/leetcode/solution/src/LongestAbsoluteFilePath.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/LongestAbsoluteFilePath.java
rename to leetcode/solution/src/LongestAbsoluteFilePath.java
index 6aa74fe..2116455 100644
--- a/solution/src/main/java/com/inuker/solution/LongestAbsoluteFilePath.java
+++ b/leetcode/solution/src/LongestAbsoluteFilePath.java
@@ -1,13 +1,5 @@
-package com.inuker.solution;
-
-import java.util.ArrayDeque;
-import java.util.Deque;
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/11/26.
- */
-
public class LongestAbsoluteFilePath {
// 耗时4ms
diff --git a/solution/src/main/java/com/inuker/solution/LongestCommonPrefix.java b/leetcode/solution/src/LongestCommonPrefix.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/LongestCommonPrefix.java
rename to leetcode/solution/src/LongestCommonPrefix.java
index 4996559..1246091 100644
--- a/solution/src/main/java/com/inuker/solution/LongestCommonPrefix.java
+++ b/leetcode/solution/src/LongestCommonPrefix.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/5/2.
- */
-
/**
* 注意只有一个字符串的情况
* 还要注意字符串越界问题
diff --git a/solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java b/leetcode/solution/src/LongestConsecutiveSequence.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java
rename to leetcode/solution/src/LongestConsecutiveSequence.java
index 7ea27cb..cf90150 100644
--- a/solution/src/main/java/com/inuker/solution/LongestConsecutiveSequence.java
+++ b/leetcode/solution/src/LongestConsecutiveSequence.java
@@ -1,9 +1,6 @@
-package com.inuker.solution;
-
import java.util.HashMap;
/**
- * Created by dingjikerbo on 16/11/22.
* https://leetcode.com/articles/longest-consecutive-sequence/
*/
public class LongestConsecutiveSequence {
diff --git a/solution/src/main/java/com/inuker/solution/LongestIncreasingPathInAMatrix.java b/leetcode/solution/src/LongestIncreasingPathInAMatrix.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/LongestIncreasingPathInAMatrix.java
rename to leetcode/solution/src/LongestIncreasingPathInAMatrix.java
index 4af1f18..e4eb4ff 100644
--- a/solution/src/main/java/com/inuker/solution/LongestIncreasingPathInAMatrix.java
+++ b/leetcode/solution/src/LongestIncreasingPathInAMatrix.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/1.
- */
-
/**
* https://leetcode.com/articles/longest-increasing-path-matrix/
*/
diff --git a/solution/src/main/java/com/inuker/solution/LongestIncreasingSubsequence.java b/leetcode/solution/src/LongestIncreasingSubsequence.java
similarity index 53%
rename from solution/src/main/java/com/inuker/solution/LongestIncreasingSubsequence.java
rename to leetcode/solution/src/LongestIncreasingSubsequence.java
index 34a886e..92e76f3 100644
--- a/solution/src/main/java/com/inuker/solution/LongestIncreasingSubsequence.java
+++ b/leetcode/solution/src/LongestIncreasingSubsequence.java
@@ -1,15 +1,9 @@
-package com.inuker.solution;
-
import java.util.Arrays;
-/**
- * Created by dingjikerbo on 17/1/5.
- */
-
public class LongestIncreasingSubsequence {
/**
- * 这题是典型的DP,f[i]包含i的最长子序列长度
+ * 这题是典型的DP,f[i]包含i的最长递增子序列长度
*/
public int lengthOfLIS(int[] nums) {
int n = nums.length;
@@ -33,24 +27,22 @@ public int lengthOfLIS(int[] nums) {
return max;
}
+ /**
+ * 核心思路是dp维护了一个递增的序列,dp[i]表示nums中长度为i+1的递增子序列中tail最小的值
+ * tail越小意味着之后延长这个序列更容易
+ * 为了保证dp[i]是tail最小的,我们遍历nums时,当发现有更小的值时,会替换dp[i]
+ */
public int lengthOfLIS2(int[] nums) {
- int len = 1;
- for (int i = 0; i < nums.length; i++) {
- /**
- * 注意这里要指定区间,且end是开区间
- */
- int k = Arrays.binarySearch(nums, 0, len, nums[i]);
- /**
- * 只处理k<0的情况
- */
- if (k < 0) {
- k = -(k + 1);
- if (k == len) {
- len++;
- }
- nums[k] = nums[i];
- }
+ int[] dp = new int[nums.length];
+ int len = 0;
+
+ for(int x : nums) {
+ int i = Arrays.binarySearch(dp, 0, len, x);
+ if(i < 0) i = -(i + 1);
+ dp[i] = x;
+ if(i == len) len++;
}
+
return len;
}
}
diff --git a/solution/src/main/java/com/inuker/solution/LongestPalindrome.java b/leetcode/solution/src/LongestPalindrome.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/LongestPalindrome.java
rename to leetcode/solution/src/LongestPalindrome.java
index 7e2224c..4326fbe 100644
--- a/solution/src/main/java/com/inuker/solution/LongestPalindrome.java
+++ b/leetcode/solution/src/LongestPalindrome.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/19.
- */
-
public class LongestPalindrome {
public int longestPalindrome(String s) {
diff --git a/solution/src/main/java/com/inuker/solution/LongestPalindromicSubstring.java b/leetcode/solution/src/LongestPalindromicSubstring.java
similarity index 61%
rename from solution/src/main/java/com/inuker/solution/LongestPalindromicSubstring.java
rename to leetcode/solution/src/LongestPalindromicSubstring.java
index 1217e54..9c2d555 100644
--- a/solution/src/main/java/com/inuker/solution/LongestPalindromicSubstring.java
+++ b/leetcode/solution/src/LongestPalindromicSubstring.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
-
/**
* 这题采用动态规划也可,但是不论时间还是空间性能都不好
*/
@@ -11,21 +5,26 @@ public class LongestPalindromicSubstring {
private int begin, maxLen;
- // 耗时14ms,平均复杂度O(n)
- public String longestPalindrome(String s) {
+ // 耗时11ms,平均复杂度O(n)
+ public String longestPalindrome(String s) {
+ int[] res = new int[2];
for (int i = 0; i < s.length(); i++) {
- helper(s, i, i);
- helper(s, i, i + 1);
+ helper(s, i - 1, i + 1, res);
+ helper(s, i, i + 1, res);
}
- return s.substring(begin, begin + maxLen);
+ return s.substring(res[1], res[1] + res[0]);
}
- private void helper(String s, int i, int j) {
- for (; i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j); i--, j++) ;
- int len = j - i - 1;
- if (len > maxLen) {
- maxLen = len;
- begin = i + 1;
+ private void helper(String s, int left, int right, int[] res) {
+ for ( ; left >= 0 && right < s.length(); left--, right++) {
+ if (s.charAt(left) != s.charAt(right)) {
+ break;
+ }
+ }
+ int len = --right - ++left + 1;
+ if (len > res[0]) {
+ res[0] = len;
+ res[1] = left;
}
}
diff --git a/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtLeastKRepeatingCharacters.java b/leetcode/solution/src/LongestSubstringWithAtLeastKRepeatingCharacters.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/LongestSubstringWithAtLeastKRepeatingCharacters.java
rename to leetcode/solution/src/LongestSubstringWithAtLeastKRepeatingCharacters.java
index 3da934b..0abd7e2 100644
--- a/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtLeastKRepeatingCharacters.java
+++ b/leetcode/solution/src/LongestSubstringWithAtLeastKRepeatingCharacters.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/27.
- */
-
/**
* 要注意,这道题不能用window法来做,window法是先尽可能扩张window到不符合条件,然后
* 缩小window直到将不符合因素排除,所以通常适用于AtMost类型的问题,因为首先要超出most,
diff --git a/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtMostKDistinctCharacters.java b/leetcode/solution/src/LongestSubstringWithAtMostKDistinctCharacters.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/LongestSubstringWithAtMostKDistinctCharacters.java
rename to leetcode/solution/src/LongestSubstringWithAtMostKDistinctCharacters.java
index f8d46e3..46cdd41 100644
--- a/solution/src/main/java/com/inuker/solution/LongestSubstringWithAtMostKDistinctCharacters.java
+++ b/leetcode/solution/src/LongestSubstringWithAtMostKDistinctCharacters.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import java.util.HashMap;
-
-/**
- * Created by dingjikerbo on 16/11/27.
- */
-
public class LongestSubstringWithAtMostKDistinctCharacters {
/**
diff --git a/leetcode/solution/src/LongestSubstringWithAtMostTwoDistinctCharacters.java b/leetcode/solution/src/LongestSubstringWithAtMostTwoDistinctCharacters.java
new file mode 100644
index 0000000..dc1675b
--- /dev/null
+++ b/leetcode/solution/src/LongestSubstringWithAtMostTwoDistinctCharacters.java
@@ -0,0 +1,22 @@
+public class LongestSubstringWithAtMostTwoDistinctCharacters {
+
+ // 1ms
+ public int lengthOfLongestSubstringTwoDistinct2(String s) {
+ if (s.length() == 0) {
+ return 0;
+ }
+ int[] dp = new int[256];
+ int count = 0, longest = 0;
+ for (int i = 0, j = 0; j < s.length(); j++) {
+ if (dp[s.charAt(j)]++ == 0) {
+ for (++count; i <= j && count > 2; ) {
+ if (--dp[s.charAt(i++)] == 0) {
+ --count;
+ }
+ }
+ }
+ longest = Math.max(longest, j - i + 1);
+ }
+ return longest;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/LongestSubstringWithoutRepeatingCharacters.java b/leetcode/solution/src/LongestSubstringWithoutRepeatingCharacters.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/LongestSubstringWithoutRepeatingCharacters.java
rename to leetcode/solution/src/LongestSubstringWithoutRepeatingCharacters.java
index 4465e72..3ff3e38 100644
--- a/solution/src/main/java/com/inuker/solution/LongestSubstringWithoutRepeatingCharacters.java
+++ b/leetcode/solution/src/LongestSubstringWithoutRepeatingCharacters.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
/**
* https://leetcode.com/articles/longest-substring-without-repeating-characters/
*/
diff --git a/solution/src/main/java/com/inuker/solution/LongestValidParentheses.java b/leetcode/solution/src/LongestValidParentheses.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/LongestValidParentheses.java
rename to leetcode/solution/src/LongestValidParentheses.java
index e6789ca..b1ee7b9 100644
--- a/solution/src/main/java/com/inuker/solution/LongestValidParentheses.java
+++ b/leetcode/solution/src/LongestValidParentheses.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
import java.util.Stack;
/**
diff --git a/solution/src/main/java/com/inuker/solution/LongestWordInDictionaryThroughDeleting.java b/leetcode/solution/src/LongestWordInDictionaryThroughDeleting.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/LongestWordInDictionaryThroughDeleting.java
rename to leetcode/solution/src/LongestWordInDictionaryThroughDeleting.java
index 1a492c8..98a9890 100644
--- a/solution/src/main/java/com/inuker/solution/LongestWordInDictionaryThroughDeleting.java
+++ b/leetcode/solution/src/LongestWordInDictionaryThroughDeleting.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.List;
-/**
- * Created by liwentian on 2017/12/10.
- */
-
public class LongestWordInDictionaryThroughDeleting {
public String findLongestWord(String s, List d) {
diff --git a/solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinarySearchTree.java b/leetcode/solution/src/LowestCommonAncestorOfBinarySearchTree.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinarySearchTree.java
rename to leetcode/solution/src/LowestCommonAncestorOfBinarySearchTree.java
index 8b58c0d..b895c46 100644
--- a/solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinarySearchTree.java
+++ b/leetcode/solution/src/LowestCommonAncestorOfBinarySearchTree.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
-
public class LowestCommonAncestorOfBinarySearchTree {
/**
diff --git a/solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinaryTree.java b/leetcode/solution/src/LowestCommonAncestorOfBinaryTree.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinaryTree.java
rename to leetcode/solution/src/LowestCommonAncestorOfBinaryTree.java
index 13ca994..4c5689f 100644
--- a/solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinaryTree.java
+++ b/leetcode/solution/src/LowestCommonAncestorOfBinaryTree.java
@@ -1,16 +1,5 @@
-package com.inuker.solution;
+import java.util.*;
-import com.leetcode.library.TreeNode;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.Queue;
-import java.util.Set;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
public class LowestCommonAncestorOfBinaryTree {
/**
diff --git a/solution/src/main/java/com/inuker/solution/MajorityElement.java b/leetcode/solution/src/MajorityElement.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/MajorityElement.java
rename to leetcode/solution/src/MajorityElement.java
index f9c00cb..bdedd39 100644
--- a/solution/src/main/java/com/inuker/solution/MajorityElement.java
+++ b/leetcode/solution/src/MajorityElement.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class MajorityElement {
public int majorityElement(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/MajorityElementII.java b/leetcode/solution/src/MajorityElementII.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/MajorityElementII.java
rename to leetcode/solution/src/MajorityElementII.java
index a7496b8..83a9cce 100644
--- a/solution/src/main/java/com/inuker/solution/MajorityElementII.java
+++ b/leetcode/solution/src/MajorityElementII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class MajorityElementII {
public List majorityElement(int[] nums) {
diff --git a/leetcode/solution/src/MaxAreaOfIsland.java b/leetcode/solution/src/MaxAreaOfIsland.java
new file mode 100644
index 0000000..9f01d6b
--- /dev/null
+++ b/leetcode/solution/src/MaxAreaOfIsland.java
@@ -0,0 +1,28 @@
+public class MaxAreaOfIsland {
+
+ public int maxAreaOfIsland(int[][] grid) {
+ int[] area = new int[1];
+ int max = 0;
+
+ for (int i = 0; i < grid.length; i++) {
+ for (int j = 0; j < grid[0].length; j++) {
+ area[0] = 0;
+ dfs(grid, i, j, area);
+ max = Math.max(max, area[0]);
+ }
+ }
+ return max;
+ }
+
+ private void dfs(int[][] grid, int i, int j, int[] count) {
+ if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != 1) {
+ return;
+ }
+ grid[i][j] = 0;
+ count[0]++;
+ dfs(grid, i + 1, j, count);
+ dfs(grid, i - 1, j, count);
+ dfs(grid, i, j + 1, count);
+ dfs(grid, i, j - 1, count);
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/MaxConsecutiveOnes.java b/leetcode/solution/src/MaxConsecutiveOnes.java
similarity index 76%
rename from solution/src/main/java/com/inuker/solution/MaxConsecutiveOnes.java
rename to leetcode/solution/src/MaxConsecutiveOnes.java
index d9215dd..f51f473 100644
--- a/solution/src/main/java/com/inuker/solution/MaxConsecutiveOnes.java
+++ b/leetcode/solution/src/MaxConsecutiveOnes.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class MaxConsecutiveOnes {
public int findMaxConsecutiveOnes(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/MaxConsecutiveOnesII.java b/leetcode/solution/src/MaxConsecutiveOnesII.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/MaxConsecutiveOnesII.java
rename to leetcode/solution/src/MaxConsecutiveOnesII.java
index a3b7867..b98a62a 100644
--- a/solution/src/main/java/com/inuker/solution/MaxConsecutiveOnesII.java
+++ b/leetcode/solution/src/MaxConsecutiveOnesII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.Queue;
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class MaxConsecutiveOnesII {
/**
diff --git a/solution/src/main/java/com/inuker/solution/MaxPointsOnALine.java b/leetcode/solution/src/MaxPointsOnALine.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/MaxPointsOnALine.java
rename to leetcode/solution/src/MaxPointsOnALine.java
index c2b6ac4..f77bcfd 100644
--- a/solution/src/main/java/com/inuker/solution/MaxPointsOnALine.java
+++ b/leetcode/solution/src/MaxPointsOnALine.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.awt.Point;
import java.util.HashMap;
-/**
- * Created by dingjikerbo on 16/12/7.
- */
-
/**
* TestCase
* 边界条件很多,比如有重复的点,有斜率无穷大的线
diff --git a/solution/src/main/java/com/inuker/solution/MaximalRectangle.java b/leetcode/solution/src/MaximalRectangle.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/MaximalRectangle.java
rename to leetcode/solution/src/MaximalRectangle.java
index 053904d..ddc77be 100644
--- a/solution/src/main/java/com/inuker/solution/MaximalRectangle.java
+++ b/leetcode/solution/src/MaximalRectangle.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
-
public abstract class MaximalRectangle {
// 耗时10ms,时间复杂度是O(mn),空间复杂度O(n)
diff --git a/solution/src/main/java/com/inuker/solution/MaximalSquare.java b/leetcode/solution/src/MaximalSquare.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/MaximalSquare.java
rename to leetcode/solution/src/MaximalSquare.java
index 7558aee..2000238 100644
--- a/solution/src/main/java/com/inuker/solution/MaximalSquare.java
+++ b/leetcode/solution/src/MaximalSquare.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/23.
- */
-
/**
* https://leetcode.com/articles/maximal-square/
*/
diff --git a/leetcode/solution/src/MaximumBinaryTree.java b/leetcode/solution/src/MaximumBinaryTree.java
new file mode 100644
index 0000000..767ca33
--- /dev/null
+++ b/leetcode/solution/src/MaximumBinaryTree.java
@@ -0,0 +1,25 @@
+public class MaximumBinaryTree {
+
+ /**
+ * 复杂度平均O(nlgn),最差O(n^2)
+ */
+ public TreeNode constructMaximumBinaryTree(int[] nums, int start, int end) {
+ if (start > end) {
+ return null;
+ }
+ int max = start;
+ for (int i = start + 1; i <= end; i++) {
+ if (nums[i] > nums[max]) {
+ max = i;
+ }
+ }
+ TreeNode root = new TreeNode(nums[max]);
+ root.left = constructMaximumBinaryTree(nums, start, max - 1);
+ root.right = constructMaximumBinaryTree(nums, max + 1, end);
+ return root;
+ }
+
+ public TreeNode constructMaximumBinaryTree(int[] nums) {
+ return constructMaximumBinaryTree(nums, 0, nums.length - 1);
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/MaximumDepthOfBinaryTree.java b/leetcode/solution/src/MaximumDepthOfBinaryTree.java
similarity index 53%
rename from solution/src/main/java/com/inuker/solution/MaximumDepthOfBinaryTree.java
rename to leetcode/solution/src/MaximumDepthOfBinaryTree.java
index 2e1f7c0..73fe2f7 100644
--- a/solution/src/main/java/com/inuker/solution/MaximumDepthOfBinaryTree.java
+++ b/leetcode/solution/src/MaximumDepthOfBinaryTree.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/11/28.
- */
-
public class MaximumDepthOfBinaryTree {
- private int maxDepth(TreeNode node) {
+ public int maxDepth(TreeNode node) {
if (node == null) {
return 0;
}
diff --git a/solution/src/main/java/com/inuker/solution/MaximumGap.java b/leetcode/solution/src/MaximumGap.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/MaximumGap.java
rename to leetcode/solution/src/MaximumGap.java
index e91a3ef..c151ae1 100644
--- a/solution/src/main/java/com/inuker/solution/MaximumGap.java
+++ b/leetcode/solution/src/MaximumGap.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Arrays;
-/**
- * Created by dingjikerbo on 16/12/8.
- */
-
public class MaximumGap {
/**
diff --git a/solution/src/main/java/com/inuker/solution/MaximumProductSubarray.java b/leetcode/solution/src/MaximumProductSubarray.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/MaximumProductSubarray.java
rename to leetcode/solution/src/MaximumProductSubarray.java
index 8839090..6c4d5a9 100644
--- a/solution/src/main/java/com/inuker/solution/MaximumProductSubarray.java
+++ b/leetcode/solution/src/MaximumProductSubarray.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/1/5.
- */
-
public class MaximumProductSubarray {
/**
diff --git a/solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java b/leetcode/solution/src/MaximumSizeSubarraySumEqualsK.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java
rename to leetcode/solution/src/MaximumSizeSubarraySumEqualsK.java
index f7663da..022112d 100644
--- a/solution/src/main/java/com/inuker/solution/MaximumSizeSubarraySumEqualsK.java
+++ b/leetcode/solution/src/MaximumSizeSubarraySumEqualsK.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.HashMap;
-/**
- * Created by dingjikerbo on 2016/11/19.
- */
-
public class MaximumSizeSubarraySumEqualsK {
// 耗时29ms,时间复杂度O(n)
diff --git a/solution/src/main/java/com/inuker/solution/MaximumSubarray.java b/leetcode/solution/src/MaximumSubarray.java
similarity index 58%
rename from solution/src/main/java/com/inuker/solution/MaximumSubarray.java
rename to leetcode/solution/src/MaximumSubarray.java
index bf81892..2c7d279 100644
--- a/solution/src/main/java/com/inuker/solution/MaximumSubarray.java
+++ b/leetcode/solution/src/MaximumSubarray.java
@@ -1,34 +1,24 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/15.
- */
-
public class MaximumSubarray {
// dp[i]表示包含第i个元素时的最大和
public int maxSubArray(int[] nums) {
- if (nums.length == 0) {
- return 0;
- }
int[] dp = new int[nums.length];
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
- dp[i] = nums[i];
- if (i > 0 && dp[i - 1] > 0) {
- dp[i] += dp[i - 1];
- }
+ dp[i] = Math.max(i > 0 ? dp[i - 1] + nums[i] : nums[i], nums[i]);
max = Math.max(max, dp[i]);
}
return max;
}
- public int maxSubArray2(int[] nums) {
+ /* 这里dp其实可以去掉,换成一个普通int变量即可
+ public int maxSubArray(int[] nums) {
int max = Integer.MIN_VALUE, prev = 0;
for (int i = 0; i < nums.length; i++) {
- prev = Math.max(nums[i], nums[i] + prev);
+ prev = Math.max(i > 0 ? prev + nums[i] : nums[i], nums[i]);
max = Math.max(max, prev);
}
return max;
}
+ */
}
diff --git a/solution/src/main/java/com/inuker/solution/MedianOfTwoSortedArrays.java b/leetcode/solution/src/MedianOfTwoSortedArrays.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/MedianOfTwoSortedArrays.java
rename to leetcode/solution/src/MedianOfTwoSortedArrays.java
index 9e7a4d5..1edb615 100644
--- a/solution/src/main/java/com/inuker/solution/MedianOfTwoSortedArrays.java
+++ b/leetcode/solution/src/MedianOfTwoSortedArrays.java
@@ -1,7 +1,4 @@
-package com.inuker.solution;
-
/**
- * Created by dingjikerbo on 16/12/20.
* https://leetcode.com/articles/median-of-two-sorted-arrays/
*/
@@ -27,7 +24,7 @@ private double findKth(int[] nums1, int start1, int end1, int[] nums2, int start
if (len1 > len2) {
return findKth(nums2, start2, end2, nums1, start1, end1, k);
} else if (len1 == 0) {
- return nums2[k - 1];
+ return nums2[start2 + k - 1];
} else if (k == 1) {
return Math.min(nums1[start1], nums2[start2]);
}
diff --git a/solution/src/main/java/com/inuker/solution/MeetingRooms.java b/leetcode/solution/src/MeetingRooms.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/MeetingRooms.java
rename to leetcode/solution/src/MeetingRooms.java
index 490e3c8..c2ae246 100644
--- a/solution/src/main/java/com/inuker/solution/MeetingRooms.java
+++ b/leetcode/solution/src/MeetingRooms.java
@@ -1,14 +1,6 @@
-package com.inuker.solution;
-
-import com.leetcode.library.Interval;
-
import java.util.Arrays;
import java.util.Comparator;
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
/**
* https://leetcode.com/articles/meeting-rooms/
*/
diff --git a/solution/src/main/java/com/inuker/solution/MeetingRoomsII.java b/leetcode/solution/src/MeetingRoomsII.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/MeetingRoomsII.java
rename to leetcode/solution/src/MeetingRoomsII.java
index 16540f1..a4281e8 100644
--- a/solution/src/main/java/com/inuker/solution/MeetingRoomsII.java
+++ b/leetcode/solution/src/MeetingRoomsII.java
@@ -1,19 +1,11 @@
-package com.inuker.solution;
-
-import com.leetcode.library.Interval;
-
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
public class MeetingRoomsII {
- // 耗时17ms,时间复杂度O(nlgn)
+ // 耗时7ms,时间复杂度O(nlgn)
public int minMeetingRooms(Interval[] intervals) {
Arrays.sort(intervals, new Comparator() {
@Override
diff --git a/solution/src/main/java/com/inuker/solution/MergeIntervals.java b/leetcode/solution/src/MergeIntervals.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/MergeIntervals.java
rename to leetcode/solution/src/MergeIntervals.java
index 7242531..ba5e827 100644
--- a/solution/src/main/java/com/inuker/solution/MergeIntervals.java
+++ b/leetcode/solution/src/MergeIntervals.java
@@ -1,14 +1,9 @@
-package com.inuker.solution;
-
-import com.leetcode.library.Interval;
-
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
/**
- * Created by dingjikerbo on 16/11/21.
* https://leetcode.com/articles/merge-intervals/
*/
diff --git a/leetcode/solution/src/MergeKSortedList.java b/leetcode/solution/src/MergeKSortedList.java
new file mode 100644
index 0000000..c046b9d
--- /dev/null
+++ b/leetcode/solution/src/MergeKSortedList.java
@@ -0,0 +1,81 @@
+import java.util.Comparator;
+import java.util.PriorityQueue;
+
+/**
+ * https://leetcode.com/articles/merge-k-sorted-list/
+ */
+
+public class MergeKSortedList {
+
+ // 耗时19ms
+ // 时间复杂度为O(knlgn),空间复杂度O(k)
+ /**
+ * 这里要注意lists中可能有node为null
+ */
+ public ListNode mergeKLists(ListNode[] lists) {
+ ListNode dummy = new ListNode(0), cur = dummy;
+
+ PriorityQueue queue = new PriorityQueue<>(new Comparator() {
+ @Override
+ public int compare(ListNode node1, ListNode node2) {
+ return node1.val - node2.val;
+ }
+ });
+
+ for (ListNode node : lists) {
+ if (node != null) {
+ queue.offer(node);
+ }
+ }
+
+ while (!queue.isEmpty()) {
+ ListNode node = queue.poll();
+ cur.next = node;
+ cur = cur.next;
+ if (node.next != null) {
+ queue.offer(node.next);
+ }
+ }
+
+ return dummy.next;
+ }
+
+ /**
+ * 第二种方法
+ * 时间复杂度O(Nlgk),空间O(l)
+ * 耗时10ms
+ */
+ public ListNode mergeKLists2(ListNode[] lists) {
+ return helper(lists, 0, lists.length - 1);
+ }
+
+ private ListNode helper(ListNode[] lists, int start, int end) {
+ if (start > end) {
+ return null;
+ }
+ if (start == end) {
+ return lists[start];
+ }
+ int mid = start + (end - start) / 2;
+ ListNode l1 = helper(lists, start, mid);
+ ListNode l2 = helper(lists, mid + 1, end);
+ return mergeTwoLists(l1, l2);
+ }
+
+ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
+ ListNode dummy = new ListNode(0);
+ ListNode p = l1, q = l2, cur = dummy;
+ for ( ; p != null && q != null; ) {
+ if (p.val < q.val) {
+ cur.next = p;
+ p = p.next;
+ } else {
+ cur.next = q;
+ q = q.next;
+ }
+ cur = cur.next;
+ }
+ cur.next = p != null ? p : q;
+ return dummy.next;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/MergeSortedArray.java b/leetcode/solution/src/MergeSortedArray.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/MergeSortedArray.java
rename to leetcode/solution/src/MergeSortedArray.java
index 1dfd673..d834f14 100644
--- a/solution/src/main/java/com/inuker/solution/MergeSortedArray.java
+++ b/leetcode/solution/src/MergeSortedArray.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/23.
- */
-
public class MergeSortedArray {
public void merge(int[] nums1, int m, int[] nums2, int n) {
diff --git a/leetcode/solution/src/MergeTwoBinaryTrees.java b/leetcode/solution/src/MergeTwoBinaryTrees.java
new file mode 100644
index 0000000..49cae76
--- /dev/null
+++ b/leetcode/solution/src/MergeTwoBinaryTrees.java
@@ -0,0 +1,13 @@
+public class MergeTwoBinaryTrees {
+
+ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
+ if (t1 == null)
+ return t2;
+ if (t2 == null)
+ return t1;
+ t1.val += t2.val;
+ t1.left = mergeTrees(t1.left, t2.left);
+ t1.right = mergeTrees(t1.right, t2.right);
+ return t1;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/MergeTwoSortedList.java b/leetcode/solution/src/MergeTwoSortedList.java
similarity index 81%
rename from solution/src/main/java/com/inuker/solution/MergeTwoSortedList.java
rename to leetcode/solution/src/MergeTwoSortedList.java
index 2b3e110..f006d7c 100644
--- a/solution/src/main/java/com/inuker/solution/MergeTwoSortedList.java
+++ b/leetcode/solution/src/MergeTwoSortedList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class MergeTwoSortedList {
// 耗时15ms
diff --git a/solution/src/main/java/com/inuker/solution/MinStack.java b/leetcode/solution/src/MinStack.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/MinStack.java
rename to leetcode/solution/src/MinStack.java
index 253ab94..ac8cf47 100644
--- a/solution/src/main/java/com/inuker/solution/MinStack.java
+++ b/leetcode/solution/src/MinStack.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Stack;
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class MinStack {
Stack mMinStack;
diff --git a/solution/src/main/java/com/inuker/solution/MinimumDepthOfBinaryTree.java b/leetcode/solution/src/MinimumDepthOfBinaryTree.java
similarity index 50%
rename from solution/src/main/java/com/inuker/solution/MinimumDepthOfBinaryTree.java
rename to leetcode/solution/src/MinimumDepthOfBinaryTree.java
index ceffc06..82ad4f4 100644
--- a/solution/src/main/java/com/inuker/solution/MinimumDepthOfBinaryTree.java
+++ b/leetcode/solution/src/MinimumDepthOfBinaryTree.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
-/**
- * Created by dingjikerbo on 16/11/28.
- */
-
public class MinimumDepthOfBinaryTree {
/**
@@ -24,31 +16,15 @@ public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
- // 如果一边空了,那这边深度为0,但是由于不是叶子节点,所以不能算,得看另一边。
+ if (root.left == null && root.right == null) {
+ return 1;
+ }
if (root.left == null) {
return minDepth(root.right) + 1;
- } else if (root.right == null) {
- return minDepth(root.left) + 1;
- } else {
- return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
- }
-
- /**
- public int minDepth(TreeNode root) {
- if (root == null) {
- return 0;
+ if (root.right == null) {
+ return minDepth(root.left) + 1;
}
- return helper(root);
+ return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
-
- private int helper(TreeNode root) {
- if (root == null) {
- return Integer.MAX_VALUE;
- }
- if (root.left == null && root.right == null) {
- return 1;
- }
- return Math.min(helper(root.left), helper(root.right)) + 1;
- }*/
}
diff --git a/solution/src/main/java/com/inuker/solution/MinimumHeightTrees.java b/leetcode/solution/src/MinimumHeightTrees.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/MinimumHeightTrees.java
rename to leetcode/solution/src/MinimumHeightTrees.java
index 8d51200..a231294 100644
--- a/solution/src/main/java/com/inuker/solution/MinimumHeightTrees.java
+++ b/leetcode/solution/src/MinimumHeightTrees.java
@@ -1,14 +1,4 @@
-package com.inuker.solution;
-
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
-/**
- * Created by liwentian on 2017/9/3.
- */
+import java.util.*;
/**
* 从叶子节点开始,一层层剥掉,直到最后剩下的节点就是所求深度最大的
diff --git a/solution/src/main/java/com/inuker/solution/MinimumSizeSubarraySum.java b/leetcode/solution/src/MinimumSizeSubarraySum.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/MinimumSizeSubarraySum.java
rename to leetcode/solution/src/MinimumSizeSubarraySum.java
index 4bf67a0..74245f5 100644
--- a/solution/src/main/java/com/inuker/solution/MinimumSizeSubarraySum.java
+++ b/leetcode/solution/src/MinimumSizeSubarraySum.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Arrays;
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class MinimumSizeSubarraySum {
/**
* 仍然采用滑动窗口的办法
diff --git a/solution/src/main/java/com/inuker/solution/MinimumWindowSubstring.java b/leetcode/solution/src/MinimumWindowSubstring.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/MinimumWindowSubstring.java
rename to leetcode/solution/src/MinimumWindowSubstring.java
index 6886d04..1d3cb68 100644
--- a/solution/src/main/java/com/inuker/solution/MinimumWindowSubstring.java
+++ b/leetcode/solution/src/MinimumWindowSubstring.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/21.
- */
-
public class MinimumWindowSubstring {
- // 耗时8ms,时间复杂度O(n)
+ // 耗时6ms,时间复杂度O(n)
public String minWindow(String s, String t) {
int[] tc = new int[256], sc = new int[256];
for (char c : t.toCharArray()) {
diff --git a/solution/src/main/java/com/inuker/solution/MissingNumber.java b/leetcode/solution/src/MissingNumber.java
similarity index 74%
rename from solution/src/main/java/com/inuker/solution/MissingNumber.java
rename to leetcode/solution/src/MissingNumber.java
index 89c7763..97c42e2 100644
--- a/solution/src/main/java/com/inuker/solution/MissingNumber.java
+++ b/leetcode/solution/src/MissingNumber.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class MissingNumber {
public int missingNumber(int[] nums) {
diff --git a/solution/src/main/java/com/inuker/solution/MissingRanges.java b/leetcode/solution/src/MissingRanges.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/MissingRanges.java
rename to leetcode/solution/src/MissingRanges.java
index d27c689..4380310 100644
--- a/solution/src/main/java/com/inuker/solution/MissingRanges.java
+++ b/leetcode/solution/src/MissingRanges.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/11.
- */
-
/**
* TestCases
* [], lower=1, upper=1
diff --git a/leetcode/solution/src/MostCommonWord.java b/leetcode/solution/src/MostCommonWord.java
new file mode 100644
index 0000000..31649cf
--- /dev/null
+++ b/leetcode/solution/src/MostCommonWord.java
@@ -0,0 +1,44 @@
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+
+public class MostCommonWord {
+
+ /**
+ * 要注意结尾的单词
+ */
+ public String mostCommonWord(String paragraph, String[] banned) {
+ HashMap map = new HashMap<>();
+ HashSet bannes = new HashSet<>(Arrays.asList(banned));
+
+ String result = null;
+ int count = 0;
+
+ boolean enterWord = false;
+ for (int i = 0, j = 0; j <= paragraph.length(); j++) {
+ char c = j < paragraph.length() ? paragraph.charAt(j) : '.';
+ if (Character.isAlphabetic(c)) {
+ if (!enterWord) {
+ enterWord = true;
+ i = j;
+ }
+ } else {
+ if (enterWord) {
+ enterWord = false;
+ String word = paragraph.substring(i, j).toLowerCase();
+ if (!bannes.contains(word)) {
+ int cnt = map.getOrDefault(word, 0) + 1;
+ map.put(word, cnt);
+
+ if (cnt > count) {
+ count = cnt;
+ result = word;
+ }
+ }
+ }
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/MostFrequentSubtreeSum.java b/leetcode/solution/src/MostFrequentSubtreeSum.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/MostFrequentSubtreeSum.java
rename to leetcode/solution/src/MostFrequentSubtreeSum.java
index 43558b0..ab746d4 100644
--- a/solution/src/main/java/com/inuker/solution/MostFrequentSubtreeSum.java
+++ b/leetcode/solution/src/MostFrequentSubtreeSum.java
@@ -1,15 +1,7 @@
-package com.inuker.solution;
-
-import com.leetcode.library.TreeNode;
-
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by liwentian on 17/8/3.
- */
-
/**
* 这题算法比较直观
*/
diff --git a/solution/src/main/java/com/inuker/solution/MoveZeroes.java b/leetcode/solution/src/MoveZeroes.java
similarity index 67%
rename from solution/src/main/java/com/inuker/solution/MoveZeroes.java
rename to leetcode/solution/src/MoveZeroes.java
index 038c848..c93b7b5 100644
--- a/solution/src/main/java/com/inuker/solution/MoveZeroes.java
+++ b/leetcode/solution/src/MoveZeroes.java
@@ -1,23 +1,19 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/19.
- */
-
/**
* https://leetcode.com/articles/move-zeroes/
*/
public class MoveZeroes {
/**
- * 要保持顺序,写的次数最少
+ * 要保持顺序
*/
public void moveZeroes(int[] nums) {
- for (int i = 0, j = 0; j < nums.length; j++) {
- if (nums[j] != 0) {
- swap(nums, i++, j);
+ int j = 0;
+ for (int i = 0; i < nums.length; i++) {
+ if (nums[i] != 0) {
+ nums[j++] = nums[i];
}
}
+ for ( ; j < nums.length; nums[j++] = 0);
}
private void swap(int[] nums, int i, int j) {
@@ -28,6 +24,7 @@ private void swap(int[] nums, int i, int j) {
/**
* 如果不要求保持顺序,且写的次数最少
+ * 从两头开始往中间走,如果左边是0,右边非0,则交换,写的次数是0的个数乘2
*/
public void moveZeroes2(int[] nums) {
for (int i = 0, j = nums.length - 1; j > i; ) {
diff --git a/solution/src/main/java/com/inuker/solution/MovingAverage.java b/leetcode/solution/src/MovingAverage.java
similarity index 83%
rename from solution/src/main/java/com/inuker/solution/MovingAverage.java
rename to leetcode/solution/src/MovingAverage.java
index 4d7eb68..3916cfa 100644
--- a/solution/src/main/java/com/inuker/solution/MovingAverage.java
+++ b/leetcode/solution/src/MovingAverage.java
@@ -1,12 +1,5 @@
-package com.inuker.solution;
-
import java.util.Deque;
import java.util.LinkedList;
-import java.util.List;
-
-/**
- * Created by dingjikerbo on 16/12/4.
- */
public class MovingAverage {
diff --git a/solution/src/main/java/com/inuker/solution/MultiplyStrings.java b/leetcode/solution/src/MultiplyStrings.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/MultiplyStrings.java
rename to leetcode/solution/src/MultiplyStrings.java
index e596665..114cc6e 100644
--- a/solution/src/main/java/com/inuker/solution/MultiplyStrings.java
+++ b/leetcode/solution/src/MultiplyStrings.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/22.
- */
-
public class MultiplyStrings {
public String multiply(String num1, String num2) {
diff --git a/solution/src/main/java/com/inuker/solution/MyQueue.java b/leetcode/solution/src/MyQueue.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/MyQueue.java
rename to leetcode/solution/src/MyQueue.java
index fbe1a71..6341b73 100644
--- a/solution/src/main/java/com/inuker/solution/MyQueue.java
+++ b/leetcode/solution/src/MyQueue.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.Stack;
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
/**
* https://leetcode.com/articles/implement-queue-using-stacks/
*/
diff --git a/solution/src/main/java/com/inuker/solution/MyStack.java b/leetcode/solution/src/MyStack.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/MyStack.java
rename to leetcode/solution/src/MyStack.java
index 073f29b..e22ec04 100644
--- a/solution/src/main/java/com/inuker/solution/MyStack.java
+++ b/leetcode/solution/src/MyStack.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.Queue;
-/**
- * Created by dingjikerbo on 16/12/10.
- */
-
/**
* https://leetcode.com/articles/implement-stack-using-queues/
*/
diff --git a/solution/src/main/java/com/inuker/solution/NQueens.java b/leetcode/solution/src/NQueens.java
similarity index 96%
rename from solution/src/main/java/com/inuker/solution/NQueens.java
rename to leetcode/solution/src/NQueens.java
index 030b708..43444a2 100644
--- a/solution/src/main/java/com/inuker/solution/NQueens.java
+++ b/leetcode/solution/src/NQueens.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 17/1/12.
- */
-
public class NQueens {
public List> solveNQueens(int n) {
diff --git a/solution/src/main/java/com/inuker/solution/NQueensII.java b/leetcode/solution/src/NQueensII.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/NQueensII.java
rename to leetcode/solution/src/NQueensII.java
index c2fb4e6..d4ae0d5 100644
--- a/solution/src/main/java/com/inuker/solution/NQueensII.java
+++ b/leetcode/solution/src/NQueensII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 17/1/12.
- */
-
public class NQueensII {
private int total;
diff --git a/solution/src/main/java/com/inuker/solution/NestedListWeightSum.java b/leetcode/solution/src/NestedListWeightSum.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/NestedListWeightSum.java
rename to leetcode/solution/src/NestedListWeightSum.java
index b0b2580..606fa13 100644
--- a/solution/src/main/java/com/inuker/solution/NestedListWeightSum.java
+++ b/leetcode/solution/src/NestedListWeightSum.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
/**
* https://leetcode.com/articles/nested-list-weight-sum/
*/
diff --git a/solution/src/main/java/com/inuker/solution/NestedListWeightSumII.java b/leetcode/solution/src/NestedListWeightSumII.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/NestedListWeightSumII.java
rename to leetcode/solution/src/NestedListWeightSumII.java
index a304283..59e2bbf 100644
--- a/solution/src/main/java/com/inuker/solution/NestedListWeightSumII.java
+++ b/leetcode/solution/src/NestedListWeightSumII.java
@@ -1,11 +1,5 @@
-package com.inuker.solution;
-
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
/**
* https://leetcode.com/articles/nested-list-weight-sum/
*/
diff --git a/leetcode/solution/src/NextClosestTime.java b/leetcode/solution/src/NextClosestTime.java
new file mode 100644
index 0000000..7d75759
--- /dev/null
+++ b/leetcode/solution/src/NextClosestTime.java
@@ -0,0 +1,62 @@
+import java.util.Arrays;
+import java.util.TreeSet;
+
+public class NextClosestTime {
+
+ /**
+ * 整体思路如下:
+ * 首先对输入的时间所包含的数要统计一下,并从小到大排序
+ * 接下来从时间的高位开始,寻找可替代的更大的数,如果找不到,就看下一位,如果找到了就替换成该数,判断时间是否有效
+ * 如果有效,则将该位往上全部替换为全局最小的数,并返回结果。否则还原该位,并继续看下一位。
+ */
+ public String nextClosestTime(String time) {
+ int[] number = new int[4];
+
+ time = time.substring(0, 2) + time.substring(3);
+
+ for (int i = 0; i < time.length(); i++) {
+ number[i] = time.charAt(i) - '0';
+ }
+
+ Arrays.sort(number);
+
+ StringBuilder sb = new StringBuilder(time);
+
+ int i;
+ for (i = 3; i >= 0; i--) {
+ char c = time.charAt(i);
+ int k = nextNumber(number, c - '0');
+ if (k >= 0) {
+ sb.setCharAt(i, (char) (k + '0'));
+ if (isValid(sb)) {
+ break;
+ }
+ sb.setCharAt(i, c);
+ }
+ }
+ for (i++; i < 4; i++) {
+ sb.setCharAt(i, (char) (number[0] + '0'));
+ }
+ sb.insert(2, ':');
+ return sb.toString();
+ }
+
+ // 判断时间是否有效
+ private boolean isValid(StringBuilder sb) {
+ boolean flag1 = sb.charAt(0) < '2' || (sb.charAt(0) == '2' && sb.charAt(1) <= '3');
+ boolean flag2 = sb.charAt(2) < '5' || (sb.charAt(2) == '5' && sb.charAt(3) <= '9');
+ return flag1 && flag2;
+ }
+
+ /**
+ * 找比n更大的数,number是一个升序的数组
+ */
+ private int nextNumber(int[] number, int n) {
+ for (int i = 0; i < number.length; i++) {
+ if (number[i] > n) {
+ return number[i];
+ }
+ }
+ return -1;
+ }
+}
diff --git a/solution/src/main/java/com/inuker/solution/NextPermutation.java b/leetcode/solution/src/NextPermutation.java
similarity index 59%
rename from solution/src/main/java/com/inuker/solution/NextPermutation.java
rename to leetcode/solution/src/NextPermutation.java
index a4fce65..3c5efb2 100644
--- a/solution/src/main/java/com/inuker/solution/NextPermutation.java
+++ b/leetcode/solution/src/NextPermutation.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
/**
* https://leetcode.com/articles/next-permutation/
*/
@@ -14,26 +8,24 @@ public class NextPermutation {
* 然后从升序中找到一个大于nums[i-1]的最小数与i-1交换,然后对升序整体revert为降序即可
*/
public void nextPermutation(int[] nums) {
- int i = nums.length - 1;
+ int i, j;
- for ( ; i >= 0; i--) {
- if (i > 0 && nums[i - 1] < nums[i]) {
+ for (i = nums.length - 1; i > 0; i--) {
+ if (nums[i - 1] < nums[i]) {
break;
}
}
- if (i < 0) {
- revert(nums, 0, nums.length - 1);
- return;
- }
-
- for (int j = nums.length - 1; j >= i; j--) {
- if (nums[j] > nums[i - 1]) {
- swap(nums, j, i - 1);
- revert(nums, i, nums.length - 1);
- break;
+ if (i > 0) {
+ for (j = i; j < nums.length; j++) {
+ if (nums[j] <= nums[i - 1]) {
+ break;
+ }
}
+ swap(nums, i - 1, j - 1);
}
+
+ revert(nums, i, nums.length - 1);
}
private void swap(int[] nums, int left, int right) {
diff --git a/solution/src/main/java/com/inuker/solution/NumArray.java b/leetcode/solution/src/NumArray.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/NumArray.java
rename to leetcode/solution/src/NumArray.java
index 9005296..9659f01 100644
--- a/solution/src/main/java/com/inuker/solution/NumArray.java
+++ b/leetcode/solution/src/NumArray.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/26.
- */
-
/**
* https://leetcode.com/articles/range-sum-query-immutable/
*/
diff --git a/solution/src/main/java/com/inuker/solution/NumArrayII.java b/leetcode/solution/src/NumArrayII.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/NumArrayII.java
rename to leetcode/solution/src/NumArrayII.java
index 6b8e206..153d026 100644
--- a/solution/src/main/java/com/inuker/solution/NumArrayII.java
+++ b/leetcode/solution/src/NumArrayII.java
@@ -1,8 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/26.
- */
/**
* 这题可以用segment tree或binary index tree(bit),两者时间复杂度都是lgn,不过segment tree因为用到树,
* 而bit用的是数组,所以bit更省空间。
diff --git a/solution/src/main/java/com/inuker/solution/NumMatrix.java b/leetcode/solution/src/NumMatrix.java
similarity index 92%
rename from solution/src/main/java/com/inuker/solution/NumMatrix.java
rename to leetcode/solution/src/NumMatrix.java
index 6661c58..f5d5323 100644
--- a/solution/src/main/java/com/inuker/solution/NumMatrix.java
+++ b/leetcode/solution/src/NumMatrix.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/26.
- */
-
/**
* https://leetcode.com/articles/range-sum-query-2d-immutable/
*/
diff --git a/solution/src/main/java/com/inuker/solution/NumMatrixII.java b/leetcode/solution/src/NumMatrixII.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/NumMatrixII.java
rename to leetcode/solution/src/NumMatrixII.java
index 20de7c5..ce0c8c9 100644
--- a/solution/src/main/java/com/inuker/solution/NumMatrixII.java
+++ b/leetcode/solution/src/NumMatrixII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/27.
- */
-
/**
* 这道题有两种方法
* 1,2D BIT
diff --git a/solution/src/main/java/com/inuker/solution/NumberOfConnectedComponents.java b/leetcode/solution/src/NumberOfConnectedComponents.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/NumberOfConnectedComponents.java
rename to leetcode/solution/src/NumberOfConnectedComponents.java
index 9ba9593..07446e3 100644
--- a/solution/src/main/java/com/inuker/solution/NumberOfConnectedComponents.java
+++ b/leetcode/solution/src/NumberOfConnectedComponents.java
@@ -1,14 +1,8 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
-/**
- * Created by dingjikerbo on 17/1/2.
- */
-
public class NumberOfConnectedComponents {
// 8ms
diff --git a/solution/src/main/java/com/inuker/solution/NumberOfDigitOne.java b/leetcode/solution/src/NumberOfDigitOne.java
similarity index 91%
rename from solution/src/main/java/com/inuker/solution/NumberOfDigitOne.java
rename to leetcode/solution/src/NumberOfDigitOne.java
index dce32db..fe8b2b3 100644
--- a/solution/src/main/java/com/inuker/solution/NumberOfDigitOne.java
+++ b/leetcode/solution/src/NumberOfDigitOne.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/21.
- */
-
public class NumberOfDigitOne {
/**
diff --git a/solution/src/main/java/com/inuker/solution/NumberOfIslands.java b/leetcode/solution/src/NumberOfIslands.java
similarity index 95%
rename from solution/src/main/java/com/inuker/solution/NumberOfIslands.java
rename to leetcode/solution/src/NumberOfIslands.java
index 3f1f93d..7e699e8 100644
--- a/solution/src/main/java/com/inuker/solution/NumberOfIslands.java
+++ b/leetcode/solution/src/NumberOfIslands.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/11/16.
- */
-
import java.util.LinkedList;
import java.util.Queue;
diff --git a/solution/src/main/java/com/inuker/solution/NumberOfIslandsII.java b/leetcode/solution/src/NumberOfIslandsII.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/NumberOfIslandsII.java
rename to leetcode/solution/src/NumberOfIslandsII.java
index a835dcf..2a4d191 100644
--- a/solution/src/main/java/com/inuker/solution/NumberOfIslandsII.java
+++ b/leetcode/solution/src/NumberOfIslandsII.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/11.
- */
-
/**
* 经典的UF(Union Find)问题,可翻阅《算法》1.5章
* UF时间复杂度和节点个数有关
diff --git a/solution/src/main/java/com/inuker/solution/OddEvenLinkedList.java b/leetcode/solution/src/OddEvenLinkedList.java
similarity index 83%
rename from solution/src/main/java/com/inuker/solution/OddEvenLinkedList.java
rename to leetcode/solution/src/OddEvenLinkedList.java
index 9e6608f..dbd91a7 100644
--- a/solution/src/main/java/com/inuker/solution/OddEvenLinkedList.java
+++ b/leetcode/solution/src/OddEvenLinkedList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by dingjikerbo on 16/12/7.
- */
-
public class OddEvenLinkedList {
public ListNode oddEvenList(ListNode head) {
diff --git a/solution/src/main/java/com/inuker/solution/OneEditDistance.java b/leetcode/solution/src/OneEditDistance.java
similarity index 90%
rename from solution/src/main/java/com/inuker/solution/OneEditDistance.java
rename to leetcode/solution/src/OneEditDistance.java
index 6063d64..3786f06 100644
--- a/solution/src/main/java/com/inuker/solution/OneEditDistance.java
+++ b/leetcode/solution/src/OneEditDistance.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/11/20.
- */
-
public class OneEditDistance {
/**
* 最容易错的是结尾的条件sL != tL
diff --git a/solution/src/main/java/com/inuker/solution/PaintHouse.java b/leetcode/solution/src/PaintHouse.java
similarity index 85%
rename from solution/src/main/java/com/inuker/solution/PaintHouse.java
rename to leetcode/solution/src/PaintHouse.java
index 3360784..2e4d1b8 100644
--- a/solution/src/main/java/com/inuker/solution/PaintHouse.java
+++ b/leetcode/solution/src/PaintHouse.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/18.
- */
-
public class PaintHouse {
public int minCost(int[][] costs) {
diff --git a/solution/src/main/java/com/inuker/solution/PaintHouseII.java b/leetcode/solution/src/PaintHouseII.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/PaintHouseII.java
rename to leetcode/solution/src/PaintHouseII.java
index d1bf76a..751d23e 100644
--- a/solution/src/main/java/com/inuker/solution/PaintHouseII.java
+++ b/leetcode/solution/src/PaintHouseII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 16/12/18.
- */
-
public class PaintHouseII {
/**
diff --git a/solution/src/main/java/com/inuker/solution/PalindromeLinkedList.java b/leetcode/solution/src/PalindromeLinkedList.java
similarity index 88%
rename from solution/src/main/java/com/inuker/solution/PalindromeLinkedList.java
rename to leetcode/solution/src/PalindromeLinkedList.java
index e8b91e2..d6128d4 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromeLinkedList.java
+++ b/leetcode/solution/src/PalindromeLinkedList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by dingjikerbo on 2016/11/17.
- */
-
public class PalindromeLinkedList {
// 耗时2ms
diff --git a/solution/src/main/java/com/inuker/solution/PalindromeNumber.java b/leetcode/solution/src/PalindromeNumber.java
similarity index 53%
rename from solution/src/main/java/com/inuker/solution/PalindromeNumber.java
rename to leetcode/solution/src/PalindromeNumber.java
index 63e8338..2fd2171 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromeNumber.java
+++ b/leetcode/solution/src/PalindromeNumber.java
@@ -1,11 +1,6 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class PalindromeNumber {
+ // 耗时101ms
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
@@ -22,4 +17,19 @@ public boolean isPalindrome(int x) {
return true;
}
+
+ /**
+ * 直接给数倒过来看是否相等
+ * 耗时103ms
+ */
+ public boolean isPalindrome2(int x) {
+ if (x < 0) {
+ return false;
+ }
+ int n = 0, m = x;
+ for ( ; x > 0; x /= 10) {
+ n = n * 10 + x % 10;
+ }
+ return n == m;
+ }
}
diff --git a/solution/src/main/java/com/inuker/solution/PalindromePairs.java b/leetcode/solution/src/PalindromePairs.java
similarity index 97%
rename from solution/src/main/java/com/inuker/solution/PalindromePairs.java
rename to leetcode/solution/src/PalindromePairs.java
index c46f38c..8f9c3b8 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromePairs.java
+++ b/leetcode/solution/src/PalindromePairs.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class PalindromePairs {
public List> palindromePairs(String[] words) {
diff --git a/solution/src/main/java/com/inuker/solution/PalindromePartitioning.java b/leetcode/solution/src/PalindromePartitioning.java
similarity index 93%
rename from solution/src/main/java/com/inuker/solution/PalindromePartitioning.java
rename to leetcode/solution/src/PalindromePartitioning.java
index f3013be..3693b58 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromePartitioning.java
+++ b/leetcode/solution/src/PalindromePartitioning.java
@@ -1,13 +1,7 @@
-package com.inuker.solution;
-
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class PalindromePartitioning {
/**
diff --git a/solution/src/main/java/com/inuker/solution/PalindromePartitioningII.java b/leetcode/solution/src/PalindromePartitioningII.java
similarity index 89%
rename from solution/src/main/java/com/inuker/solution/PalindromePartitioningII.java
rename to leetcode/solution/src/PalindromePartitioningII.java
index 5cce6af..3d14b4d 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromePartitioningII.java
+++ b/leetcode/solution/src/PalindromePartitioningII.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/17.
- */
-
public class PalindromePartitioningII {
/**
diff --git a/solution/src/main/java/com/inuker/solution/PalindromePermutation.java b/leetcode/solution/src/PalindromePermutation.java
similarity index 84%
rename from solution/src/main/java/com/inuker/solution/PalindromePermutation.java
rename to leetcode/solution/src/PalindromePermutation.java
index 39cf299..0f02c8f 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromePermutation.java
+++ b/leetcode/solution/src/PalindromePermutation.java
@@ -1,9 +1,3 @@
-package com.inuker.solution;
-
-/**
- * Created by dingjikerbo on 2016/12/19.
- */
-
public class PalindromePermutation {
public boolean canPermutePalindrome(String s) {
diff --git a/solution/src/main/java/com/inuker/solution/PalindromePermutationII.java b/leetcode/solution/src/PalindromePermutationII.java
similarity index 94%
rename from solution/src/main/java/com/inuker/solution/PalindromePermutationII.java
rename to leetcode/solution/src/PalindromePermutationII.java
index 2e08cb0..1e92297 100644
--- a/solution/src/main/java/com/inuker/solution/PalindromePermutationII.java
+++ b/leetcode/solution/src/PalindromePermutationII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.LinkedList;
import java.util.List;
-/**
- * Created by dingjikerbo on 2016/12/19.
- */
-
/**
* https://leetcode.com/articles/palindrome-permutation-ii/
*/
diff --git a/solution/src/main/java/com/inuker/solution/PartitionList.java b/leetcode/solution/src/PartitionList.java
similarity index 80%
rename from solution/src/main/java/com/inuker/solution/PartitionList.java
rename to leetcode/solution/src/PartitionList.java
index 68d8200..f03168f 100644
--- a/solution/src/main/java/com/inuker/solution/PartitionList.java
+++ b/leetcode/solution/src/PartitionList.java
@@ -1,11 +1,3 @@
-package com.inuker.solution;
-
-import com.leetcode.library.ListNode;
-
-/**
- * Created by liwentian on 2017/9/11.
- */
-
public class PartitionList {
ListNode partition(ListNode head, int x) {
diff --git a/solution/src/main/java/com/inuker/solution/PascalTriangleII.java b/leetcode/solution/src/PascalTriangleII.java
similarity index 86%
rename from solution/src/main/java/com/inuker/solution/PascalTriangleII.java
rename to leetcode/solution/src/PascalTriangleII.java
index c448c85..da420e5 100644
--- a/solution/src/main/java/com/inuker/solution/PascalTriangleII.java
+++ b/leetcode/solution/src/PascalTriangleII.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 17/8/12.
- */
-
public class PascalTriangleII {
public List getRow(int rowIndex) {
diff --git a/solution/src/main/java/com/inuker/solution/PascalsTriangle.java b/leetcode/solution/src/PascalsTriangle.java
similarity index 87%
rename from solution/src/main/java/com/inuker/solution/PascalsTriangle.java
rename to leetcode/solution/src/PascalsTriangle.java
index 93bdb63..00c940e 100644
--- a/solution/src/main/java/com/inuker/solution/PascalsTriangle.java
+++ b/leetcode/solution/src/PascalsTriangle.java
@@ -1,12 +1,6 @@
-package com.inuker.solution;
-
import java.util.ArrayList;
import java.util.List;
-/**
- * Created by liwentian on 2017/9/10.
- */
-
public class PascalsTriangle {
public List