-
Notifications
You must be signed in to change notification settings - Fork 0
/
180622.js
191 lines (127 loc) · 5.42 KB
/
180622.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
// 1. Password Check - Binary to String
// https://www.codewars.com/kata/password-check-binary-to-string
function decodePass( passArr, bin ){
// Code here
var binArr = bin.split(" ");
var result = [];
for (var i=0; i < binArr.length; i++){
result.push(String.fromCharCode(parseInt(binArr[i],2)));
};
var pass = result.join("");
return passArr.includes(pass) ? pass : false;
};
console.log(decodePass('password123', 'admin1'));
console.log(decodePass('password321', 'admin'));
// 2. Move Zeroes
// Given an array nums,
// write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
for(var i = nums.length; i--;){
if(nums[i] === 0){
nums.splice(i,1);
nums.push(0);
} else {
nums.sort(function(a, b) {return a - b});
}
}
return nums;
};
console.log(moveZeroes([0, 1, 0, 3, 12]));
// Example:
// Input: [0,1,0,3,12]
// Output: [1,3,12,0,0]
// Note:
// You must do this in-place without making a copy of the array.
// Minimize the total number of operations.
// @param {number[]} nums
// @return {void} Do not rturn anything, modify nums in-place instead.
// var moveZeroes = function (nums) {
// };
// 3. Maximum Product
// https://www.codewars.com/kata/maximum-product
// 주어진 정수 배열을 사용하여 배열에서 인접한 2개의 숫자를 곱하여 얻은 최대 값을 찾습니다.
function adjacentElementsProduct(array) {
// max product
var Arr = [];
for(var i = 0; i < array.length -1; i++){
Arr.push(array[i] * array[i+1]);
}
return Math.max(...Arr);
};
console.log(adjacentElementsProduct([1,2,3]));
// 별도 문제.
/**
* @param {string} word
* @return {boolean}
*/
var detectCapitalUse = function(word) {
var myWord = /^([A-Z]+)([a-z]*)$/g
// var myWord2 = /^([a-z]*)$/g
if(myWord.test(word)) {return true;}
// if(myWord2.test(word)) {return true;}
return false;
};
console.log(detectCapitalUse('USA'));
console.log(detectCapitalUse('usa'));
console.log(detectCapitalUse('UsA'));
// 4. 두 개의 파라미터로 전달되는 문자열 중 중복되는 문자열의 유무를 파악해서 true, false로 반환합니다.
// function harmlessRansomNote(noteText, magazineText){}
// return noteIsPossible;
// 5. Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
// To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
// To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
// Example 1:
// Input: [[1,1,0],[1,0,1],[0,0,0]]
// Output: [[1,0,0],[0,1,0],[1,1,1]]
// Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
// Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
// 6. Add Digits
// Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
// Example:
// Input: 38
// Output: 2
// Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
// Since 2 has only one digit, return it.
// @param {number} num
// @return {number}
// var addDigits = function (num) {
// };
// 7. The goal of this exercise is to convert a string to a new string where each character in the new string is ‘(’ if that character appears only once in the original string, or ‘)’ if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
// 문자를 새로운 문자열로 변환 하는데, 새 문자열의 각 문자가 원본 문자열에 한번만 나타나면 ‘(’ 반영
// 중복될 경우 ‘)’ 반영. (대소문자는 무시)
// ex)
// “din” => “(((”
// “recede” => “()()()”
// “Success” => “)())())”
// “(( @” => “))((”
// 8. 단어 목록이 있으면 아래 이미지와 같이 한 줄의 미국식 키보드에서만 알파벳 문자를 사용하여 입력 할 수있는 단어를 반환하십시오 .
// 미국식 키보드
// 예제 1 :
// 입력 : [ "Hello", "Alaska", "Dad", "Peace"]
// 출력 : [ "Alaska", "Dad"]
// 9. Detect Capital
// Given a word, you need to judge whether the usage of capitals in it is right or not.
// We define the usage of capitals in a word to be right when one of the following cases holds:
// All letters in this word are capitals, like “USA”.
// All letters in this word are not capitals, like “leetcode”.
// Only the first letter in this word is capital if it has more than one letter, like “Google”.
// Otherwise, we define that this word doesn’t use capitals in a right way.
// Example 1:
// Input: “USA”
// Output: True
// Example 2:
// Input: “FlaG”
// Output: False
// Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
// @param {string} word
// @return {boolean}
// var detectCapitalUse = function (word) {
// };
// 10. Dominant array elements
// https://www.codewars.com/kata/dominant-array-elements/train/javascript
// 11. 10미만의 자연수에서 3과 5의 배수를 구하면 3,5,6,9이다. 이들의 총합은 23이다.
// 1000미만의 자연수에서 3,5의 배수의 총합을 구하라.