發表文章

目前顯示的是有「leetcode」標籤的文章

Markdown

412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to  n . But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] /** * @param {number} n * @return {string[]} */ var fizzBuzz = function(n) { var arr =[]; for(let i=1; i<n+1;i++){ var ans =""; if(i%3===0) ans+="Fizz"; if(i%5===0) ans+="Buzz"; if(ans==="") ans+=i; arr.push(ans); } return arr; }; 題目:輸入n 判斷3,5,15的倍數 結果輸出到陣列 思路: ...

476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given integer is guaranteed to fit within the range of a 32-bit signed integer. You could assume no leading zero bit in the integer’s binary representation. Example 1: Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. Example 2: Input: 1 Output: 0 Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. /** * @param {number} num * @return {number} */ var findComplement = function(num) { // convert num to string in binary var s= num.toString(2); // swap 1,0 var q = s.replace(/1/g, 'a'); var p = q.replace(/0/g, 'b'); p = p.replace(/b/g, 1); p = p.replace(/a/g, 0); /* convert binary string to decimal var...

1. Two Sum

1. Two Sum Given an array of integers, return  indices  of the two numbers  such that they add up to a specific target. You may assume that each input would have  exactly  one solution, and you may not use the  same  element twice. Example: /**  * @param {number[]} nums  * @param {number} target  * @return {number[]}  */ var twoSum = function(nums, target) {     for(let i=0; i<nums.length;i++){         for(let j=0; j<nums.length; j++){             if((nums[i]+nums[j]==target) && (i!=j)){                             var ans;                 ans=[i,j];                 return(ans);                     }else{       ...

461. Hamming Distance

The  Hamming distance  between two integers is the number of positions at which the corresponding bits are different. Given two integers  x  and  y , calculate the Hamming distance. Note: 0 ≤  x ,  y  < 2 31 . Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different. /**  * @param {number} x  * @param {number} y  * @return {number}  */ var hammingDistance = function(x, y) {     return(Number((x^y).toString(2).split("").reduce((a,b)=>Number(a)+Number(b)))); }; 思路: 1. 先確定x,y >0 屬於unsigned numbers 這是題目給的, 這樣就不用做處理可以直接做XOR 2. XOR完了之後會將結果輸出為十進位整數,接著使用toString轉二進位再塞進陣列 3. 將陣列累加 看有幾個一 由於此時陣列元素還是string因此先轉num在進行累加 輸出結果會變成string再轉回num 得到結果 觀摩區: 直接把0砍掉然後算字串多長wow D   drizzlezxy   Reputation:    1 we need only one line /** * @param {...

[ javascript] binary-tree

https://code.tutsplus.com/articles/data-structures-with-javascript-tree--cms-23393

448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤  n  ( n  = size of array), some elements appear twice and others appear once. Find all the elements of [1,  n ] inclusive that do not appear in this array. Could you do it without extra space and in O( n ) runtime? You may assume the returned list does not count as extra space. /**  * @param {number[]} nums  * @return {number[]}  */ var findDisappearedNumbers = function(nums) {     var n = nums.length;     var s = nums.toString().replace(/,/g,"");     var arr = [];    //var t = 0;          for(let i=1; i<n+1; i++){                  if(s.indexOf(i)<0 ){             let j = 0 ;            j = arr.push(i);             //t+=i;         }else{ ...

556. Next Greater Element III // GG

Given a positive  32-bit  integer  n , you need to find the smallest  32-bit  integer which has exactly the same digits existing in the integer  n  and is greater in value than n. If no such positive  32-bit  integer exists, you need to return -1. Example 1: Input: 12 Output: 21 Example 2: 21 -1 /**  * @param {number} n  * @return {number}  */ var nextGreaterElement = function(n) {     var temp = n;     var arr = (""+n).split("");     var arr2 = (""+temp).split(""); //原序     var arr3 = arr.sort().reverse();  // 大至小     //arr.reverse();     var s = Number(arr3.join("")); //大     var s2 = Number(arr2.join("")); // 原     //var t =  typeof(s);     //return (console.log(arr+":"+arr2+":"+arr3+":"+s+":"+s2+":"+t));         if(s2>s2){        // v...

557. Reverse Words in a String III

Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Note:  In the string, each word is separated by single space and there will not be any extra space in the string. /**  * @param {string} s  * @return {string}  */ var reverseWords = function(s) {     var arr1 = [];     var ans = [];     var ans2 = ""          //reverse arr1     arr1 = s.split(" ");     for (let i = 0 ;i<arr1.length;i++){         var arr3 = "";         arr3 = arr1[i].toString();         arr3 = arr3.split("").reverse().join("");         ans.push(arr3);     }          var add = ans.pop();          for(let i=0; i<ans.length; i++){         ans2 += ans[i]+" ";     } ...