-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava2.java
More file actions
executable file
·130 lines (116 loc) · 2.48 KB
/
Java2.java
File metadata and controls
executable file
·130 lines (116 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.example;
import java.util.Arrays;
import java.util.Stack;
public class Lab {
/*
1. Return the nth fibonacci number
f(0) = 0
f(1) = 1
f(10) = 55
*/
public static int fibonacci(int n) {
int result = 0;
if (n<=0){
return 0;
}
else if(n==1){
return 1;
}
else{
return fibonacci(n-1) + fibonacci(n-2);
}
}
/*
2. Sort array of integers
f([2,4,5,1,3,1]) = [1,1,2,3,4,5]
Don't use built-in sort() method... that would be lame.
*/
public static int[] sort(int[] array) {
for (i=0; i<array.length; i++) {
for (j=i+1; j<array.length; j++){
if (array[j]<array[i]){
int t = array[i];
array[i] = array[j];
array[j]=t;
}
}
}
return array;
}
/*
3. Return the factorial of n
f(0) = 1
f(1) = 1
f(3) = 6
*/
public static int factorial(int n) {
if (n==0 || n==1){
return 1;
}
if (n<0){
IllegalArgumentException e = new IllegalArgumentException();
throw e;
}
else {
return n * factorial(n-1);
}
}
/*
4. Rotate left
Given array, rotate left n times and return array
f([1,2,3,4,5], 1) = [2,3,4,5,1]
f([1,2,3,4,5], 6) = [2,3,4,5,1]
f([1,2,3,4,5], 3) = [4,5,1,2,3]
*/
public static int[] rotateLeft(int[] array, int n) {
int shift = n%array.length;
for (i=0; i<shift; i++) {
int t = array[0];
for (j=0; j<array.length-1; j++) {
array[j] = array[j + 1];
}
array[array.length-1] = t;
}
return array;
}
/*
5. Balanced Brackets
A bracket is any one of the following: (, ), {, }, [, or ]
The following are balanced brackets:
()
()()
(())
({[]})
The following are NOT balanced brackets:
(
)
(()
([)]
Return true if balanced
Return false if not balanced
*/
public static boolean balancedBrackets(String bracketsString) {
Stack<Character> st = new Stack();
// can pop, push, peek, empty to see if it is empty
boolean result = true;
int i=0;
while( result && i<bracketsString.length()){
Character s = bracketsString.charAt(i);
if (s == '(' || s == '{' || s == '{'){
st.push(s);
}
else if (st.empty()) {
return false;
}
else {
Character c = st.pop();
result =( (c=='(' && s ==')' )|| (c=='{' && s =='}' ) || (c=='[' && s ==']' ));
if (result == false) {
return false;
}
}
i++;
}
return (st.empty());
}
}