Skip to content

Commit 136db8c

Browse files
committed
第一周作业格式变更
1 parent 41f138a commit 136db8c

12 files changed

Lines changed: 472 additions & 0 deletions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int findMin(int[] nums) {
3+
int left = 0,right = nums.length-1,end = nums.length-1,mid;
4+
if(end < 0){
5+
return -1;
6+
}else if(end == 0){
7+
return nums[0];
8+
}
9+
10+
if(nums[0]<nums[end]){
11+
return nums[0];
12+
}
13+
14+
int maxIndex = -1;
15+
16+
while(left <= right){
17+
mid = left + ((right - left) >> 1);
18+
if(nums[mid] > nums[mid+1]){
19+
maxIndex = mid;
20+
break;
21+
}else if(nums[mid] < nums[0]){
22+
right = mid -1;
23+
}else{
24+
left = mid +1;
25+
}
26+
}
27+
return nums[maxIndex + 1];
28+
}
29+
}

Week_01/id_77/leetcode_20_077.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
Stack<Character> stack = new Stack<Character>();
4+
if(s.length() == 0){
5+
return true;
6+
}
7+
boolean flag = true;
8+
char ch;
9+
for(int i=0;i<s.length();i++){
10+
ch = s.charAt(i);
11+
try {
12+
if (ch == ')') {
13+
if ('('==stack.pop()) {
14+
continue;
15+
} else {
16+
return false;
17+
}
18+
} else if (ch==']') {
19+
if ('['==stack.pop()) {
20+
continue;
21+
} else {
22+
return false;
23+
}
24+
} else if (ch =='}') {
25+
if ('{'==stack.pop()) {
26+
continue;
27+
} else {
28+
return false;
29+
}
30+
}else{
31+
stack.push(ch);
32+
}
33+
} catch (Exception e) {
34+
return false;
35+
}
36+
37+
}
38+
39+
if(stack.size()>0){
40+
return false;
41+
}else {
42+
return flag;
43+
}
44+
}
45+
}

Week_01/id_77/leetcode_21_077.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11+
ListNode p1 = l1;
12+
ListNode p2 = l2;
13+
14+
if(l1==null){
15+
return l2;
16+
}else if(l2==null){
17+
return l1;
18+
}
19+
ListNode listNew ;
20+
if(p1.val<p2.val){
21+
listNew = new ListNode(p1.val);
22+
p1 = p1.next;
23+
}else{
24+
listNew = new ListNode(p2.val);
25+
p2 = p2.next;
26+
}
27+
ListNode p=listNew;
28+
29+
30+
while(p1!=null && p2!=null){
31+
if(p1.val<p2.val){
32+
p.next = new ListNode(p1.val);
33+
p1 = p1.next;
34+
}else{
35+
p.next = new ListNode(p2.val);
36+
p2 = p2.next;
37+
}
38+
p = p.next;
39+
40+
}
41+
42+
if(p1==null){
43+
p.next = p2;
44+
}else{
45+
p.next = p1;
46+
}
47+
48+
return listNew;
49+
50+
}
51+
}

