leetcode.com/problems/number-of-good-pairs/

 

Number of Good Pairs - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

javascript

/**
 * @param {number[]} nums
 * @return {number} 
 */
const numIdenticalPairs = (nums) => {
  const pairs = {}
  for (let i = 0; i < nums.length; i++) {
    pairs[nums[i]] ? pairs[nums[i]] += 1 : pairs[nums[i]] = 1
  }
  let numGoodPairs = 0
  const pairNums = Object.values(pairs)
  for (let i = 0; i < pairNums.length; i++) {
    numGoodPairs += (pairNums[i] * (pairNums[i] - 1)) / 2
  }
  return numGoodPairs
}

typescript

interface Pairs {
  [key: string]: number
}

const numIdenticalPairsTS = (nums: number[]): number => {
  const pairs: Pairs = {}
  for (let i = 0; i < nums.length; i++) {
    pairs[nums[i]] ? pairs[nums[i]] += 1 : pairs[nums[i]] = 1
  }
  let numGoodPairs: number = 0
  const pairNums = Object.keys(pairs)
  for (let i = 0; i < pairNums.length; i++) {
    numGoodPairs += (pairs[pairNums[i]] * (pairs[pairNums[i]] - 1)) / 2
  }
  return numGoodPairs
}

golang

func numIdenticalPairs(nums []int) int {
	goodPairs := 0
	pairs := make([]int, 101)
	for _, num := range nums {
		pairs[num]++
	}
	for _, pairsNum := range pairs {
		if pairsNum > 1 {
			goodPairs += pairsNum * (pairsNum - 1) / 2
		}
	}
	return goodPairs
}

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) 771. Jewels and Stones  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy) Kids With the Greatest Number of Candies  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02

 

leetcode.com/problems/defanging-an-ip-address/

 

Defanging an IP Address - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

javascript

/**
 * @param {string} address
 * @return {string}
 */
const defangIPaddr = (address) => {
  return address.replace(/\./g, '[.]')
  // address.split('.').join('[.]')
};

typescript

const defangIPaddrTS = (address: string): string => {
  return address.replace(/\./g, '[.]')
  // address.split('.').join('[.]')
};

golang

func defangIPaddr(address string) string {
	return strings.Replace(address, ".", "[.]", -1)
}

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 771. Jewels and Stones  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy) Kids With the Greatest Number of Candies  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02

 

leetcode.com/problems/jewels-and-stones/

 

Jewels and Stones - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

javascript

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
const numJewelsInStones = function (J, S) {
  const stons = [...S]
  let numJewelsInStone = 0
  for (let i = 0; i < stons.length; i++) {
    if (J.includes(stons[i])) {
      numJewelsInStone++
    }
  }
  return numJewelsInStone
};

typescript

const numJewelsInStonesTS = function (J: string, S: string): number {
  const stons: string[] = [...S]
  let numJewelsInStone: number = 0
  for (let i = 0; i < stons.length; i++) {
    if (J.includes(stons[i])) {
      numJewelsInStone++
    }
  }
  return numJewelsInStone
};

golang

func numJewelsInStones(J string, S string) int {
	jewels := make(map[rune]bool)
	for _, jewel := range J {
		jewels[jewel] = true
	}
	numJewelsInStone := 0
	for _, stone := range S {
		if jewels[stone] {
			numJewelsInStone++
		}
	}
	return numJewelsInStone
}

/*
func numJewelsInStones(J string, S string) int {
	num := 0
	for _, jewel := range J {
		for _, stone := range S {
			if jewel == stone {
				num++
			}
		}
	}
	return num
}
*/

 

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy) Kids With the Greatest Number of Candies  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02

leetcode.com/problems/shuffle-the-array/

 

Shuffle the Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

nums 배열이 [1, 2, 3, 1, 2, 3] 이고

n 이 3이면

결과값은 [1, 1, 2, 2, 3, 3] 이 되어야 합니다.

 

그래서 

먼저 중간 인덱스 값은 mid를 구합니다.

그리고 배열이 나누어져 있다고 생각하고 

[1, 2, 3] [1, 2, 3]

새로운 빈 배열에 하나씩 넣습니다.

 

javascript

/**
 * @param {number[]} nums
 * @param {number} n
 * @return {number[]}
 */
const shuffle = (nums, n) => {
  if (nums.length <= 2) return nums
  const shuffled = []
  const mid = Math.floor(nums.length / 2)
  for (let i = 0; i < mid; i++) {
    shuffled.push(nums[i])
    shuffled.push(nums[i + n])
  }
  return shuffled
};

typescript

function shuffleTS(nums: number[], n: number): number[] {
  if (nums.length <= 2) return nums
  const shuffled: number[] = []
  const mid: number = Math.floor(nums.length / 2)
  for (let i = 0; i < mid; i++) {
    shuffled.push(nums[i])
    shuffled.push(nums[i + n])
  }
  return shuffled
};

golang

func shuffle(nums []int, n int) []int {
	if len(nums) <= 2 {
		return nums
	}
	result := make([]int, len(nums))
	mid := int(len(nums) / 2)

	i := 0
	j := 0
	for i < mid {
		result[j] = nums[i]
		result[j+1] = nums[i+n]
		i++
		j += 2
	}
	return result
}

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

 

Kids With the Greatest Number of Candies - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

candies 양의 정수 배열은 각 index 별로 아이들이 가지고 있는 캔디 수를 의미합니다.

아이에게 extraCandies를 주면 아이들 중에 가장 많은 캔디를 가질 수 있는지 없는지를 판별하여

boolean 배열을 반환합니다.

 

javascript

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
const kidsWithCandies = (candies, extraCandies) => {
  const greatestNum = Math.max(...candies)
  const result = candies.map(kid => kid + extraCandies >= greatestNum)
  return result
}

typescript

const kidsWithCandiesTS = (candies: number[], extraCandies: number): boolean[] => {
  const greatestNum: number = Math.max(...candies)
  const result: boolean[] = candies.map(kid => kid + extraCandies >= greatestNum)
  return result
}

golang

func kidsWithCandies(candies []int, extraCandies int) []bool {
	var result = make([]bool, len(candies))
	var greatestNum int = candies[0]
	for i := 0; i < len(candies); i++ {
		if greatestNum < candies[i] {
			greatestNum = candies[i]
		}
	}
	for index, kid := range candies {
		if kid+extraCandies >= greatestNum {
			result[index] = true
		}
	}
	return result
}

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) 771. Jewels and Stones  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy. Running Sum of 1d Array  (0) 2020.07.02
var runningSum = function (nums) {
  let sum = 0;
  let result = new Array();
  for (let i = 0; i < nums.length; i++) {
    sum += nums[i];
    result.push(sum);
  }
  return result;
};

Input: nums = [1,2,3,4]

Output: [1,3,6,10]

Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]

 

Input: nums = [1,1,1,1,1]

Output: [1,2,3,4,5]

Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]

 

반복문을 사용해 num 값들을 sum에 차례대로 더해준다.

더해줄 때마다 sum을 result 배열에 담아준다.

'코딩 테스트 > LeetCode' 카테고리의 다른 글

Easy) 1512. Number of Good Pairs  (0) 2020.09.15
Easy) 1108. Defanging an IP Address  (0) 2020.09.15
Easy) 771. Jewels and Stones  (0) 2020.09.15
Easy) Shuffle the Array  (0) 2020.09.13
Easy) Kids With the Greatest Number of Candies  (0) 2020.09.13

+ Recent posts