Giter Site home page Giter Site logo

leetcode's People

Watchers

 avatar

leetcode's Issues

计算插入位置

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

罗马数字转整数

let maps = {
  'I': 1,
  'V': 5,
  'X': 10,
  'L': 50,
  'C': 100,
  'D': 500,
  'M': 1000,
  'IV': 4,
  'IX': 9,
  'XL': 40,
  'XC': 90,
  'CD': 400,
  'CM': 900
}

/**
* @param {string}
* @return {number}
*/
function romanToInt (str) {
  let map =  /(X[LC])|(I[VX])|(C[DM])|[IVXLCDM]/g  // 注意:g的使用使得 match返回 一个包含所有匹配的元素的数组 
  let keys = str.match(map) // 返回拆分后的匹配的各个key
  let resulut = keys.reduce(function(accumulator, currentValue){
    return maps[currentValue] + accumulator
  }, 0)  //  第二个参数为accumulator的初始值
  console.log(resulut)
}

优化后:在match时就进行计算,减少一次遍历

function romanToInt (str) {
  let map =  /(X[LC])|(I[VX])|(C[DM])|[IVXLCDM]/g  // 注意:g的使用使得 match返回 一个包含所有匹配的元素的数组
  let result = 0;
  str.replace(map, function(match){
    console.log(match)
    result += maps[match]
  })
  console.log(result)
}

无重复字符的最长子串

let a = 'abcashdekdajdhf';

let max = 0, str;
let strObj = [] // 保存所有不重复子串
for (let m = 0, n = 0; n <= a.length; n++) {
  str = a.slice(m, n) // 截取区间不包含N
  let next = a.charAt(n)
  let index = str.indexOf(next);
  if (index != -1) {
    strObj.push(str)
    max = Math.max(max, str.length)
    m = m + index + 1 // 准确的窗口m值
  }
}
console.log(strObj)
console.log(max)

爬楼梯

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。

每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?

注意:给定 n 是一个正整数。

二维数组斜45度打印

let arr = [
	[1,2,3,4],
	[5,6,7,8],
	[9,10,11,12,13]
]

let arrLen = arr.length
let result =[]
for (let i = 0; i< arrLen; i++) {
	let initIndex = i > 0 ? (arr[i - 1].length - 1) : 0
	for (let j = initIndex; j < arr[i].length; j++) {
		for (let next_index = i; next_index < arrLen; next_index++) {
			let offset = next_index - i
			let item = j - offset
			if (item < 0) {
				break
			} else {
				result.push(arr[next_index][item])
			}
		}
	}
}
console.log(result.join(','))

有效的括号

给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。

回文数

function isRoundNum(number){
 if (number < 0 || (number% 10 == 0 && number != 0) ) { // 负数不能是回文 和  最后一个数为0 的情况
   return false;
 }
  let a = number.toString();  // 去掉+
  let harf = Math.round(a.length / 2) - 1; // 注意中间数 和 下标值之间差1
  for(let i = 0; i<= harf; i++) {
    let other = a.length -1 - i;  //  注意最后一个中间数的下标值
    if (a[i] != a[other]) {
      return false;
    }
  }
  return true
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.