-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticeArea.js
204 lines (172 loc) · 4.51 KB
/
PracticeArea.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Return early on empty input
if (!strs.length) return '';
// Loop through the letters of the first word
for (let i = 0; i <= strs[0].length; i++) {
// Check if this character is present in the same position of every string
if (!strs.every((string) => string[i] === strs[0][i])) {
// If not, return the string up to and including the previous character
return strs[0].slice(0, i);
}
}
return strs[0];
let aa = ['tom','tony','jerry'];
console.log(aa[1][1])
let str = '(([]))' ;
var isVaild = function(str){
let current, stackArr = [];
for (let i = 0; i<str.length; i++){
if(str[i]==='(' || str[i]==='[' || str[i]==='{'){
stackArr.push(str[i]);
}
else if( str[i]===')'){
if (stackArr.pop() !== '('){
return false;
}
}
else if( str[i]===']'){
if (stackArr.pop() !== '['){
return false;
}
}
else if( str[i]==='}'){
if (stackArr.pop() !== '{'){
return false;
}
}
}
return true;
}
let twoSum = (nums, target) =>{
for(let i = 0; i<nums.length; i++){
for(let j = i+1; j<nums.length-1; j++){
if(nums[i]+nums[j] == target){
return [i,j];
} else {
return null;
}
}
}
}
const long = (strs) =>{
if(!strs.length) return "";
let firstStr = strs[0];
for(let i = 0; i< firstStr.length; i++){
if(!strs.every(ele => {return ele[i] === firstStr[i]})){
return firstStr.slice(0,i);
}
}
}
let strs = [""];
console.log(strs.length)
/**
* @param {string} s
* @return {boolean}
*/
var isBILL = function(str) {
let current, stackArr = [];
for (let i = 0; i<str.length; i++){
if(str[i]==='(' || str[i]==='[' || str[i]==='{'){
stackArr.push(str[i]);
}
else if( str[i]===')'){
if (stackArr.pop() !== '('){
return false;
}
}
else if( str[i]===']'){
if (stackArr.pop() !== '['){
return false;
}
}
else if( str[i]==='}'){
if (stackArr.pop() !== '{'){
return false;
}
}
}
return stackArr.length === 0;
};
var isValid = function(s) {
let stack = [];
for(let i=0; i < s.length ; i++){
if(s[i] === '(' || s[i] === '[' || s[i] === '{'){
stack.push(s[i]);
} else if (s[i] === ")"){
if(stack.pop() !== "(" ){
console.log(stack)
return false;
}
} else if (s[i] === "]"){
if(stack.pop() !== "[" ){
return false;
}
} else if (s[i] === "}"){
if(stack.pop() !== "{" ){
return false;
}
}
}
return s.length === 0;
};
let s= "()";
console.log(isValid(s));
console.log(isBILL(s));
let l1 = [1,2,3,5,6]
let l2 = [3,4,6,7,8]
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
let question = (l1,l2) =>{
while(l1 && l2){
if(l1.val <= l2.val){
console.log(l1.val)
result.next = new ListNode(l1.val)
result = result.next
l1 = l1.next
}
console.log(l1)
console.log(result)
console.log(result.next)
}}
console.log(question(l1.val))
var mergeTwoLists = function(list1, list2) {
let result = new ListNode(), l3 = result;
while(list1 && list2){
if(list1.val <= list2.val){
result.next = new ListNode(list1.val)
result = result.next
list1 = list1.next
}
else if(list2.val < list1.val){
result.next =new ListNode(list2.val)
result = result.next
list2 = list2.next
}
}
while(list1){
result.next = new ListNode(list1.val)
result = result.next
list1 = list1.next
}
while(list2){
result.next = new ListNode(list2.val)
result = result.next
list2 = list2.next
}
return l3.next
};
function makeAdder(x){
return function(y){
return x+y
}
}
var add5 = makeAdder(5)
console.log(makeAdder(5))
console.log(add5(2))
let bar = [1,8,3,6,89,11,54]
console.log(bar.push(2));
console.log(bar)
push()
shift()
unshift()