LeetCode每日一题
题目:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
var reverse = function(x) {
if(x>0){
x = x+''
x = parseInt(x.split('').reverse().join(''))
}else{
x = -x + ''
x = -parseInt(x.split('').reverse().join(''))
}
return (x<=-Math.pow(2,31)||x>=Math.pow(2,31)+1) ? 0 : parseInt(x)
};
解题思路:
数组有反转的方法所以先将整数转为字符串然后通过split方法转化为数组,将数组反转后用join拼接,在进行判断是否溢出,如溢出返回0
VUE学习
vuex
**state:**存放数据
state: {
counter: 0,
girl: {
name: 'yy',
age: 18
}
},
//使用通过store.state
<h1>{{$store.state.counter}}</h1>
**mutations:**存放一些方法
mutations: {
add (state, num) {
state.counter += num
},
jian (state, playload) {
state.counter = state.counter - playload.num
},
xg (state) {
Vue.set(state.girl, 'sex', 'girl')
}
}
//使用通过store.commit来进行调用
methods: {
add () {
this.$store.commit('add', 1)
},
jian () {
this.$store.commit({
type: 'jian',
num: 1
})
},
xg () {
this.$store.commit({
type: 'xg'
})
}
}
mutations传值:
1.
//直接将参数加在后面
add () {
this.$store.commit('add', 1)
}
//第二个参数就是数据
add (state, num) {
state.counter += num
}
//传对象的形式type就是类型要传的数据直接定义
jian () {
this.$store.commit({
type: 'jian',
num: 1
})
}
//传递过来的是一个对象通过里面的属性名直接得到值
jian (state, playload) {
state.counter = state.counter - playload.num
}
mutations动态修改数组或者对象的值:
通过Vue.set或者Vue.delete
xg (state) {
Vue.set(state.girl, 'sex', 'girl')
Vue.delete(state.girl, 'age')
}
**ps:**在使用时最好将type进行抽离都写在一个js里面
export const ADD = 'add'
export const JIAN = 'jian'
//使用时import导入js
import {ADD, JIAN} from './store/mutations-type'
[ADD] () {
this.$store.commit('add', 1)
},
[JIAN] () {
this.$store.commit({
type: 'jian',
num: 1
})
}
add () {
this.$store.commit(ADD, 1)
},
jian () {
this.$store.commit({
type: JIAN,
num: 1
})
}
**getters:**可以对数据进行一些操作
getters: {
getPow (state) {
return state.counter * state.counter
},
getNumPow (state) {
return num => state.counter * num
}
}
//使用通过store.getters
<h3>{{$store.getters.getPow}}</h3>
getters传值:
通过返回一个函数来进行传值
//num是传递过来的参数
getNumPow (state) {
return num => state.counter * num
}
//使用时加参数加入
<h3>{{$store.getters.getNumPow(3)}}</h3>
actions:
经常用在异步
actions: {
updateNmae (context, payload) {
return new Promise((resolve) => {
setTimeout(() => {
context.commit('updata', payload)
resolve('完成')
}, 1000)
})
}
//使用时用dispatch
updata () {
this.$store.dispatch('updateNmae', 3).then(msg => {
console.log(msg)
})
}
modules:
导入多个store
const a = {
state: {
name: '我是模块'
},
mutations: {
cadd (state, payload) {
state.name = payload
}
},
actions: {
},
//第三个参数可以访问到根State对象
getters: {
newName (state, getters, rootState) {
return state.name + rootState.counter
}
}
}
//使用时用modules
modules: {
a
}