Skip to content

Commit 62b6059

Browse files
committed
add solutions of 4 problems
1 parent 064ae0b commit 62b6059

4 files changed

Lines changed: 82 additions & 64 deletions

File tree

javascript/LeetCode/Array/3190.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 3190. Find Minimum Operations to Make All Elements Divisible by Three
3+
*
4+
* 計算能將所有元素被3整除的步驟為幾,一次操作能將元素+1或-1。
5+
*
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var minimumOperations = function(nums) {
10+
let answer = 0;
11+
for(let i = 0;i < nums.length;++i) {
12+
if(nums[i] % 3 !== 0){
13+
answer++;
14+
}
15+
}
16+
return answer;
17+
};
18+
let nums = [1,2,3,4];
19+
// 3
20+
console.log(minimumOperations(nums));

javascript/LeetCode/String/28.js

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,17 @@
1-
2-
/**
3-
* 28. Implement strStr()
4-
* Difficulty:Easy(459延伸題)
5-
*
6-
* Implement strStr().
7-
*
8-
* Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
9-
*
10-
* Clarification:
11-
* What should we return when needle is an empty string? This is a great question to ask during an interview.
12-
* For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
13-
*
14-
* ---------------------------------------------------
15-
* Input: haystack and needle are string
16-
* Output:在haystack中第一個出現的needle index值
17-
* 若haystack沒有needle,return -1;
18-
* 或needle是空的,return 0
19-
* ------------------------------------------------------
20-
* Example 1:
21-
* Input: haystack = "hello", needle = "ll"
22-
* Output: 2
23-
*
24-
* Example 2:
25-
* Input: haystack = "aaaaa", needle = "bba"
26-
* Output: -1
27-
*
28-
* Example 3:
29-
* Input: haystack = "", needle = ""
30-
* Output: 0
31-
*
32-
* Constraints:
33-
* 0 <= haystack.length, needle.length <= 5 * 104
34-
* haystack and needle consist of only lower-case English characters.
35-
*/
361
/**
2+
* 28. Find the Index of the First Occurrence in a String
3+
4+
* 回傳needle第一次出現在haystack的index,找不到回傳-1
5+
*
376
* @param {string} haystack
387
* @param {string} needle
398
* @return {number}
409
*/
41-
var strStr = function (haystack, needle) {
42-
/**
43-
* haystack中:
44-
* 有needle,return haystack index
45-
* 沒有,return -1
46-
* $haystack或$needle是空,retunr 0
47-
*
48-
* $haystack、$needle 都是英文小寫
49-
*/
50-
51-
if (needle === "") {
52-
return 0;
53-
}
54-
for (let i = 0; i < haystack.length; i++) {
55-
56-
// Return part of string:
57-
// for JS: string.substr(start,end)
58-
// for PHP: substr(string,start,end)
59-
let slice = haystack.substr(i, needle.length);
60-
if (slice === needle) {
61-
return i;
62-
}
63-
}
64-
return -1;
65-
10+
var strStr = function(haystack, needle) {
11+
return haystack.indexOf(needle);
6612
};
67-
let h = "hello";
68-
let nee = "ll";
69-
// 2
70-
console.log(strStr(h, nee));
13+
// let haystack = "leetcode", needle = "leeto";
14+
// -1
15+
let haystack = "sadbutsad", needle = "sad";
16+
// 0
17+
console.log(strStr(haystack,needle));

javascript/LeetCode/String/58.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 58. Length of Last Word
3+
*
4+
* 取得最後一個單字的長度,參數內會有空白格
5+
*
6+
* @param {string} s
7+
* @return {number}
8+
*/
9+
var lengthOfLastWord = function(s) {
10+
// solution 1.
11+
let temp = [];
12+
let splitToArr = s.split(" ");
13+
for(let i = 0;i < splitToArr.length;++i) {
14+
if(splitToArr[i] !== ""){
15+
temp.push(splitToArr[i]);
16+
}
17+
}
18+
return temp[temp.length - 1].length;
19+
20+
// solution 2.
21+
// let ans = 0;
22+
// let trim = s.trim();
23+
// for(let i = trim.length - 1;i >= 0;i--){
24+
// if(trim[i] !== " "){
25+
// ans++;
26+
// }else if(ans > 0){
27+
// break;
28+
// }
29+
// }
30+
// return ans;
31+
};
32+
let s = " fly me to the moon ";
33+
// 4
34+
console.log(lengthOfLastWord(s))

javascript/LeetCode/math/9.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* 9. Palindrome Number
3+
*
4+
* 檢查x是否是迴文(倒著念也一樣)
5+
* @param {number} x
6+
* @return {boolean}
7+
*/
8+
var isPalindrome = function(x) {
9+
// 數值不適合顛倒,轉字串
10+
let toStr = x.toString().split("").reverse();
11+
// console.log(toStr.join(""))
12+
13+
return x.toString() === toStr.join("");
14+
};
15+
let x = -121;
16+
// false
17+
console.log(isPalindrome(x))

0 commit comments

Comments
 (0)