Skip to content

Instantly share code, notes, and snippets.

@yingmu52
Last active March 16, 2018 20:08
Show Gist options
  • Save yingmu52/8a9b99746d71ac5a83f63bac2b543f85 to your computer and use it in GitHub Desktop.
Save yingmu52/8a9b99746d71ac5a83f63bac2b543f85 to your computer and use it in GitHub Desktop.
Leet Code Restaurant Inc

Weekly Leet Code Challenage

Every week we will have an easy leet code question, posted on every friday. and on the same day we will share the code for the previous challenage :)

Weekly Leet Code 771. Jewels and Stones

@suguru03
Copy link

suguru03 commented Mar 5, 2018

From Suguru (JavaScript) O(n)

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
function numJewelsInStones(J, S) {
  const map = {};
  for (let i = 0; i < S.length; i++) {
    const c = S[i];
    map[c] = ++map[c] || 1;
  }
  let result = 0;
  for (let i = 0; i < J.length; i++) {
    const n = map[J[i]];
    if (n) {
      result += n;
    }
  }
  return result;
}

@yingmu52
Copy link
Author

yingmu52 commented Mar 8, 2018

Weekly Leet Code Find All Numbers Disappeared

@mintothejoo
Copy link

mintothejoo commented Mar 9, 2018

From Minjoo (Javascript) O(n2)

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var findDisappearedNumbers = function(nums) {
    var toRet = [];
    for(var i = 1; i<nums.length+1; i++){
        if(!nums.includes(i)) toRet.push(i);
    }
    return toRet;
};

@Rambochicken
Copy link

From ~Josh the man (C#) O(n)

using System.Linq;


public class Solution
{
    public IList<int> FindDisappearedNumbers(int[] nums)
    {
        return Enumerable.Range(1, nums.Length).Except(nums.Distinct()).ToList();
        
    }
}

BEAT EVERYBODY IN C# BABY FIRST LEETCODE!

@yingmu52
Copy link
Author

yingmu52 commented Mar 9, 2018

From Xinyi (Golang) O(n)

func findDisappearedNumbers(nums []int) []int {
	result := []int{}
	// mark as many numbers to negative as much as possible
	for _, value := range nums {
		nums[abs(value)-1] = -abs(nums[abs(value)-1])
	}
	// find position and get the index
	for index, value := range nums {
		if value > 0 { 
			result = append(result, index + 1)
		}
	}
	return result
}
func abs(val int) int {
	return int(math.Abs(float64(val)))
 }

Copy link

ghost commented Mar 15, 2018

From Felipe (Golang) O(n)

func findDisappearedNumbers(nums []int) []int {
    a, r := make([]int, len(nums)), []int{}
           for _,n := range nums {
                   a[n-1] = 1
           }
   
           for i,n := range a {
                   if (n == 0){
                           r = append(r,i+1)
                   }
           }
           return (r)
}   

@yingmu52
Copy link
Author

Weekly LeetCode Question: Rotate String

@yingmu52
Copy link
Author

yingmu52 commented Mar 16, 2018

From Xinyi (Swift) O(n)

class Solution {
    var i = 0
    func rotateString(_ A: String, _ B: String) -> Bool {
      if A == B { return true }
      if i == A.count { return false }
      i += 1
      return rotateString(A.shiftLeft, B)
    } 
}

extension String {
    var shiftLeft: String {
        let index = self.startIndex
        let nextIndex = self.index(index, offsetBy: 1)
        return String(self[nextIndex...] + self[...index])
    }
}

@Rambochicken
Copy link

From Josh the Man (C#)

{
    public bool RotateString(string A, string B)
    {
       return (A+A).Contains(B);
    }
    
}

Beats 37% :( Next time I have to learn how to define my RotateString. NEXT ONE!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment