Skip to content

Commit dc4ac66

Browse files
author
digitspace
committed
Week 01算法题
1 parent 7e93078 commit dc4ac66

File tree

9 files changed

+433
-0
lines changed

9 files changed

+433
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/*
3+
* 189.Rotate Array
4+
* 旋转数组
5+
*/
6+
public class LeetCode_189_569 {
7+
8+
public static void main(String[] args) {
9+
int[] nums = { 1,2,3,4,5,6,7 };
10+
int k = 4;
11+
(new LeetCode_189_569()).new Solution().rotate(nums,k);
12+
for( int i = 0; i < nums.length; i++ ){
13+
System.out.println(nums[i]);
14+
}
15+
}
16+
17+
class Solution {
18+
public void rotate(int[] nums, int k) {
19+
k = k % nums.length;
20+
for (int i = 0, count = 0; count < nums.length; i++) {
21+
int source = i;
22+
int sourceValue = nums[source];
23+
do {
24+
int dest = (source + k) % nums.length;
25+
int temp = nums[dest];
26+
nums[dest] = sourceValue;
27+
source = dest;
28+
sourceValue = temp;
29+
count++;
30+
} while (source != i);
31+
}
32+
}
33+
}
34+
}
35+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/*
5+
* 1. Two Sum
6+
* 两数之和
7+
*/
8+
public class LeetCode_1_569 {
9+
10+
public static void main(String[] args) {
11+
// TODO Auto-generated method stub
12+
int[] nums = { 12,2,7,11,15 };
13+
int target = 9;
14+
int[] n = (new LeetCode_1_569()).new Solution().twoSum(nums, target);
15+
for( int i = 0; i < n.length; i++ ){
16+
System.out.println(n[i]);
17+
}
18+
}
19+
20+
class Solution {
21+
public int[] twoSum(int[] nums, int target) {
22+
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
23+
for (int i = 0; i < nums.length; i++) {
24+
int want = target - nums[i];
25+
if( map.containsKey(want) ) {
26+
return new int[] { map.get(want), i };
27+
}
28+
map.put(nums[i], i);
29+
}
30+
return null;
31+
}
32+
}
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* 21. Merge Two Sorted Lists
3+
* 合并两个有序链表
4+
*/
5+
public class LeetCode_21_569 {
6+
7+
public static void main(String[] args) {
8+
LeetCode_21_569 lc = new LeetCode_21_569();
9+
ListNode ln13 = lc.createNewNode(4, null);
10+
ListNode ln12 = lc.createNewNode(2, ln13);
11+
ListNode ln11 = lc.createNewNode(1, ln12);
12+
13+
ListNode ln23 = lc.createNewNode(4, null);
14+
ListNode ln22 = lc.createNewNode(3, ln23);
15+
ListNode ln21 = lc.createNewNode(1, ln22);
16+
17+
ListNode node = lc.new Solution().mergeTwoLists(ln11,ln21);
18+
while (node != null) {
19+
System.out.println(node.val);
20+
node = node.next;
21+
}
22+
23+
}
24+
25+
public ListNode createNewNode( int i, ListNode next ) {
26+
ListNode node = new ListNode(i);
27+
node.next = next;
28+
return node;
29+
}
30+
31+
32+
public class ListNode {
33+
int val;
34+
ListNode next;
35+
ListNode(int x) { val = x; }
36+
}
37+
38+
class Solution {
39+
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
40+
ListNode tempNode = new ListNode(0);
41+
ListNode currentNode = tempNode;
42+
while (l1 != null && l2 != null) {
43+
currentNode.next = (l1.val < l2.val) ? l1 : l2;
44+
ListNode n = (l1.val < l2.val) ? (l1 = l1.next) : (l2 = l2.next);
45+
currentNode = currentNode.next;
46+
}
47+
ListNode nonEmpty = (l1 != null) ? l1 : l2;
48+
currentNode.next = nonEmpty;
49+
return tempNode.next;
50+
}
51+
}
52+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* 26. Remove Duplicates from Sorted Array
3+
* 删除排序数组中的重复项
4+
*/
5+
public class LeetCode_26_569 {
6+
public static void main( String[] argv) {
7+
int[] nums = { 0,0,1,1,1,2,2,3,3,4,4 };
8+
int n = (new LeetCode_26_569()).new Solution().removeDuplicates(nums);
9+
for( int i = 0; i < n; i++ ){
10+
System.out.println(nums[i]);
11+
}
12+
}
13+
14+
class Solution {
15+
public int removeDuplicates(int[] nums) {
16+
if (nums.length <= 1)
17+
return nums.length;
18+
19+
int validIndex = 1;
20+
int lastNum = nums[0];
21+
for (int i = 1; i < nums.length; i++) {
22+
if (nums[i] != lastNum) {
23+
nums[validIndex++] = nums[i];
24+
lastNum = nums[i];
25+
}
26+
}
27+
return validIndex;
28+
}
29+
}
30+
}
31+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
/*
3+
* 283. Move Zeroes
4+
* 移动零
5+
*/
6+
public class LeetCode_283_569 {
7+
8+
public static void main(String[] args) {
9+
int[] nums = { 0,1,0,3,12,0 };
10+
int target = 9;
11+
(new LeetCode_283_569()).new Solution().moveZeroes(nums);
12+
for( int i = 0; i < nums.length; i++ ){
13+
System.out.println(nums[i]);
14+
}
15+
}
16+
17+
class Solution {
18+
public void moveZeroes(int[] nums) {
19+
int validIndex = 0;
20+
for ( int i = 0; i < nums.length; i++ ) {
21+
if ( nums[i] != 0 ) {
22+
nums[validIndex++] = nums[i];
23+
nums[i] = 0;
24+
}
25+
}
26+
}
27+
}
28+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* 42. Trapping Rain Water
3+
* 接雨水
4+
*/
5+
public class LeetCode_42_569 {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
int[] nums = { 2,0,2 };
10+
int n = (new LeetCode_42_569()).new Solution().trap(nums);
11+
System.out.println(n);
12+
}
13+
14+
class Solution {
15+
/*
16+
* 按行算水量,行数等于高度数组的最大值,但这个算法对于高度高的数组超时,比较垃圾
17+
*/
18+
public int trap(int[] height) {
19+
int water = 0;
20+
int rows = 0;
21+
for (int i:height) {
22+
rows = Math.max(rows, i);
23+
}
24+
//遍历每行
25+
for ( int i = 1; i <= rows; i++ ) {
26+
int rowWater = 0;
27+
int left = -1;
28+
int right = -1;
29+
for ( int j = 0; j < height.length; j++ ) {
30+
if ( height[j] >= i ) {
31+
if ( left < 0 ) { //第一个left
32+
left = j;
33+
}else {
34+
if ( j == left + 1 ) {
35+
left = j;
36+
}else {
37+
right = j;
38+
rowWater = rowWater + (right - left - 1);
39+
left = right;
40+
right = -1;
41+
}
42+
}
43+
}
44+
}
45+
water += rowWater;
46+
}
47+
return water;
48+
}
49+
}
50+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* 641. Design Circular Deque
3+
* 设计循环双端队列
4+
*/
5+
public class LeetCode_641_569 {
6+
7+
public static void main(String[] args) {
8+
9+
MyCircularDeque circularDeque = new MyCircularDeque(8);
10+
circularDeque.insertFront(5);
11+
circularDeque.getFront();
12+
circularDeque.isEmpty();
13+
circularDeque.deleteFront();
14+
circularDeque.insertLast(3);
15+
circularDeque.getRear();
16+
circularDeque.insertLast(7);
17+
circularDeque.insertFront(7);
18+
circularDeque.deleteLast();
19+
circularDeque.insertLast(4);
20+
circularDeque.isEmpty();
21+
22+
23+
24+
25+
26+
27+
28+
/*circularDeque.insertFront(1);
29+
circularDeque.insertLast(2);
30+
circularDeque.getFront();
31+
circularDeque.insertLast(3);
32+
circularDeque.getFront();
33+
circularDeque.insertFront(5);
34+
circularDeque.getRear();
35+
circularDeque.getFront();
36+
circularDeque.getFront();
37+
circularDeque.deleteLast();
38+
circularDeque.getRear();*/
39+
40+
41+
/* circularDeque.insertLast(1); // 返回 true
42+
circularDeque.insertLast(2); // 返回 true
43+
circularDeque.insertFront(3); // 返回 true
44+
circularDeque.insertFront(4); // 已经满了,返回 false
45+
circularDeque.getRear(); // 返回 2
46+
circularDeque.isFull(); // 返回 true
47+
circularDeque.deleteLast(); // 返回 true
48+
circularDeque.insertFront(4); // 返回 true
49+
circularDeque.getFront(); // 返回 4
50+
*/
51+
}
52+
53+
54+
}
55+
56+
class MyCircularDeque {
57+
58+
class Node {
59+
Node prev;
60+
int value;
61+
Node next;
62+
63+
Node( Node prev, int value, Node next) {
64+
this.prev = prev;
65+
this.value = value;
66+
this.next = next;
67+
}
68+
}
69+
70+
int maxSize;
71+
int size;
72+
Node head;
73+
Node tail;
74+
75+
public MyCircularDeque(int k) {
76+
this.maxSize = k;
77+
}
78+
79+
public boolean insertFront(int value) {
80+
if ( size == maxSize )
81+
return false;
82+
Node n = new Node( null, value, head );
83+
if ( head == null )
84+
tail = n;
85+
else
86+
head.prev = n;
87+
head = n;
88+
size++;
89+
return true;
90+
}
91+
92+
public boolean insertLast(int value) {
93+
if ( size == maxSize )
94+
return false;
95+
Node n = new Node( tail, value, null );
96+
if ( tail == null )
97+
head = n;
98+
else
99+
tail.next = n;
100+
tail = n;
101+
size++;
102+
return true;
103+
}
104+
105+
public boolean deleteFront() {
106+
if ( head == null )
107+
return false;
108+
Node n = head;
109+
Node next = head.next;
110+
n.next = null;
111+
head = next;
112+
if ( head != null )
113+
head.prev = null;
114+
else
115+
tail = null;
116+
size--;
117+
return true;
118+
}
119+
120+
public boolean deleteLast() {
121+
if ( tail == null )
122+
return false;
123+
Node n = tail;
124+
Node prev = tail.prev;
125+
n.prev = null;
126+
tail = prev;
127+
if ( tail != null )
128+
tail.next = null;
129+
else
130+
head = null;
131+
size--;
132+
return true;
133+
}
134+
135+
public int getFront() {
136+
return (size==0) ? -1 : head.value;
137+
}
138+
139+
public int getRear() {
140+
return (size==0) ? -1 : tail.value;
141+
}
142+
143+
public boolean isEmpty() {
144+
return (size == 0);
145+
}
146+
147+
public boolean isFull() {
148+
return (size == maxSize);
149+
}
150+
}

0 commit comments

Comments
 (0)