Comments (4)
给定一个 32 位有符号整数,将整数中的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
注意:
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。
正确解法:
var reverse = function(x) {
if(x!=0){
var y = x > 0 ? x : -x
var z = y + ''
var toz = z.split('').reverse().join('') // 转数组后反转再转字符串
var reverse = parseInt(toz)
return reverse >= 2147483648?0 : (y / x) * reverse // 不太和谐的解决环境存储方法
}
else{
return 0
}
};
from blog.
给定一个只包括 '(',')'
,'{','}'
,'[',']'
的字符串,判断字符串是否有效。
有效字符串需满足:
左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
示例 1:
输入: "()"
输出: true
示例 2:
输入: "()[]{}"
输出: true
示例 3:
输入: "(]"
输出: false
示例 4:
输入: "([)]"
输出: false
示例 5:
输入: "{[]}"
输出: true
正确代码:
var isRight = function(x){
return ~[')',']','}'].indexOf(x)
}
var isLeft = function(x){
return ~['(','[','{'].indexOf(x)
}
var isMatch = function(x, y){
if(x == '('){
return y===')'
}
if(x == '['){
return y===']'
}
if(x == '{'){
return y==='}'
}
}
var isValid = function(s) {
var arrl = []
var arrf = s.split('')
if(arrf.length>=2147483647){
return false
}
if(s){
if(arrf.length <= 1){
return false
}
for(var i in arrf){
if(isRight(arrf[i])){
if(!isMatch(arrl.pop(),arrf[i])){
return false
}
}
else if(isLeft(arrf[i])){
arrl.push(arrf[i])
}
}
if(arrl.length==0){
return true
}
else{
return false
}
}
else{
return true
}
};
from blog.
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小。
因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。)
返回一个整数数组 ans,其中 ans[0] 是爱丽丝必须交换的糖果棒的大小,ans[1] 是 Bob 必须交换的糖果棒的大小。
如果有多个答案,你可以返回其中任何一个。保证答案存在。
示例 1:
输入:A = [1,1], B = [2,2]
输出:[1,2]
示例 2:
输入:A = [1,2], B = [2,3]
输出:[1,2]
示例 3:
输入:A = [2], B = [1,3]
输出:[2,3]
示例 4:
输入:A = [1,2,5], B = [2,4]
输出:[5,4]
提示:
1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
保证爱丽丝与鲍勃的糖果总量不同。
答案肯定存在。
/**
* @param {number[]} A
* @param {number[]} B
* @return {number[]}
*/
var fairCandySwap = function(A, B) {
let c = sum(...A) - sum(...B)
let a1 = 0,b1 = 0
for (let a in A) {
for (let b in B) {
if (c == A[a] - B[b]) {
a1 = a
b1 = b
}
}
}
return [A[a1], B[b1]]
}
var sum = function () {
return [...arguments].reduce((sum, v) => sum + v, 0)
}
from blog.
摩斯密码解码
decodeMorse = function(morseCode){
morseCode = morseCode.replace(/(^\s*)|(\s*$)/g, "")
let resultArr = morseCode.split(' ').map(ele => ele.split(' ').map(i => MORSE_CODE[i]).join('')).join(' ')
return resultArr
}
from blog.
Related Issues (20)
- 链接失效
- 关于 electron 数据缓存
- 一些关于 VSCode 插件开发的解决方案 HOT 1
- peerDependencies作用
- ELO rating system
- VSCode 插件在 kooltest 自动化测试的开发实践
- 薅一手 Github Actions 自动发包 VSCode 插件
- 八月小结
- [摘]个人效率指南
- 关于 CDN 前端该懂的一切 HOT 1
- 《DDIA》读书笔记
- PostgreSQL 数据迁移
- 九月小结
- 如何在 Sketch 插件中使用 React Typescript HOT 1
- 十月小结
- 从 Node 上传服务说起的爬坑日记
- 八股合集
- 一个说新不算新的服务端渲染技术——流式渲染 HOT 1
- 我来杭州的这两年 HOT 1
- 五月第一周周报
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.