pull/6/head
yuanguangxin 5 years ago
parent f306cb3332
commit cded4a22b7

@ -2,7 +2,7 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/src/栈相关/q32_最长有效括号/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/分治法/q34_在排序数组中查找元素的第一个和最后一个位置/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$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
</list>
@ -80,13 +80,13 @@
<recent name="$PROJECT_DIR$/src/q206/f1" />
</key>
</component>
<component name="RunManager" selected="Application.Solution (6)">
<configuration name="Solution (2)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="双指针遍历.q121_买卖股票的最佳时机.Solution" />
<component name="RunManager" selected="Application.Solution (1)">
<configuration name="Solution (1)" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="分治法.q34_在排序数组中查找元素的第一个和最后一个位置.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="双指针遍历.q121_买卖股票的最佳时机.*" />
<option name="PATTERN" value="分治法.q34_在排序数组中查找元素的第一个和最后一个位置.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
@ -146,19 +146,6 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="回溯法.q46_全排列.f1.Solution" />
<module name="LeetCode" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="回溯法.q46_全排列.f1.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Solution" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="双指针遍历.q42_接雨水.Solution" />
<module name="LeetCode" />
@ -213,11 +200,11 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Solution (1)" />
<item itemvalue="Application.Solution (6)" />
<item itemvalue="Application.Solution (5)" />
<item itemvalue="Application.Solution (4)" />
<item itemvalue="Application.Solution (3)" />
<item itemvalue="Application.Solution (2)" />
</list>
</recent_temporary>
</component>
@ -277,14 +264,7 @@
<workItem from="1585037684584" duration="753000" />
<workItem from="1585041029099" duration="2193000" />
<workItem from="1585219892120" duration="917000" />
<workItem from="1585223904505" duration="2190000" />
</task>
<task id="LOCAL-00006" summary="add q21">
<created>1580409713059</created>
<option name="number" value="00006" />
<option name="presentableId" value="LOCAL-00006" />
<option name="project" value="LOCAL" />
<updated>1580409713059</updated>
<workItem from="1585223904505" duration="5402000" />
</task>
<task id="LOCAL-00007" summary="add q26/q43">
<created>1580496640692</created>
@ -622,7 +602,14 @@
<option name="project" value="LOCAL" />
<updated>1585220219370</updated>
</task>
<option name="localTasksCounter" value="55" />
<task id="LOCAL-00055" summary="add q32">
<created>1585226292787</created>
<option name="number" value="00055" />
<option name="presentableId" value="LOCAL-00055" />
<option name="project" value="LOCAL" />
<updated>1585226292788</updated>
</task>
<option name="localTasksCounter" value="56" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -675,7 +662,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="add q1277" />
<MESSAGE value="add q42" />
<MESSAGE value="update md" />
<MESSAGE value="update q10/q746" />
@ -700,7 +686,8 @@
<MESSAGE value="add q945" />
<MESSAGE value="add q70/q104" />
<MESSAGE value="add q19" />
<option name="LAST_COMMIT_MESSAGE" value="add q19" />
<MESSAGE value="add q32" />
<option name="LAST_COMMIT_MESSAGE" value="add q32" />
</component>
<component name="WindowStateProjectService">
<state x="320" y="115" key="#Inspections" timestamp="1581061018990">
@ -715,55 +702,55 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<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="1585220219177">
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1585226292610">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1585220219177" />
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1585226292610" />
<state x="143" y="78" width="1152" height="720" key="DiffContextDialog" timestamp="1585027870766">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="143" y="78" width="1152" height="720" key="DiffContextDialog/0.0.1440.900@0.0.1440.900" timestamp="1585027870766" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.23.1440.793@0.23.1440.793" timestamp="1582714686712" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog/0.23.1440.797@0.23.1440.797" timestamp="1581927787180" />
<state width="1398" height="245" key="GridCell.Tab.0.bottom" timestamp="1585224543864">
<state width="1398" height="245" key="GridCell.Tab.0.bottom" timestamp="1585229512716">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1585224543864" />
<state width="1398" height="177" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1581533895880" />
<state width="1398" height="245" key="GridCell.Tab.0.center" timestamp="1585224543863">
<state width="1398" height="245" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1585229512716" />
<state width="1398" height="245" key="GridCell.Tab.0.bottom/0.23.1440.797@0.23.1440.797" timestamp="1585228590486" />
<state width="1398" height="245" key="GridCell.Tab.0.center" timestamp="1585229512714">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1585224543863" />
<state width="1398" height="177" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1581533895878" />
<state width="1398" height="245" key="GridCell.Tab.0.left" timestamp="1585224543862">
<state width="1398" height="245" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1585229512714" />
<state width="1398" height="245" key="GridCell.Tab.0.center/0.23.1440.797@0.23.1440.797" timestamp="1585228590484" />
<state width="1398" height="245" key="GridCell.Tab.0.left" timestamp="1585229512714">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1585224543862" />
<state width="1398" height="177" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1581533895878" />
<state width="1398" height="245" key="GridCell.Tab.0.right" timestamp="1585224543863">
<state width="1398" height="245" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1585229512714" />
<state width="1398" height="245" key="GridCell.Tab.0.left/0.23.1440.797@0.23.1440.797" timestamp="1585228590483" />
<state width="1398" height="245" key="GridCell.Tab.0.right" timestamp="1585229512715">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1585224543863" />
<state width="1398" height="177" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1581533895879" />
<state width="1398" height="204" key="GridCell.Tab.1.bottom" timestamp="1581745235895">
<state width="1398" height="245" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1585229512715" />
<state width="1398" height="245" key="GridCell.Tab.0.right/0.23.1440.797@0.23.1440.797" timestamp="1585228590484" />
<state width="1398" height="303" key="GridCell.Tab.1.bottom" timestamp="1585228962390">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="204" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1581745235895" />
<state width="1398" height="303" key="GridCell.Tab.1.bottom/0.0.1440.900@0.0.1440.900" timestamp="1585228962390" />
<state width="1398" height="304" key="GridCell.Tab.1.bottom/0.23.1440.797@0.23.1440.797" timestamp="1580408030266" />
<state width="1398" height="204" key="GridCell.Tab.1.center" timestamp="1581745235894">
<state width="1398" height="303" key="GridCell.Tab.1.center" timestamp="1585228962389">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="204" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1581745235894" />
<state width="1398" height="303" key="GridCell.Tab.1.center/0.0.1440.900@0.0.1440.900" timestamp="1585228962389" />
<state width="1398" height="304" key="GridCell.Tab.1.center/0.23.1440.797@0.23.1440.797" timestamp="1580408030265" />
<state width="1398" height="204" key="GridCell.Tab.1.left" timestamp="1581745235893">
<state width="1398" height="303" key="GridCell.Tab.1.left" timestamp="1585228962388">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="204" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1581745235893" />
<state width="1398" height="303" key="GridCell.Tab.1.left/0.0.1440.900@0.0.1440.900" timestamp="1585228962388" />
<state width="1398" height="304" key="GridCell.Tab.1.left/0.23.1440.797@0.23.1440.797" timestamp="1580408030262" />
<state width="1398" height="204" key="GridCell.Tab.1.right" timestamp="1581745235894">
<state width="1398" height="303" key="GridCell.Tab.1.right" timestamp="1585228962389">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state width="1398" height="204" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1581745235894" />
<state width="1398" height="303" key="GridCell.Tab.1.right/0.0.1440.900@0.0.1440.900" timestamp="1585228962389" />
<state width="1398" height="304" key="GridCell.Tab.1.right/0.23.1440.797@0.23.1440.797" timestamp="1580408030266" />
<state x="431" y="173" key="MultipleFileMergeDialog" timestamp="1582971939233">
<screen x="0" y="0" width="1440" height="900" />
@ -773,10 +760,10 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="221" y="63" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1585223890241" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1585220220623">
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1585226293961">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1585220220623" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1585226293961" />
<state x="100" y="100" width="1240" height="700" key="com.intellij.history.integration.ui.views.DirectoryHistoryDialog" timestamp="1581744794182">
<screen x="0" y="23" width="1440" height="797" />
</state>