Week_01/id_77/leetcode_24_077.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode swapPairs(ListNode head) {
11+
if(head == null || head.next == null){
12+
return head;
13+
}
14+
ListNode slowPointer = head;
15+
ListNode fastPointer = head.next;
16+
ListNode p=fastPointer.next;
17+
18+
fastPointer.next = slowPointer;
19+
slowPointer.next =p;
20+
21+
if(p == null){
22+
return fastPointer;
23+
}
24+
head = fastPointer;
25+
ListNode headPointer = fastPointer.next;
26+
27+
slowPointer = p;
28+
fastPointer = p.next;
29+
30+
while(slowPointer != null && fastPointer !=null){
31+
p=fastPointer.next;
32+
fastPointer.next = slowPointer;
33+
slowPointer.next =p;
34+
headPointer.next = fastPointer;
35+
headPointer = fastPointer.next;
36+
slowPointer = p;
37+
if(p!=null){
38+
fastPointer = p.next;
39+
}else{
40+
fastPointer= null;
41+
}
42+
}
43+
return head;
44+
}
45+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode reverseKGroup(ListNode head, int k) {
11+
12+
if(head == null){
13+
return null;
14+
}
15+
16+
//记录头节点
17+
ListNode headPointer = head;
18+
//记录尾节点
19+
ListNode tailPointer = head;
20+
//记录节点个数
21+
int n = 0;
22+
while(tailPointer != null){
23+
n++;
24+
if(n == k){
25+
//k以后的节点递归处理
26+
ListNode tmpTail = reverseKGroup(tailPointer.next,k);
27+
//反转前k个节点 并链接尾节点
28+
reverseK(headPointer,k).next = tmpTail;
29+
break;
30+
}
31+
tailPointer = tailPointer.next;
32+
}
33+
34+
if(n < k){
35+
//不足k个节点 直接返回头节点
36+
return headPointer;
37+
}else{
38+
//有k个节点 返回反转后的头节点 即原来的尾节点
39+
return tailPointer;
40+
}
41+
42+
43+
}
44+
45+
public ListNode reverseK(ListNode head, int k){
46+
if(k == 1){
47+
return head;
48+
}
49+
ListNode headNew = reverseK(head.next,k-1);
50+
headNew.next = head;
51+
head.next = null;
52+
53+
return head;
54+
}
55+
56+
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
3+
public int search(int[] nums, int target) {
4+
int left = 0,right = nums.length-1,end = nums.length-1,mid;
5+
if(end < 0){
6+
return -1;
7+
}else if(end == 0){
8+
return nums[0] == target ? 0 : -1;
9+
}
10+
11+
if(nums[0]<nums[end]){
12+
return binarySearch(nums,target,0 ,end);
13+
}
14+
15+
int maxIndex = -1;
16+
//查找最大值索引
17+
while(left <= right){
18+
mid = left + ((right - left) >> 1);
19+
if(nums[mid] > nums[mid+1]){
20+
maxIndex = mid;
21+
break;
22+
}else if(nums[mid] < nums[0]){
23+
right = mid -1;
24+
}else{
25+
left = mid +1;
26+
}
27+
}
28+
29+
//比较最大值 进行区间二分查找
30+
if(target < nums[0]){
31+
return binarySearch(nums,target,maxIndex+1 ,end) ;
32+
}else{
33+
return binarySearch(nums,target,0 ,maxIndex) ;
34+
}
35+
36+
}
37+
38+
public int binarySearch(int[] nums,int target,int left ,int right){
39+
if(left>right){
40+
return -1;
41+
}
42+
int mid = left + ((right - left) >> 1);
43+
if(nums[mid] > target){
44+
return binarySearch(nums,target,left,mid-1);
45+
}else if(nums[mid] < target){
46+
return binarySearch(nums,target,mid + 1,right);
47+
}else{
48+
return mid;
49+
}
50+
}
51+
}

Week_01/id_77/leetcode_33_077.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
3+
public int search(int[] nums, int target) {
4+
int left = 0,right = nums.length-1,end = nums.length-1,mid;
5+
if(end < 0){
6+
return -1;
7+
}else if(end == 0){
8+
return nums[0] == target ? 0 : -1;
9+
}
10+
11+
if(nums[0]<nums[end]){
12+
return binarySearch(nums,target,0 ,end);
13+
}
14+
15+
int maxIndex = -1;
16+
//查找最大值索引
17+
while(left <= right){
18+
mid = left + ((right - left) >> 1);
19+
if(nums[mid] > nums[mid+1]){
20+
maxIndex = mid;
21+
break;
22+
}else if(nums[mid] < nums[0]){
23+
right = mid -1;
24+
}else{
25+
left = mid +1;
26+
}
27+
}
28+
29+
//比较最大值 进行区间二分查找
30+
if(target < nums[0]){
31+
return binarySearch(nums,target,maxIndex+1 ,end) ;
32+
}else{
33+
return binarySearch(nums,target,0 ,maxIndex) ;
34+
}
35+
36+
}
37+
38+
public int binarySearch(int[] nums,int target,int left ,int right){
39+
if(left>right){
40+
return -1;
41+
}
42+
int mid = left + ((right - left) >> 1);
43+
if(nums[mid] > target){
44+
return binarySearch(nums,target,left,mid-1);
45+
}else if(nums[mid] < target){
46+
return binarySearch(nums,target,mid + 1,right);
47+
}else{
48+
return mid;
49+
}
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
// 默认初始值
3+
private static final Integer VALUED_EFAULT = Integer.valueOf(-1);
4+
5+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
6+
7+
Stack<Integer> stack = new Stack<Integer>();
8+
//利用哨兵来去除判空
9+
stack.push(Integer.MAX_VALUE);
10+
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
11+
int[] arr = new int[nums1.length];
12+
for(Integer node:nums2){
13+
while( node > stack.peek()){
14+
map.put(stack.pop(),node);
15+
}
16+
stack.push(node);
17+
}
18+
19+
for(int i = 0;i < nums1.length;i++){
20+
//获取为空时添加默认值
21+
arr[i] = map.getOrDefault(nums1[i],VALUED_EFAULT);
22+
}
23+
return arr;
24+
25+
}
26+
}

0 commit comments

Comments
 (0)