pull/6/head
yuanguangxin 5 years ago
parent 4dfbf67583
commit 832b75721a

@ -2,8 +2,7 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment=""> <list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/q53/q1/Solution.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/q54/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/q53/q2/Solution.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -74,7 +73,7 @@
<recent name="$PROJECT_DIR$/src/q206/f1" /> <recent name="$PROJECT_DIR$/src/q206/f1" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Application.Solution (5)"> <component name="RunManager" selected="Application.Solution">
<configuration name="Solution (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Solution (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q46.f2.Solution" /> <option name="MAIN_CLASS_NAME" value="q46.f2.Solution" />
<module name="LeetCode" /> <module name="LeetCode" />
@ -101,19 +100,6 @@
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="Solution (3)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q21.f1.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="q21.f1.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution (4)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Solution (4)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q26.Solution" /> <option name="MAIN_CLASS_NAME" value="q26.Solution" />
<module name="LeetCode" /> <module name="LeetCode" />
@ -141,11 +127,11 @@
</method> </method>
</configuration> </configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="q53.q1.Solution" /> <option name="MAIN_CLASS_NAME" value="q54.Solution" />
<module name="LeetCode" /> <module name="LeetCode" />
<extension name="coverage"> <extension name="coverage">
<pattern> <pattern>
<option name="PATTERN" value="q53.q1.*" /> <option name="PATTERN" value="q54.*" />
<option name="ENABLED" value="true" /> <option name="ENABLED" value="true" />
</pattern> </pattern>
</extension> </extension>
@ -181,11 +167,11 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Application.Solution" />
<item itemvalue="Application.Solution (5)" /> <item itemvalue="Application.Solution (5)" />
<item itemvalue="Application.Solution (5)" /> <item itemvalue="Application.Solution (5)" />
<item itemvalue="Application.Solution (1)" /> <item itemvalue="Application.Solution (1)" />
<item itemvalue="Application.Solution (2)" /> <item itemvalue="Application.Solution (2)" />
<item itemvalue="Application.Solution (4)" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
@ -225,7 +211,7 @@
<workItem from="1579759214129" duration="10251000" /> <workItem from="1579759214129" duration="10251000" />
<workItem from="1580044287086" duration="50376000" /> <workItem from="1580044287086" duration="50376000" />
<workItem from="1580562331089" duration="8289000" /> <workItem from="1580562331089" duration="8289000" />
<workItem from="1580626297484" duration="10002000" /> <workItem from="1580626297484" duration="13148000" />
</task> </task>
<task id="LOCAL-00001" summary="add q5/q7/q172"> <task id="LOCAL-00001" summary="add q5/q7/q172">
<created>1580045439607</created> <created>1580045439607</created>
@ -283,7 +269,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1580585083883</updated> <updated>1580585083883</updated>
</task> </task>
<option name="localTasksCounter" value="9" /> <task id="LOCAL-00009" summary="add q53">
<created>1580714190854</created>
<option name="number" value="00009" />
<option name="presentableId" value="LOCAL-00009" />
<option name="project" value="LOCAL" />
<updated>1580714190854</updated>
</task>
<option name="localTasksCounter" value="10" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -298,40 +291,41 @@
<MESSAGE value="add q21" /> <MESSAGE value="add q21" />
<MESSAGE value="add q26/q43" /> <MESSAGE value="add q26/q43" />
<MESSAGE value="add q46" /> <MESSAGE value="add q46" />
<option name="LAST_COMMIT_MESSAGE" value="add q46" /> <MESSAGE value="add q53" />
<option name="LAST_COMMIT_MESSAGE" value="add q53" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser" timestamp="1578548165321"> <state x="533" y="166" key="#com.intellij.ide.util.MemberChooser" timestamp="1578548165321">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" /> <state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" />
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1580585083630"> <state x="303" y="50" key="CommitChangelistDialog2" timestamp="1580714190621">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1580585083630" /> <state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1580714190621" />
<state x="144" y="78" key="DiffContextDialog" timestamp="1580585072145"> <state x="144" y="78" key="DiffContextDialog" timestamp="1580585072145">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state x="144" y="78" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1580585072145" /> <state x="144" y="78" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1580585072145" />
<state width="1398" height="204" key="GridCell.Tab.0.bottom" timestamp="1580714110161"> <state width="1398" height="204" key="GridCell.Tab.0.bottom" timestamp="1580753043493">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state width="1398" height="204" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580714110161" /> <state width="1398" height="204" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1580753043493" />
<state width="1398" height="204" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1580706887662" /> <state width="1398" height="204" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1580706887662" />
<state width="1398" height="204" key="GridCell.Tab.0.center" timestamp="1580714110159"> <state width="1398" height="204" key="GridCell.Tab.0.center" timestamp="1580753043491">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state width="1398" height="204" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1580714110159" /> <state width="1398" height="204" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1580753043491" />
<state width="1398" height="204" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1580706887660" /> <state width="1398" height="204" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1580706887660" />
<state width="1398" height="204" key="GridCell.Tab.0.left" timestamp="1580714110158"> <state width="1398" height="204" key="GridCell.Tab.0.left" timestamp="1580753043490">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state width="1398" height="204" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1580714110158" /> <state width="1398" height="204" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1580753043490" />
<state width="1398" height="204" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1580706887659" /> <state width="1398" height="204" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1580706887659" />
<state width="1398" height="204" key="GridCell.Tab.0.right" timestamp="1580714110160"> <state width="1398" height="204" key="GridCell.Tab.0.right" timestamp="1580753043492">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state width="1398" height="204" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1580714110160" /> <state width="1398" height="204" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1580753043492" />
<state width="1398" height="204" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1580706887661" /> <state width="1398" height="204" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1580706887661" />
<state width="1398" height="261" key="GridCell.Tab.1.bottom" timestamp="1580626092580"> <state width="1398" height="261" key="GridCell.Tab.1.bottom" timestamp="1580626092580">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
@ -357,10 +351,10 @@
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state x="221" y="88" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1580562273160" /> <state x="221" y="88" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1580562273160" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1580585085542"> <state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1580714192376">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1580585085542" /> <state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1580714192376" />
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1578496174047"> <state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1578496174047">
<screen x="0" y="0" width="1440" height="900" /> <screen x="0" y="0" width="1440" height="900" />
</state> </state>

@ -0,0 +1,72 @@
package q54;
import java.util.ArrayList;
import java.util.List;
/**
* o(n)
*/
public class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> rs = new ArrayList<>();
if (matrix.length == 0 || matrix[0].length == 0) {
return rs;
}
int m = matrix.length;
int n = matrix[0].length;
boolean[][] visited = new boolean[m][n];
int i = 0;
int j = 0;
int direction = 1;
while (true) {
if (i < 0 || j < 0 || i == m || j == n || visited[i][j]) {
break;
}
rs.add(matrix[i][j]);
visited[i][j] = true;
switch (direction) {
case 1:
if (j + 1 == n || visited[i][j + 1]) {
i++;
direction = 2;
} else {
j++;
}
break;
case 2:
if (i + 1 == m || visited[i + 1][j]) {
j--;
direction = 3;
} else {
i++;
}
break;
case 3:
if (j == 0 || visited[i][j - 1]) {
i--;
direction = 4;
} else {
j--;
}
break;
case 4:
if (visited[i - 1][j]) {
j++;
direction = 1;
} else {
i--;
}
break;
default:
break;
}
}
return rs;
}
public static void main(String[] args) {
System.out.println(new Solution().spiralOrder(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}));
}
}
Loading…
Cancel
Save