@ -79,6 +79,7 @@
* [q23_合并K个排序链表](/src/分治法/q23_合并K个排序链表)
* [q33_搜索旋转排序数组](/src/分治法/q33_搜索旋转排序数组)
* [q34_在排序数组中查找元素的第一个和最后一个位置](/src/分治法/q34_在排序数组中查找元素的第一个和最后一个位置)
### 动态规划

@ -0,0 +1,60 @@
package .q34_;
/**
* o(log(n))
*/
public class Solution {
public int[] searchRange(int[] nums, int target) {
if (nums == null || nums.length < 1) {
return new int[]{-1, -1};
}
int midIndex = find(0, nums.length - 1, nums, target);
int[] rs = new int[2];
rs[0] = midIndex;
rs[1] = midIndex;
if (midIndex == -1) {
return rs;
}
while (nums[rs[0]] == target && rs[0] > 0) {
int temp = find(0, rs[0] - 1, nums, target);
if (temp == -1) {
break;
} else {
rs[0] = temp;
}
}
while (nums[rs[1]] == target && rs[1] < nums.length - 1) {
int temp = find(rs[1] + 1, nums.length - 1, nums, target);
if (temp == -1) {
break;
} else {
rs[1] = temp;
}
}
return rs;
}
public int find(int beginIndex, int endIndex, int[] nums, int target) {
if (beginIndex == endIndex) {
if (nums[beginIndex] == target) {
return beginIndex;
} else {
return -1;
}
}
int mid = (endIndex - beginIndex) / 2 + beginIndex;
if (nums[mid] > target) {
return find(beginIndex, mid, nums, target);
} else if (nums[mid] < target) {
return find(mid + 1, endIndex, nums, target);
} else {
return mid;
}
}
public static void main(String[] args) {
new Solution().searchRange(new int[]{2, 2}, 2);
}
}
Loading…
Cancel
Save