android:delete avb

avb :https://source.android.google.cn/docs/core/architecture/kernel/mounting-partitions-early

file: vendor/etc/fstab.qcom

clears all AVB code (except for the 7 AVBs in the figure)

Find the line of code [userdata] with code similar to this, and delete it.

【fileencryption=ice,wrappedkey,keydirectory=/metadata/vold/metadata_encryption,】

MIUI delete screen lock

1
/data/system/locksettings

Similar to this kind of file, del two. The screen lock key was deleted.

crDroid delete screen lock

1
/data/system/locksettings.db

Already flashed Gapps’ crDroid, after re-flashing the rom. Then restore the backup and the screen passcode will change. At this time, you need to delete the locksettings.db file. If crDroid is stuck on the second screen after restarting, it may be caused by Gapps and needs to be re-flashed.

Check the current A/B slots for Android.

In the Android system, if the device supports A/B partitioning, the system will create two partitions in the device, one is the currently used partition and the other is the alternate partition. These two partitions are called slots. When using the adb shell command, you can see which slot the current system belongs to with the following command:

1
getprop ro.boot.slot_suffix

This command returns the slot number to which the current system belongs, for example, if the returned result is “_a”, it means that the current system belongs to slot A.

If the returned result is “_b”, it means that the current system belongs to slot B.

git-Modify the userName after push

This method is suitable for updating the username after pushing.

cd the root of THE GIT PROJECT

Replace the following parameters.

OLD_EMAIL
CORRECT_NAME
CORRECT_EMAIL

executive:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git filter-branch  -f --env-filter '
OLD_EMAIL="oldEmail@foxmail.com"
CORRECT_NAME="newName"
CORRECT_EMAIL="new@email.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Push up the dev branch.

1
git push --force --tags origin 'refs/heads/dev'

centos7 update ssh port

0x01 open ports firewall

It should include 22 port

1
2
3
4
systemctl status firewalld
firewall-cmd --zone=public --list-all
firewall-cmd --permanent --zone=public --add-port=222/tcp
firewall-cmd --permanent --zone=public --add-port=22/tcp

0x02 update sshd config

1
sudo vi /etc/ssh/sshd_config

add:

1
2
Port 22
Port 222

0x03 install semanage

It involve with the problem of SELinux when we update ssh ports.

1
2
3
yum provides semanage
yum -y install policycoreutils-python
semanage port -a -t ssh_port_t -p tcp 222

and

1
2
3
systemctl restart sshd
or
reboot

0x04 end

1
2
firewall-cmd --permanent --zone=public --remove-port=22/tcp
firewall-cmd --reload

vi /etc/ssh/sshd_config & remove Port 22

Android自带应用精简

个别软件,前提要去卡米

1
2
3
4
5
6
7
8
9
10
public class RmRfTest {
public static void main(String[] args) {

for (String str : FileUtil.readUtf8Lines("rm44.txt")) {
System.out.println(str + "||||||" + RuntimeUtil.execForStr(str));
ThreadUtil.sleep(200);
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
adb shell pm uninstall --user 0  com.miui.systemAdSolution
adb shell pm uninstall --user 0 com.miui.analytics
adb shell pm uninstall --user 0 com.xiaomi.gamecenter.sdk.service
adb shell pm uninstall --user 0 com.xiaomi.gamecenter
adb shell pm uninstall --user 0 com.sohu.inputmethod.sogou.xiaomi
adb shell pm uninstall --user 0 com.miui.player
adb shell pm uninstall --user 0 com.miui.video
adb shell pm uninstall --user 0 com.miui.notes
adb shell pm uninstall --user 0 com.miui.translation.youdao
adb shell pm uninstall --user 0 com.miui.translation.kingsoft
adb shell pm uninstall --user 0 com.android.email
adb shell pm uninstall --user 0 com.xiaomi.scanner
adb shell pm uninstall --user 0 com.miui.hybrid
adb shell pm uninstall --user 0 com.miui.bugreport
adb shell pm uninstall --user 0 com.milink.service
adb shell pm uninstall --user 0 com.android.browser
adb shell pm uninstall --user 0 com.miui.yellowpage
adb shell pm uninstall --user 0 com.xiaomi.payment
adb shell pm uninstall --user 0 com.mipay.wallet
adb shell pm uninstall --user 0 com.android.soundrecorder
adb shell pm uninstall --user 0 com.miui.voiceassist
adb shell pm uninstall --user 0 com.miui.touchassistant
adb shell pm uninstall --user 0 com.xiaomi.mitunes
adb shell pm uninstall --user 0 com.xiaomi.pass
adb shell pm uninstall --user 0 com.android.wallpaper
adb shell pm uninstall --user 0 com.android.wallpaper.livepicker
adb shell pm uninstall --user 0 com.miui.klo.bugreport
adb shell pm uninstall --user 0 com.miui.securityinputmethod
adb shell pm uninstall --user 0 com.baidu.input_mi
adb shell pm uninstall --user 0 com.iflytek.inputmethod.miui
adb shell pm uninstall --user 0 com.sohu.inputmethod.sogou.xiaomi
adb shell pm uninstall --user 0 com.miui.personalassistant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
adb shell pm uninstall --user 0 com.miui.accessibility
adb shell pm uninstall --user 0 com.miui.analytics
adb shell pm uninstall --user 0 com.miui.aod
adb shell pm uninstall --user 0 com.miui.bugreport
adb shell pm uninstall --user 0 com.miui.cleanmaster
adb shell pm uninstall --user 0 com.miui.compass
adb shell pm uninstall --user 0 com.miui.contentextension
adb shell pm uninstall --user 0 com.miui.easygo
adb shell pm uninstall --user 0 com.miui.huanji
adb shell pm uninstall --user 0 com.miui.hybrid
adb shell pm uninstall --user 0 com.miui.hybrid.accessory
adb shell pm uninstall --user 0 com.miui.maintenancemode
adb shell pm uninstall --user 0 com.miui.miservice
adb shell pm uninstall --user 0 com.miui.mishare.connectivity
adb shell pm uninstall --user 0 com.miui.miwallpaper.earth
adb shell pm uninstall --user 0 com.miui.miwallpaper.geometry
adb shell pm uninstall --user 0 com.miui.miwallpaper.mars
adb shell pm uninstall --user 0 com.miui.miwallpaper.saturn
adb shell pm uninstall --user 0 com.miui.miwallpaper.snowmountain
adb shell pm uninstall --user 0 com.miui.newhome
adb shell pm uninstall --user 0 com.miui.newmidrive
adb shell pm uninstall --user 0 com.miui.nextpay
adb shell pm uninstall --user 0 com.miui.notes
adb shell pm uninstall --user 0 com.miui.smarttravel
adb shell pm uninstall --user 0 com.miui.systemAdSolution
adb shell pm uninstall --user 0 com.miui.touchassistant
adb shell pm uninstall --user 0 com.miui.thirdappassistant
adb shell pm uninstall --user 0 com.miui.tsmclient
adb shell pm uninstall --user 0 com.miui.voiceassist
adb shell pm uninstall --user 0 com.miui.voicetrigger
adb shell pm uninstall --user 0 com.miui.weather2
adb shell pm uninstall --user 0 com.miui.yellowpage
adb shell pm uninstall --user 0 com.miui.translation.kingsoft
adb shell pm uninstall --user 0 com.miui.translation.youdao
adb shell pm uninstall --user 0 com.miui.translation.xmcloud
adb shell pm uninstall --user 0 com.miui.translationservice
adb shell pm uninstall --user 0 com.miui.cloudbackup
adb shell pm uninstall --user 0 com.miui.cloudservice
adb shell pm uninstall --user 0 com.miui.micloudsync
adb shell pm uninstall --user 0 com.xiaomi.ab
adb shell pm uninstall --user 0 com.xiaomi.drivemode
adb shell pm uninstall --user 0 com.xiaomi.gamecenter
adb shell pm uninstall --user 0 com.xiaomi.gamecenter.sdk.service
adb shell pm uninstall --user 0 com.xiaomi.joyose
adb shell pm uninstall --user 0 com.xiaomi.jr
adb shell pm uninstall --user 0 com.xiaomi.macro
adb shell pm uninstall --user 0 com.xiaomi.mi_connect_service
adb shell pm uninstall --user 0 com.xiaomi.mibrain.speech
adb shell pm uninstall --user 0 com.xiaomi.migameservice
adb shell pm uninstall --user 0 com.xiaomi.mirror
adb shell pm uninstall --user 0 com.xiaomi.payment
adb shell pm uninstall --user 0 com.xiaomi.shop
adb shell pm uninstall --user 0 com.xiaomi.finddevice
adb shell pm uninstall --user 0 com.xiaomi.micloud.sdk
adb shell pm uninstall --user 0 com.mfashiongallery.emag
adb shell pm uninstall --user 0 com.mi.health
adb shell pm uninstall --user 0 com.mi.liveassistant
adb shell pm uninstall --user 0 com.mipay.wallet
adb shell pm uninstall --user 0 com.achievo.vipshop
adb shell pm uninstall --user 0 com.baidu.searchbox
adb shell pm uninstall --user 0 com.bsp.catchlog
adb shell pm uninstall --user 0 com.dianping.v1
adb shell pm uninstall --user 0 com.dragon.read
adb shell pm uninstall --user 0 com.duokan.reader
adb shell pm uninstall --user 0 com.eastmoney.android.berlin
adb shell pm uninstall --user 0 com.Qunar
adb shell pm uninstall --user 0 com.qxwz.ps.hpls
adb shell pm uninstall --user 0 com.sina.weibo
adb shell pm uninstall --user 0 com.taobao.litetao
adb shell pm uninstall --user 0 com.tencent.mtt
adb shell pm uninstall --user 0 com.UCMobile
adb shell pm uninstall --user 0 ctrip.android.view
adb shell pm uninstall --user 0 tv.pps.mobile
adb shell pm uninstall --user 0 com.android.email
adb shell pm uninstall --user 0 com.android.managedprovisioning
adb shell pm uninstall --user 0 com.android.printspooler
adb shell pm uninstall --user 0 com.android.quicksearchbox
adb shell pm uninstall --user 0 com.android.internal.systemui.navbar.threebutton
1
2
3
4
5
6
7
8
9
10
adb shell pm disable-user com.miui.daemon
adb shell pm disable-user com.xiaomi.xmsf
adb shell pm disable-user com.xiaomi.simactivate.service
adb shell pm disable-user com.xiaomi.xmsfkeeper
adb shell pm uninstall --user 0 com.miui.cloudservice
adb shell pm uninstall --user 0 com.xiaomi.account
adb shell pm uninstall --user 0 com.miui.cloudbackup
adb shell pm uninstall --user 0 com.xiaomi.market
adb shell pm uninstall --user 0 com.android.thememanager
adb shell pm uninstall --user 0 com.miui.cloudservice.sysbase
1
2
3
4
5
6
7
8
9
10
11
adb shell pm uninstall --user 0 com.miui.carlink
adb shell pm uninstall --user 0 com.unionpay.tsmservice.mi
adb shell pm uninstall --user 0 com.xiaomi.powerchecker
adb shell pm uninstall --user 0 com.miui.securityadd
adb shell pm uninstall --user 0 com.miui.core.internal.assistant
adb shell pm uninstall --user 0 com.miui.vipservice
adb shell pm uninstall --user 0 com.miui.phrase
adb shell pm uninstall --user 0 com.miui.compass
adb shell pm uninstall --user 0 com.miui.cleanmaster
adb shell pm uninstall --user 0 org.mipay.android.manager
adb shell pm uninstall --user 0 com.xiaomi.scanner

github不同项目pages使用不同的域名

github不同项目pages使用不同的域名

CNAME方式

最常见的是 CNAME方式: .github.io 或 .github.io

具体参考 https://docs.github.com/cn/pages/configuring-a-custom-domain-for-your-github-pages-site/about-custom-domains-and-github-pages

比如添加 www.example.com 指向 example.github.io在项目根目录 添加文件CNAME,

里面内容: www.example.com

之后的项目files的pages默认地址是www.example.com/files

默认方式

如果同一个GitHub账号上另一个项目比如files也开启一个pages

那么默认的访问路径是www.example.com/files

A方式

如果想把www.example.com/files 改成 file.example.com,可以通过A方式.

CNAME 文件对应一个域名, 比如file.example.com,

在域名里面A方式配置

img

效果

48个音标

48个音标

音标 类型 读音 常见用法
/iː/ 长元音1 e/i:/ ;d /diː/ ;c 通常情况下,元音在重读开音节中都发本身的音,所以e在重读开音节中发/iː/此外组合ee,ea,ie,ei在重读音节中都可以发/iː/ sheep/ʃiːp/n.绵羊;seat/siːt/n.座位;achieve/ə’tʃiːv/vt.取得 ; b bee/biː/
/uː/ 长元音2 q /kjuː/;u /juː/ 组合oo在大多数情况下都发长元音/uː/o,u单词中发/uː/组合ew,ui,oe,ou在单词中发/uː/ tooth/tuːθ/n.牙齿;lose/luːz/vi.失败;chew/tʃuː/vt.嚼碎
/ɜː/ 长元音3 early/‘ɜːlɪ/ 通常情况下组合er,ir,ur在重读音节中都发/ɜː/or组合在w后也发/ɜː/ear组合后接辅音时通常发/ɜː/ certain/ˈsɜ:tn/pron.某些;worship/ˈwɜ:ʃɪp/vt.崇拜
/ɔː/ 长元音4 or /ɔː(r)/ 组合al在大多辅音前都发/ɔː/组合au,aw,or,oar,oor,ore在重读音节中发/ɔː/组合ar在w后发/ɔː/组合ou在ght前发/ɔː/ talk/tɔːk/vt.说;cause/kɔːz/n.原因;war/wɔː/n.战争;fought/fɔːt/v.战斗
/ɑː/ 长元音5 r /ɑ:/ 组合al在大多数辅音前发/ɔ:/,但在f或m前发/ɑː/组合ar在w后发/ɔ:/,在其它重读音节中发/ɑː/元音节母a在f,n,ss,sp,st前发/ɑː/组合au发/ɑː/ calm/kɑːm/adj.静的;arctic/‘ɑːktɪk/n.北极圈;fast/fɑːst/adj.快速的
/ɪ/ 短元音1 it /ɪt/ i,y在闭音节和非重读音节中发/ɪ/,e在非重读音节发/ɪ/ bit/bɪt/adj.很小的;remain/rɪ’meɪn/vi.剩余
/e/ 短元音2 f /ef/;l /el/;m /em/;s /es/ e在重读开音节中发/iː/,在重读闭音节通常发/e/组合ea在重读音节中不仅可以发/i:音,也可以发/e/的音 check/tʃek/vi.核实;dead/ded/n.死者
/æ/ 短元音3 as /æz/;at /æt/ a在重读闭音节中发/æ/ mad/mæd/adj.疯狂的; man /mæn/; have /hæv/; that /ðæt/; and /ænd/; can /kæn/
/ə/ 短元音4 panda /‘pændə/ a在非重读音节中发/ə/e在非重读音节中发/ə/o在非重读音节中发/ə/u在非重读音节中发/ə/or在非重读音节中发/ə/er在非重读音节中发/ə/ woman/‘wʊmən/n.妇女;along/ə’lɒŋ/prep.沿着;open/‘əʊpən/adj.公开的;today/tə’deɪ/n.今天;support/sə’pɔːt/vt.支持;factor/‘fæktə/n.因素;meter/ˈmi:tə/n.米;仪表
/ʌ/ 短元音5 w /ˈdʌb(ə)lˌju/ o在m,n,v或th前常发/ʌ/u在重读闭音节中常发/ʌ/组合ou在部分单词中也发/ʌ/ love/lʌv/vi.爱;drug/drʌg/n.麻醉药;touch/tʌtʃ/vt.接触
/ʊ/ 短元音6 ;should/ʃʊd/ oo在k前发短元音/ʊ/oul在重读音节中发/ʊ/u在某些情况下发/ʊ/ cook/kʊk/vt.烹调
/ɒ/ 短元音7 hot /hɒt/ 元音o在重读闭音节中通常发/ɒ/元音a在w,wh后也常发/ɒ/ 的;want/wɒnt/vt.需要
/eɪ/ 双元音1 a/eɪ/;h /eɪtʃ/;j /dʒeɪ/ a在重读开音节中常发/eɪ/组合ai,ay,ei在重读音节中常发/eɪ/ fate/feɪt/n.命运;laid/leɪd/adj.松弛的
/aɪ/ 双元音2 i /aɪ/;y /wai/ i,y在重读开音节中都发/aɪ/i组和与y组合发/aɪ/的情况 write/raɪt/vi.写;time/taɪm/n.时间 i/aɪ/
/ɔɪ/ 双元音3 boy /bɔɪ/ 常见组合oy,oi发/ɔɪ/ toy /tɔɪ/ n. 玩具
/aʊ/ 双元音4 mouse/maʊs/ 发/aʊ/音的常见组合有ou,ow counter/‘kaʊntə/n.柜台
/əʊ/ 双元音5 oh /əʊ/美音读/o/;等同美音/o/ 元音o在重读开音节中发本身的音/əʊ/另外发/əʊ/音的常见组合有oa,oe,ow smoke/sməʊk/n.烟;boat/bəʊt/n.小船
/ɪə/ 双元音6 beer /bɪə/ 发/ɪə/音的常见组合有ear,ere,ea,eer rear/rɪə/adv.向后
/eə/ 双元音7 bear/beə/ 发/eə/这个音的常见组合有are,air,ear,ere share/ʃeə/vt.分享
/ʊə/ 双元音8 poor/pʊə/ 发/ʊə/这个音的常见组合有our,oor,ure moor/mʊə/n.沼泽
/p/ 清辅音1 p /pi:/ 发/p/音的和组合是p,pp pay/peɪ/vt.支付;apple/‘æp(ə)l/n.苹果
/t/ 清辅音2 t /tiː/ t字母发/t/音,有的单词两个t个字母 talk/tɔːk/vt.讲;tea/tiː/n.茶叶
/k/ 清辅音3 k /kei/;q /kjuː/ “c、k”和组合”ch、ck”发/k/音 key/kiː/n.钥匙;cafe/‘kæfeɪ/n.咖啡馆;quiet/‘kwaɪət/adj.安静的;bicycle/‘baɪsɪkl/n.自行车
/f/ 清辅音4 f /ef/ f,ff发本音/f/组合ph发/f/音组合”gh”出现在元音后有时发/f/音/f/可与/l/或/r/连在一起,形成辅音连缀/fl/或/fr/,后接元音构成一个音节 life/laɪf/n.生活;alpha/‘ælfə/n.开端;laugh/lɑːf/n.笑;fly/flaɪ/vt.飞行
/s/ 清辅音5 c /si:/ ; s /es/;x /eks/ 常见的发/s/音的及组合有s,ss,c,sc sit/sɪt/vi.坐;sea/siː/n.海
/ʃ/ 清辅音6 wish /wɪʃ/ 组合sh发/ʃ/当s出现在u前发/ʃ/当ss,ci,ti,si出现在单词后缀中时也常发/ʃ/ sure/ʃɔ:(r)/adj.确信的;assure/ə’ʃʊə/vt.保证
/θ/ 清辅音7 south /saʊθ/ 在英语中发/θ/这个音一般只有th组合 bathroom/‘bɑːθruːm/n.浴室; think /θɪŋk/; mouth /maʊθ/
/h/ 清辅音8 home /həʊm/ 在英文中大多数情况下只有h发/h/音另外许多首h不发音 home/həʊm/n.家;hair/heə/n.头发; hello /həˈləʊ/; happy /‘hæpɪ/
/tʃ/ 清辅音9 h /eɪtʃ/;t /tiː/ 一般发/t∫/这个音的组合为”ch”和”tch”当”t”出现在”ua,ure”等组合前时也经常发/t∫/音 chick/tʃɪk/n.小鸡;chair/tʃeə/n.椅子;natural/ˈnætʃrəl/adj.自然的
/tr/ 清辅音10 tree /triː/ 发/tr/音的组合一般只有tr ostrich/‘ɒstrɪtʃ/n.鸵鸟;trim/trɪm/n.修剪
/ts/ 清辅音11 cats /kæts/ 以t结尾的规则名词变为复数或规则动词变为第三人称单数时,我们通常在单词后加s,这时单词结尾的组合ts就发/ts/ meets /miːts/ vt.(第三人称)遇见;cats/kæts/n.(复数)猫;hats/hæts/n.(复数)帽子
/b/ 浊辅音1 w /ˈdʌb(ə)lˌju/ 及组合b,bb发/b/这个音 bread/bred/n.面包;bathbɑːθ/n.沐浴/
/d/ 浊辅音2 d /diː/ 及组合d,dd发/d/这个音规则动词以清辅音结尾(t除外)变为过去式或过去分词词尾加ed发/t/,以浊辅音结尾(d除外)变为过去式或过去分词词尾加ed发/d/ duck/dʌk/n.鸭子;day/deɪ/n.一天;lived/lɪvd/adj.有生命的
/ɡ/ 浊辅音3 glass /glɑːs/ 及组合g,gg通常发/g/音组合”gu”或”gh”通常也发这个音,它们可以出现在元音前,与元音构成一个音节 game/geɪm/n.游戏;catalogue/‘kæt(ə)lɒg/n.目录
/v/ 浊辅音4 v /vi:/ 大多数情况下只有v发/v/音 very/‘verɪ/adv.非常;live/lɪv/vi.居住
/z/ 浊辅音5 z /zed/ 美 /ziː/ “z”、”ze”和”zz”一般发/z/这个音”s”和”se”有时也发/z/这个音”x”可发成包含/z/这个音的/gz/ zone/zəʊn/n.地带;is/ɪz/v.是;example/ɪg’zɑːmpəl/n.例子
/ʒ/ 浊辅音6 decision /dɪ’sɪʒ(ə)n/ 当si,su出现在后缀中时其中s常发/ʒ/音组合”ge”也常发/ʒ/ visual/‘vɪʒjʊəl/adj.视觉的;collision/kə’lɪʒ(ə)n/n.碰撞;rouge/ruːʒ/n.胭脂
/ð/ 浊辅音7 that /ðæt/ 发[ð]音的组合一般只有组合th they/ðeɪ/pron.他们;that/ðæt/pron.那
/r/ 浊辅音8 rain /reɪn/ 常见的发/r/音的和组合有r,rr,wr rise/raɪz/vi.上升
/dʒ/ 浊辅音9 g /dʒiː/ ;j /dʒeɪ/ 常见的及组合有g,dge,j发/dʒ/的音当组合du出现在词中有时也发/dʒ/ jar/dʒɑː/n.罐;page/peɪdʒ/n.页;procedure/prə’siːdʒə/n.程序
/dr/ 浊辅音10 drop /drɒp/ /dr/音一定是接元音,所以不会出现在单词的末尾,一般只有组合dr发/dr/音 drink/drɪŋk/vt.喝
/dz/ 浊辅音11 ds结尾cards /kɑ:dz/ 以d结尾的规则名词变为复数或规则动词变为第三人称单数时在词尾加s,单词结尾的组合ds发/dz/ beds/bedz/n.床上用品(复数);reads/ri:dz/vt.阅读(第三人称)
/m/ 浊辅音12 m /em/ 英语中发/m/这个音时一定有”m”,及组合有m,mm一般都发/m/当组合”mb”出现在单词末尾时,其中的b不发音,只发/m/音 mud/mʌd/n.泥;mail/meɪl/n.邮件;lamb/læm/n.羔羊
/n/ 浊辅音13 n /en/ 英语中发/n/这个音时一定有”n”,如及组合n,nn,gn发/n/ no/nəʊ/adv.不;nine/naɪn/num.九
/ŋ/ 浊辅音14 wing /wɪŋ/ 常见的发/ŋ/这个音的及组合有n,ngn后接k/g发/ŋ/,n后接t/d时发/n/ ring /rɪŋ/戒指 ;singer/‘sɪŋə/n.歌手
/l/ 浊辅音15 l /el/ 和组合有l,ll发清晰/l/音和组合有l,ll发模糊/l/音这个音是一个”成音节辅音” line/laɪn/n.路线;look/lʊk/vt.看;wheel/wiːl/n.车轮;play/pleɪ/vt.游戏
/j/ 浊辅音16 q /kjuː/;u /juː/;类似yeah /jeə/ 常见的发/j/音的有y,iu发复合元音/ju:/音,/j/包含在u音中 yak/jæk/n.牦牛;you/juː/pron.你;use/juːz/n.使用
/w/ 浊辅音17 y /wai/ 常见的发/w/音的和组合有w,whu也可发/w/音,一般在q后组成qu组合发/kw/音,另外one和once是特例 swan/swɒn/n.天鹅;win/wɪn/vi.赢;quite/kwaɪt/adv.很

docker快速部署springboot例子

docker快速部署springboot项目例子

前提:linux安装了docker和maven

步骤一:docker安装jdk

查找镜像

1
docker search  jdk 

从查找的里面拉取一个

1
docker  pull jdk镜像名

查看

1
docker run --rm gmaslowski/jdk:lastest java -version

步骤二:配置pom和写Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<docker.image.prefix>springdemo</docker.image.prefix>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/com.spotify/docker-client -->
<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>


<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

开放docker端口2375

1
vim /usr/lib/systemd/system/docker.service

修改

1
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \

重新加载docker配置

1
2
systemctl daemon-reload //加载docker守护线程
systemctl restart docker //重启docker

Dockerfile

390b58b1be42是docker里面已经部署好的jdk镜像ID

1
2
3
4
5
6
7
FROM 390b58b1be42
VOLUME /tmp
ADD demo-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 8002
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

把项目传到linux系统里/home目录下

步骤三:生成项目镜像

项目根目录执行

1
mvn package docker:build

查看,如第一个demo是生成的镜像

步骤四:启动

1
docker run -p 8002:8002 -t springdemo/demo

部署成功打开查看http://127.0.0.1:8002

本例子->下载

数据库常见的操作

数据库的操作

常见查询语句

  • 每个用户最新登录的时间

1
2
3
4
//每个用户最新登录的时间
select Max(date) from login group by user_id order by user_id;
//或者(先group分组用户,再在该组里order by排序limit取值)
SELECT (SELECT DATE FROM login WHERE user_id = l.user_id ORDER BY DATE DESC LIMIT 1)d FROM login l GROUP BY user_id
  • 每个用户最近一天登录的日子

1
2
3
//每个用户最近一天登录的日子,用户的名字,以及用户用的设备的名字,并且查询结果按照user的name升序排序
SELECT DISTINCT(u.name),c.name ,MAX(l.DATE) FROM login l,CLIENT c,USER u
WHERE l.user_id =u.id AND l.client_id=c.id GROUP BY l.user_id ORDER BY u.name
  • 查询分组里前2名

1
2
3
4
5
6
7
8
9
//分区函数partition by ,不重复排序row_number(),相同同名跳跃rank(),
//连续相同归一组dense_rank(),NTILE(group_num)将所有记录分成group_num个组每组序号一样
select g.id, l.name, g.score
from (select *,
dense_rank() over(partition by language_id order by score desc) as rank
from grade) g,
language l
where g.language_id = l.id and g.rank <= 2
order by l.name asc, g.score desc, g.id asc

LeetCode

  • 重新格式化部门表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| revenue | int |
| month | varchar |
+---------------+---------+
(id, month) 是表的联合主键。
这个表格有关于每个部门每月收入的信息。
月份(month)可以取下列值 ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]。
链接:https://leetcode-cn.com/problems/reformat-department-table
# Write your MySQL query statement below

select id,
max(if( month='Jan', revenue,null )) as Jan_Revenue,
max(if( month='Feb', revenue,null )) as Feb_Revenue,
max(if( month='Mar', revenue,null )) as Mar_Revenue,
max(if( month='Apr', revenue,null )) as Apr_Revenue,
max(if( month='May', revenue,null )) as May_Revenue,
max(if( month='Jun', revenue,null )) as Jun_Revenue,
max(if( month='Jul', revenue,null )) as Jul_Revenue,
max(if( month='Aug', revenue,null )) as Aug_Revenue,
max(if( month='Sep', revenue,null )) as Sep_Revenue,
max(if( month='Oct', revenue,null )) as Oct_Revenue,
max(if( month='Nov', revenue,null )) as Nov_Revenue,
max(if( month='Dec', revenue,null )) as Dec_Revenue
from Department group by id
  • 交换工资

id name sex salary
1 A m 2500
2 B f 1500
3 C m 5500
4 D f 500

运行你所编写的更新语句之后,将会得到以下表:

id name sex salary
1 A f 2500
2 B m 1500
3 C f 5500
4 D m 500
1
2
3
# Write your MySQL query statement below
#select s.id,s.name,(if(s.sex='f','m','f'))sex,s.salary from salary s
update salary set sex=if(sex='f','m','f')
  • 换座位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
假如数据输入的是上表,则输出结果如下:

+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
# Write your MySQL query statement below
#select id from seat where mod(id,2)=0 #and id< count(id);
#select * from seat a , seat b on
(b.id in (select * from (select id from seat where mod(id,2)=0)temp) and a.id=b.id-1)
#set a.student=b.student and b.student =a.student;

select * from (
select (if(mod(s.id,2)=1,if(s.id<(select count(*) from seat),s.id+1,s.id),s.id-1)) id,s.student
from seat s
)temp order by temp.id
  • 有趣的电影

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card| Interesting| 9.1 |
+---------+-----------+--------------+-----------+
对于上面的例子,则正确的输出是为:

+---------+-----------+--------------+-----------+
| id | movie | description | rating |
+---------+-----------+--------------+-----------+
| 5 | House card| Interesting| 9.1 |
| 1 | War | great 3D | 8.9 |
+---------+-----------+--------------+-----------+
# Write your MySQL query statement below

select * from cinema where mod(id,2)=1 and description!='boring'
order by rating desc
  • 超过5名学生的课

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
+---------+------------+
| student | class |
+---------+------------+
| A | Math |
| B | English |
| C | Math |
| D | Biology |
| E | Math |
| F | Computer |
| G | Math |
| H | Math |
| I | Math |
+---------+------------+
应该输出:

+---------+
| class |
+---------+
| Math |
+---------+
# Write your MySQL query statement below

select class from courses group by class having count(distinct student)>=5
  • 大的国家

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
+-----------------+------------+------------+--------------+---------------+
| name | continent | area | population | gdp |
+-----------------+------------+------------+--------------+---------------+
| Afghanistan | Asia | 652230 | 25500100 | 20343000 |
| Albania | Europe | 28748 | 2831741 | 12960000 |
| Algeria | Africa | 2381741 | 37100000 | 188681000 |
| Andorra | Europe | 468 | 78115 | 3712000 |
| Angola | Africa | 1246700 | 20609294 | 100990000 |
+-----------------+------------+------------+--------------+---------------+
如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。

编写一个SQL查询,输出表中所有大国家的名称、人口和面积。

例如,根据上表,我们应该输出:

+--------------+-------------+--------------+
| name | population | area |
+--------------+-------------+--------------+
| Afghanistan | 25500100 | 652230 |
| Algeria | 37100000 | 2381741 |
+--------------+-------------+--------------+
# Write your MySQL query statement below


select name,population ,area from World where area > 3000000 or population > 25000000;
  • 删除重复的电子邮箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id 是这个表的主键。
例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+
# Write your MySQL query statement below

#delete * from Person b where b.Id>(select min(id) from Person where Email =b.Email) ;

DELETE FROM Person WHERE Id NOT IN (select * from (SELECT MIN(Id)
FROM Person GROUP BY Email)tmp) ;
  • 部门工资前三高的所有员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Randy | 85000 |
| IT | Joe | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
# Write your MySQL query statement below

select d.Name as Department,e.Name as Employee ,e.Salary
from Department d , Employee e where e.DepartmentId = d.id
and (e.Salary >=(select Salary from Employee
where DepartmentId= d.id
group by Salary order by Salary desc limit 2,1)
or (select count(distinct Salary) from Employee where DepartmentId= d.id)<4);


  • 部门工资最高的员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Jim | 90000 | 1 |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
编写一个 SQL 查询,找出每个部门工资最高的员工。对于上述表,您的 SQL 查询应返回以下行(行的顺序无关紧要)。

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Jim | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
# Write your MySQL query statement below

select d.Name as Department,e.Name as Employee ,e.Salary
from Department d , Employee e
where e.DepartmentId = d.id and e. Salary =(select max(Salary) from Employee
where DepartmentId= d.id)
  • 从不订购的客户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:

+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:

+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
# Write your MySQL query statement below

select Name as Customers from Customers where Id not in
(select distinct CustomerId from Orders);
  • 查找重复的电子邮箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
根据以上输入,你的查询应返回以下结果:

+---------+
| Email |
+---------+
| a@b.com |
+---------+
# Write your MySQL query statement below
select Email from Person group by Email having count(id)>1;
  • 超过经理收入的员工

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

+----------+
| Employee |
+----------+
| Joe |
+----------+
# Write your MySQL query statement below
select a.Name as Employee from Employee a,Employee b
where a.ManagerId = b.Id and a.Salary>b.Salary;
  • 连续出现的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+
# Write your MySQL query statement below

select distinct(a.Num) as ConsecutiveNums from
Logs a,Logs b,Logs c
where a.id=b.id +1 and b.id= c.id+1
and a.Num=b.Num and b.Num=c.Num;
  • 分数排名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
# Write your MySQL query statement below
#SELECT a.Score, ((SELECT COUNT(DISTINCT b.Score) FROM Scores b
#WHERE a.Score < b.Score) + 1 )'Rank' FROM Scores a order by a.Score DESC;
SELECT Score, DENSE_RANK() OVER (ORDER BY Score DESC) AS Rank
FROM Scores ORDER BY Score DESC;
  • 第N高的薪水

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N=N-1;
RETURN (
# Write your MySQL query statement below.
select DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N, 1

);
END
  • 第二高的薪水

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
# Write your MySQL query statement below
SELECT (SELECT DISTINCT(Salary) FROM Employee
ORDER BY Salary DESC LIMIT 1,1) AS SecondHighestSalary;
  • 组合两个表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
表1: Person

+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId 是上表主键
表2: Address

+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId 是上表主键


编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:



FirstName, LastName, City, State
# Write your MySQL query statement below

select p.FirstName,p.LastName,a.City,a.State from Person p
left join Address a on p.PersonId = a.PersonId;

leetcode算法

LRU缓存

https://leetcode-cn.com/problems/lru-cache-lcci/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class LRUCache extends LinkedHashMap<Integer, Integer> {
private int capacity;
public LRUCache(int capacity) {
super(capacity,0.75f,true);
this.capacity=capacity;
}

public int get(int key) {
return super.getOrDefault(key,-1);
}

public void put(int key, int value) {
super.put(key, value);
}
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}


}

/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/

46全排列

给定一个 没有重复 数字的序列,返回其所有可能的全排列。

https://leetcode-cn.com/problems/permutations/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
List<List<Integer>> results = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
LinkedList<Integer> result = new LinkedList<Integer>();
backtrack(nums,result);
return results;
}

public void backtrack(int[] nums,LinkedList<Integer> result){
if(result.size() == nums.length) {
results.add(new LinkedList(result));
return;
}

for(int i=0;i<nums.length;i++){
if(result.contains(nums[i])) continue;
result.add(nums[i]);
backtrack(nums,result);
result.removeLast();
}

}



}

78子集

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

https://leetcode-cn.com/problems/subsets/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
List<List<Integer>> results = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
LinkedList<Integer> result = new LinkedList<Integer>();

backtrack(nums,result,0);
return results;
}

public void backtrack(int[] nums,LinkedList<Integer> result,int start){


for(int i=start;i<nums.length;i++){
result.add(nums[i]);
backtrack(nums,result,i+1);
result.removeLast();
}
results.add(new LinkedList(result));
}
}

494目标和

https://leetcode-cn.com/problems/target-sum/solution/

双指针类型, 二叉树的路径和 ,左节点是加,右节点是减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
int count=0;
public int findTargetSumWays(int[] nums, int S) {
backtrack(nums,S,0);
return count;
}

public void backtrack(int[] nums,int S,int start){
if (S==0 && nums.length==start ){
count++;
return;
}
if (start < nums.length){
backtrack(nums,S+nums[start],start+1);
backtrack(nums,S-nums[start],start+1);
}
}
}

26删除有序数组中的重复项

https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

双指针类型

//输入:nums = [0,0,1,1,1,2,2,3,3,4]输出:5, nums = [0,1,2,3,4]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int removeDuplicates(int[] nums) {
//双指针
if (nums.length<2) return nums.length;
int slow=0,fast=1;
while (fast<nums.length){
if (nums[fast] == nums[slow]){
fast++;
} else {
nums[++slow]=nums[fast++];
}
}
return slow+1;
}
}

24 两两交换链表中的节点

给定 1->2->3->4, 你应该返回 2->1->4->3.

https://leetcode-cn.com/problems/swap-nodes-in-pairs/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
return reverseKGroup(head,2);
}

public ListNode reverseKGroup(ListNode head, int k) {
ListNode temp = head;
// 先用temp找到3的位置,不足,就返回head不反转
for(int i=1;i<k && temp!=null;i++){
temp=temp.next;
}
if (temp == null) return head;
// 用t2记录记录4的位置,把三四断开
ListNode t2 = temp.next;
temp.next=null;
// 反转前部分,头部设置为newHead,此时head在3的位置了
ListNode newHead = reListNode(head);
// 把1-2-3与后面的连接起来,后面的继续递归:head.next=reverseKGroup(t2,k);
head.next=reverseKGroup(t2,k);
return newHead;
}

private ListNode reListNode(ListNode head){
ListNode next=null;
ListNode pre=null;
while (head!=null){
next=head.next;
head.next=pre;
pre=head;
head=next;

}
return pre;
}
}

110. 平衡二叉树

给定一个二叉树,判断它是否是高度平衡的二叉树。

https://leetcode-cn.com/problems/balanced-binary-tree/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
int l= getH(root.left);
int r=getH(root.right);
int abs =Math.abs(l-r);
if(abs>1) return false;
return isBalanced(root.left) && isBalanced(root.right);
}
public int getH(TreeNode root){
if(root==null) return 0;
int left = getH(root.left);
int right = getH(root.right);
int result = left>right?left+1:right+1;
return result;
}
}

53. 最大子序和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int maxSubArray(int[] nums) {
//贪心算法, 前i-1个最大值是max,第i-1个前连续最大值为sum
if(nums.length<1) return Integer.MIN_VALUE;
int sum=nums[0]; //前i-1个最大值
int max=sum;
for(int i=1;i<nums.length;i++){
if(sum<0){
sum=nums[i];
}else{
sum+=nums[i];
}
max=Math.max(sum, max);
}

return max;
}
}

25. K 个一组翻转链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {

public ListNode reverseKGroup(ListNode head, int k) {
ListNode temp = head;
for(int i=1;i<k && temp!=null;i++){
temp=temp.next;
}
if (temp == null) return head;
ListNode t2 = temp.next;
temp.next=null;
ListNode newHead = reListNode(head);
head.next=reverseKGroup(t2,k);
return newHead;
}

private ListNode reListNode(ListNode head){
ListNode next=null;
ListNode pre=null;
while (head!=null){
next=head.next;
head.next=pre;
pre=head;
head=next;

}
return pre;
}
}

451. 根据字符出现频率排序

https://leetcode-cn.com/problems/sort-characters-by-frequency/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public String frequencySort(String s) {
TreeMap<Character,Integer> map =new TreeMap<Character, Integer>();
char[] chars =s.toCharArray();
for (char num : chars){
if (map.containsKey(num)){
map.put(num,map.get(num)+1);
}else {
map.put(num,1);
}
}

List<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character, Integer>>(map.entrySet());

Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>() {
@Override
public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
StringBuilder sb = new StringBuilder();
for (int i=0;i<list.size();i++){
Map.Entry<Character,Integer> entry =list.get(i);
int value=entry.getValue();
while (value-->0) sb.append(entry.getKey());
}
return sb.toString();
}
}

347. 前 K 个高频元素

https://leetcode-cn.com/problems/top-k-frequent-elements/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int[] topKFrequent(int[] nums, int k) {
int[] result = new int[k];
TreeMap<Integer,Integer> map =new TreeMap<Integer, Integer>();
for (int num : nums){
if (map.containsKey(num)){
map.put(num,map.get(num)+1);
}else {
map.put(num,1);
}
}

List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());

Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
for(int i=0;i<k;i++){
result[i] = list.get(i).getKey();
}
return result;
}
}

414. 第三大的数

https://leetcode-cn.com/problems/third-maximum-number/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int thirdMax(int[] nums) {
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
for (int num :nums){
if (!queue.contains(num)){
queue.add(num);
}
if (queue.size()>3) queue.poll();
}

if(queue.size()<3){
while (queue.size()>1) queue.poll();
return queue.peek();
}else{
return queue.peek();
}

}
}

703. 数据流中的第K大元素

https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class KthLargest {
private PriorityQueue<Integer> queue;
private int k;
public KthLargest(int k, int[] nums) {
queue = new PriorityQueue<Integer>(k);
this.k=k;
for (int num :nums){
queue.add(num);
if (queue.size()>k) queue.poll();
}
}

public int add(int val) {
queue.add(val);
if (queue.size()>k) queue.poll();
return queue.peek();
}
}

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

第 k 个数

有些数的素因子只有 3,5,7,请设计一个算法找出第 k 个数。注意,不是必须有这些素因子,而是必须不包含其他的素因子。例如,前几个数按顺序应该是 1,3,5,7,9,15,21。

https://leetcode-cn.com/problems/get-kth-magic-number-lcci\

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int getKthMagicNumber(int k) {
Queue<Long> queue = new PriorityQueue();
HashSet<Long> set = new HashSet<Long>();
queue.add(1l);
while (true){
Long temp=queue.poll();
if (!set.contains(temp)){
set.add(temp);
queue.add(temp*3);
queue.add(temp*5);
queue.add(temp*7);
}
if (set.size()==k) return temp.intValue();
}
}
}

215. 数组中的第K个最大元素

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

1
2
3
4
5
6
7
8
9
10
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> queue = new PriorityQueue();
for (int num : nums){
queue.add(num);
if (queue.size()>k) queue.poll();
}
return queue.peek();
}
}

121. 买卖股票的最佳时机

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

如果你最多只允许完成一笔交易(即买入和卖出一支股票一次),设计一个算法来计算你所能获取的最大利润。

注意:你不能在买入股票前卖出股票。
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int maxProfit(int[] prices) {
if(prices.length<1) return 0;
int max=0; //前i个最大利润
int minI=prices[0];//前i-1个最小值
for(int i = 1 ;i < prices.length ;i++){
minI=minI<prices[i]?minI:prices[i];
max=max>prices[i]-minI? max :prices[i]-minI;
}

return max;
}
}

122. 买卖股票的最佳时机 II

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii

1
2
3
4
5
6
7
8
9
10
class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int max=0; //相邻利润的总和
for(int i = 1 ;i < prices.length ;i++){
max=prices[i]-prices[i-1]>0?max+prices[i]-prices[i-1]:max;
}
return max;
}
}

70. 爬楼梯

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int climbStairs(int n) {
if(n<1) return 0;
else if(n==1) return 1;
else if(n==2) return 2;
else if(n==3) return 3;
else{
int[] dp = new int[n];
dp[0]=1;
dp[1]=2;
dp[2]=3;
for(int i=3;i<n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n-1];
}

}
}

跳水板

你正在使用一堆木板建造跳水板。有两种类型的木板,其中长度较短的木板长度为shorter,长度较长的木板长度为longer。你必须正好使用k块木板。编写一个方法,生成跳水板所有可能的长度。

返回的长度需要从小到大排列。
链接:https://leetcode-cn.com/problems/diving-board-lcci

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public int[] divingBoard(int shorter, int longer, int k) {
if(k==0){
return new int[]{};
}else if(shorter == longer ||k==0){
return new int[]{shorter*k};
}else{
TreeSet<Integer> set = new TreeSet<Integer>();
for(int i=1;i<=k;i++){
int t=k-i;
set.add(i*shorter+t*longer);
set.add(t*shorter+i*longer);
}

int[] result = new int[set.size()];
Iterator<Integer> iterator=set.iterator();
int i=0;
while(iterator.hasNext()){
result[i++]=iterator.next();
}
return result;

}

}
}

3. 无重复字符的最长子串

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public int lengthOfLongestSubstring(String s) {
int maxLen=1;
if(s.length()<1) return 0;
else if(s.length()==1) return 1;

for(int i=0;i<s.length()-1;i++){//从第一个开始
List<Character> list=new ArrayList<Character>();
list.add(s.charAt(i));
//i开始的 n个不重复的子序列
for(int j=i+1;j<s.length();j++){
if(list.contains(s.charAt(j))){
break;
}else{
list.add(s.charAt(j));
}
}

maxLen=maxLen>list.size()?maxLen:list.size();

}
return maxLen;

}
}

20. 有效的括号

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

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
注意空字符串可被认为是有效字符串。
链接:https://leetcode-cn.com/problems/valid-parentheses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean isValid(String s) {
int len = s.length();
//个数是单数,就闭合不了
if(len%2==1) return false;
Stack<Character> stack = new Stack<Character>();
for(int i=0 ; i < len ; i++){
char ch = s.charAt(i);
if(ch=='(' || ch=='{' || ch=='['){
stack.add(ch);
}else{
//判断栈顶元素是否匹配
if(stack.isEmpty()) return false;

char topC = stack.pop();
if( topC =='(') topC=')';
else if(topC=='{') topC='}';
else if(topC=='[') topC=']';

if(topC!=ch) return false;
}

}
if(stack.isEmpty())
return true;
else return false;

}

}

32. 最长有效括号

给定一个只包含 ‘(‘ 和 ‘)’ 的字符串,找出最长的包含有效括号的子串的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int longestValidParentheses(String s) {
int max = 0;
Stack<Integer> stack = new Stack<Integer>();
stack.add(-1);
for(int i=0;i<s.length();i++ ){
char ch = s.charAt(i);
if(ch == '(') {
stack.add(i);
}else{

stack.pop();
if(stack.isEmpty()) stack.add(i);

max = Math.max(max, i-stack.peek());
}
}

return max;

}


}

链表求和

输入:(7 -> 1 -> 6) + (5 -> 9 -> 2),即617 + 295
输出:2 -> 1 -> 9,即912

输入:(6 -> 1 -> 7) + (2 -> 9 -> 5),即617 + 295
输出:9 -> 1 -> 2,即912

https://leetcode-cn.com/problems/sum-lists-lcci/submissions/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null) return l2;
else if(l2 == null) return l1;
else{
int forward =0;
ListNode head = null,p = null;
boolean first =true;
while(l1 !=null || l2 !=null){
if(l1!=null) {
forward +=l1.val;
l1=l1.next;
}
if(l2!=null) {
forward +=l2.val;
l2=l2.next;
}
if(first){
p= new ListNode(forward%10);
forward/=10;
head=p;
first=false;
}else{
p.next= new ListNode(forward%10);
forward/=10;
p=p.next;
}

}
if(forward>0) p.next=new ListNode(forward);
return head;
}
}

}

540. 有序数组中的单一元素

给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。

1
2
3
4
5
6
7
8
9
10
class Solution {
public int singleNonDuplicate(int[] nums) {
for(int i=0;i<nums.length-1;i+=2){
if(nums[i]!=nums[i+1]){
return nums[i];
}
}
return nums[nums.length-1];
}
}

455. 分发饼干

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。

输入: [1,2,3], [1,1]

输出: 1

解释:
你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
所以你应该输出1。
链接:https://leetcode-cn.com/problems/assign-cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int gi=0,si=0;
while(gi<g.length&&si<s.length){
if(g[gi]<=s[si]){//满足
gi++;
si++;
}else{
si++;
}
}
return gi;
}
}

452. 用最少数量的箭引爆气球

输入:
[[10,16], [2,8], [1,6], [7,12]]

输出:
2

解释:
对于该样例,我们可以在x = 6(射爆[2,8],[1,6]两个气球)和 x = 11(射爆另外两个气球)。
链接:https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int findMinArrowShots(int[][] points) {
if(points.length<1) return 0;
Arrays.sort(points, (a,b) ->(a[1]-b[1]));
int count=1;
int temp=points[0][1];
for(int i=1;i<points.length;i++){
if(temp>=points[i][0]){
continue;
}

temp=points[i][1];
count++;
}
return count;
}
}

605. 种花问题

输入: flowerbed = [1,0,0,0,1], n = 1
输出: True

输入: flowerbed = [1,0,0,0,1], n = 2
输出: False

数组内已种好的花不会违反种植规则。
输入的数组长度范围为 [1, 20000]。
n 是非负整数,且不会超过输入数组的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
for(int i=0;i<flowerbed.length;i++){
if(flowerbed[i]==1) continue;
int pre=i>0?flowerbed[i-1]:0;
int next=(i+1)<flowerbed.length?flowerbed[i+1]:0;
if(pre==0&&next==0){
n--;
if(n<=0) return true;
flowerbed[i]=1;

}
}
return n<=0?true:false;
}
}

665. 非递减数列

输入: nums = [4,2,3]
输出: true
解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列。

输入: nums = [4,2,1]
输出: false
解释: 你不能在只改变一个元素的情况下将其变为非递减数列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public boolean checkPossibility(int[] nums) {
int count=0;
for(int i=1;i<nums.length;i++){
if(nums[i-1]>nums[i]){
count++;
if(i-2>=0&&nums[i-2]>nums[i]){
nums[i]=nums[i-1];
}
else{
nums[i-1]=nums[i];
}

}
}

return count<=1;
}
}

392. 判断子序列

示例 1:
s = “abc”, t = “ahbgdc”

返回 true.

示例 2:
s = “axc”, t = “ahbgdc”

返回 false.
链接:https://leetcode-cn.com/problems/is-subsequence

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public boolean isSubsequence(String s, String t) {
int i=0,j=0,sl=s.length(),tl=t.length();
while(i<sl&&j<tl){
if(s.charAt(i)==t.charAt(j)){
i++;
}
j++;
}

return i==sl;
}
}

763. 划分字母区间

输入:S = “ababcbacadefegdehijhklij”
输出:[9,7,8]
解释:
划分结果为 “ababcbaca”, “defegde”, “hijhklij”。
每个字母最多出现在一个片段中。
像 “ababcbacadefegde”, “hijhklij” 的划分是错误的,因为划分的片段数较少。
链接:https://leetcode-cn.com/problems/partition-labels

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public List<Integer> partitionLabels(String S) {
int startIndex=0;
int endIndex=S.lastIndexOf(S.charAt(startIndex));
List<Integer> result =new ArrayList<Integer>();
while(endIndex<=(S.length()-1)){

for(int i=startIndex+1;i<endIndex;i++){
int tmpIndex=S.lastIndexOf(S.charAt(i));
if(tmpIndex>endIndex){
endIndex=tmpIndex;
}
}
result.add(endIndex-startIndex+1);
if(endIndex+1>=S.length()){
break;
}
startIndex=endIndex+1;
endIndex=S.lastIndexOf(S.charAt(startIndex));

}

return result;
}
}

字符串压缩

输入:”aabcccccaaa”
输出:”a2b1c5a3”

输入:”abbccd”
输出:”abbccd”
解释:”abbccd”压缩后为”a1b2c2d1”,比原字符串长度更长。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public String compressString(String S) {
if (S.length()<2) return S;
StringBuilder sb = new StringBuilder();
char[] chs = S.toCharArray();
char startCh=chs[0];
int sameCout=1;
for(int i=1;i<chs.length;i++){
if (startCh == chs[i]){
sameCout++;
}else {
sb.append(startCh);
sb.append(sameCout);
startCh=chs[i];
sameCout=1;
}
}
sb.append(startCh);
sb.append(sameCout);
return sb.toString().length()<chs.length?sb.toString():S;
}
}

按摩师

示例 1:

输入: [1,2,3,1]
输出: 4
解释: 选择 1 号预约和 3 号预约,总时长 = 1 + 3 = 4。
示例 2:

输入: [2,7,9,3,1]
输出: 12
解释: 选择 1 号预约、 3 号预约和 5 号预约,总时长 = 2 + 9 + 1 = 12。
示例 3:

输入: [2,1,4,5,3,1,1,3]
输出: 12
解释: 选择 1 号预约、 3 号预约、 5 号预约和 8 号预约,总时长 = 2 + 4 + 3 + 3 = 12。
链接:https://leetcode-cn.com/problems/the-masseuse-lcci

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int massage(int[] nums) {
if(nums.length<1) return 0;
else if(nums.length==1) return nums[0];
else if(nums.length==2) return nums[0]>nums[1]?nums[0]:nums[1];
int preMax=0;
int max=0;
for(int i = 0;i < nums.length ; i++){
int temp =Math.max(preMax+nums[i],max);
preMax=max;
max=temp;

}
return max;
}
}

198. 打家劫舍

示例 1:

输入:[1,2,3,1]
输出:4
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
示例 2:

输入:[2,7,9,3,1]
输出:12
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
链接:https://leetcode-cn.com/problems/house-robber

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int rob(int[] nums) {
if(nums.length<1) return 0;
else if(nums.length==1) return nums[0];
else if(nums.length==2) return nums[0]>nums[1]?nums[0]:nums[1];
int preMax=0;
int max=0;
for(int i = 0;i < nums.length ; i++){
int temp =Math.max(preMax+nums[i],max);
preMax=max;
max=temp;

}
return max;
}
}

213. 打家劫舍 II

示例 1:

输入: [2,3,2]
输出: 3
解释: 你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
示例 2:

输入: [1,2,3,1]
输出: 4
解释: 你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
偷窃到的最高金额 = 1 + 3 = 4 。
链接:https://leetcode-cn.com/problems/house-robber-ii

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public int rob(int[] nums) {
if(nums.length<1) return 0;
else if(nums.length==1) return nums[0];
return Math.max(massage(Arrays.copyOfRange(nums,0,nums.length-1)),
massage (Arrays.copyOfRange(nums,1,nums.length)));

}

public int massage(int[] nums) {
if(nums.length<1) return 0;
else if(nums.length==1) return nums[0];
else if(nums.length==2) return nums[0]>nums[1]?nums[0]:nums[1];
int preMax=0;
int max=0;
for(int i = 0;i < nums.length ; i++){
int temp =Math.max(preMax+nums[i],max);
preMax=max;
max=temp;

}
return max;
}
}

386. 字典序排数

给定一个整数 n, 返回从 1 到 n 的字典顺序。

例如,

给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] 。

请尽可能的优化算法的时间复杂度和空间复杂度。 输入的数据 n 小于等于 5,000,000。
链接:https://leetcode-cn.com/problems/lexicographical-numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public List<Integer> lexicalOrder(int n) {
List<Integer> list = new ArrayList<Integer>();
for(int i=1;i<=n;i++){
list.add(i);
}
Collections.sort(list, new Comparator<Integer>() {

@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
return String.valueOf(o1).compareTo(String.valueOf(o2));


}
});
return list;
}
}

汉诺塔问题

示例1:

输入:A = [2, 1, 0], B = [], C = []
输出:C = [2, 1, 0]
示例2:

输入:A = [1, 0], B = [], C = []
输出:C = [1, 0]
链接:https://leetcode-cn.com/problems/hanota-lcci

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public void hanota(List<Integer> A, List<Integer> B, List<Integer> C) {
hanota(A.size(), A, B, C);
}
//n个A移到C
public void hanota(int n, List<Integer> A, List<Integer> B, List<Integer> C) {
if(n==1){
C.add(A.remove(A.size()-1));
}else{
hanota(n-1, A, C, B);
hanota(1, A, B, C);
hanota(n-1, B, A, C);
}
}
}

长度为k的最大子数组

-2, -1, 2, 1,k=1
0:-1
-2:0
-3:1
max:2
-1:2
max:2

1
2
3
4
5
6
7
8
9
10
11
12
public int maxSubArrayLen(int[] nums, int k) {
//key:存第一次出现的值 value:下标i
HashMap<Integer,Integer> map= new HashMap<>();
map.put(0,-1);
int max=0,sum = 0;
for (int i=0 ; i<nums.length ; i++){
sum += nums[i];
if (map.containsKey(sum-k)) max=Math.max(max,i-map.get(sum-k));
if (!map.containsKey(sum)) map.put(sum,i);
}
return max;
}

416分割等和子集

数组分割成两个和相等的子集

https://leetcode-cn.com/problems/partition-equal-subset-sum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
比如1, 5, 11, 5

[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1]
[1, 1, 0, 0, 0, 2, 2, 0, 0, 0, 1, 2]

//01背包
for (int i = 0; i < n; i++) {
for (int j = m; j >= V[i]; j--) {
f[j] = max(f[j], f[j-V[i]] + W[i]);
}
}
//完全背包
for (int i = 0; i < n; i++) {
for (int j = V[i]; j <= m; j++) {
f[j] = max(f[j], f[j-V[i]] + W[i]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public boolean canPartition(int[] nums) {//1, 5, 11, 5
int sum=0;
for(int num : nums){
sum += num;
}
if (sum %2 ==1) return false;
int len = sum/2 ;
int[] dp=new int[len+1];
dp[0]=1;//初始化[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for(int num : nums){//len到num(大到小,每隔num个数加1),比如num=1执行完[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
for(int i=len;i>num;i--){
dp[i] +=dp[i-num];//第i个物品,有拿和不拿两种情况,dp[i]表示不拿的情况,dp[i-num]表示拿的情况,因此要将两者相加
}
}
return dp[len]>0;
}
}

322零钱兑换

凑成总金额所需的最少的硬币个数完全背包

coins = [1, 2, 5], amount = 11

[0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 12, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 12, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 12, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 12, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 12, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 12, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

[0, 1, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 5, 6, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 6, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 7, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 8, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 9, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 10, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 11]
[0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6]

[0, 1, 1, 2, 2, 1, 3, 4, 4, 5, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 4, 4, 5, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 4, 5, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 5, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 5, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 6]
[0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int coinChange(int[] coins, int amount) {
int max = amount+1;//表示最大值,比amount大
int[] dp = new int[amount+1];//dp价值在这里代表个数
for (int i=0;i<dp.length;i++){
dp[i]=max;
}
dp[0] = 0;//金额为0的时候,需要0个硬币
for (int coin : coins) {
for (int j = coin; j <= amount; j++) {
dp[j] = Math.min(dp[j], dp[j-coin] +1);//公式代表个数所以+1
}
}
return dp[amount]==max?-1:dp[amount];
}
}

1143最长公共子序列

最长公共子序列(LCS)

https://leetcode-cn.com/problems/longest-common-subsequence/solution/dong-tai-gui-hua-jie-lcswen-ti-by-cyinge-k3zz/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"abcde","ace"
[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
[0, 1, 2, 3]
​```
[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 0, 0, 0]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
[0, 0, 0, 0]

[0, 0, 0, 0]
[0, 1, 1, 1]
[0, 1, 1, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
[0, 1, 2, 3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
//动态规划
public int longestCommonSubsequence(String text1, String text2) {
int len1=text1.length(),len2=text2.length();
char[] cs1=text1.toCharArray(),cs2=text2.toCharArray();
int[][] dp = new int[len1+1][len2+1]; //dp代表相同的个数
for(int i=1;i<=len1;i++){
for (int j=1;j<=len2;j++){
if (cs1[i-1]==cs2[j-1]){
dp[i][j]=dp[i-1][j-1]+1;
}else {
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[len1][len2];
}
}

14最长公共前缀

https://leetcode-cn.com/problems/longest-common-prefix/

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs.length<1) return "";
for (int i=1;i<strs.length;i++){
while (!strs[i].startsWith(strs[0])){
strs[0]=strs[0].substring(0,strs[0].length()-1);
}
}
return strs[0];
}
}

二叉树遍历

144二叉树的前序遍历

https://leetcode.cn/problems/binary-tree-preorder-traversal/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preBt(root,list);
return list;
}

private void preBt(TreeNode root, List<Integer> list) {
if (root==null){
return;
}
list.add(root.val);
preBt(root.left,list);
preBt(root.right,list);
}

}
94二叉树的中序遍历

https://leetcode.cn/problems/binary-tree-inorder-traversal/description/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {

public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
inBt(root,list);
return list;
}

private void inBt(TreeNode root, List<Integer> list) {
if (root==null){
return;
}

inBt(root.left,list);
list.add(root.val);
inBt(root.right,list);

}


}
145二叉树的后序遍历

https://leetcode.cn/problems/binary-tree-postorder-traversal/description/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root!=null){
postOrder(root,list);
}
return list;
}

private void postOrder(TreeNode root, List<Integer> list) {
if (root.left!=null){
postOrder(root.left,list);
}
if (root.right!=null){
postOrder(root.right,list);
}
list.add(root.val);
}
}
102二叉树的层序遍历

https://leetcode.cn/problems/binary-tree-level-order-traversal/description/

1
2
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root!=null){
queue.add(root);
}
while (!queue.isEmpty()){
List<Integer> list = new ArrayList<>();
int size = queue.size();
for (int i=0;i<size;i++){
TreeNode poll =queue.poll();
if (poll==null){
break;
}
list.add(poll.val);
if (poll.left!=null){
queue.add(poll.left);
}
if (poll.right!=null) queue.add(poll.right);
}
result.add(list);
}
return result;
}
}

125验证回文串

https://leetcode.cn/problems/valid-palindrome/description/

1
2
3
输入: s = "A man, a plan, a canal: Panama"
输出:true
解释:"amanaplanacanalpanama" 是回文串。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
int left = 0,right = s.length() - 1;
while (left<s.length() && !isLetter(s.charAt(left))){
left++;
}
while (right >0 && !isLetter(s.charAt(right))){
right--;
}
//比较字母a和字母b的char是否相同忽略大小写
while (left <= right) {
int a = Character.toUpperCase(s.charAt(left));
int b = Character.toUpperCase(s.charAt(right));
if (Character.toUpperCase(s.charAt(left))!= Character.toUpperCase(s.charAt(right))) {
return false;
}
left++;
right--;
while (left<s.length() && !isLetter(s.charAt(left))){
left++;
}
while (right > 0 && !isLetter(s.charAt(right))){
right--;
}
}
return true;
}
//判断char是不是字母
public boolean isLetter(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')|| (c >= '0' && c <= '9');
}
}

LCR 179.查找总价格为目标值的两个商品

https://leetcode.cn/problems/he-wei-sde-liang-ge-shu-zi-lcof/description/

1
2
输入:price = [3, 9, 12, 15], target = 18
输出:[3,15] 或者 [15,3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int[] twoSum(int[] price, int target) {
int length = price.length,left = 0, right = length-1;
while (left<right){
if (price[left]+price[right]>target){
right--;
}else if (price[left]+price[right]<target){
left++;
}else {
return new int[]{price[left],price[right]};
}
}
return new int[]{};
}
}

LCR 010和为 K 的子数组

https://leetcode.cn/problems/QTMn0o/description/

1
2
3
输入:nums = [1,1,1], k = 2
输出: 2
解释: 此题 [1,1][1,1] 为两种不同的情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int subarraySum(int[] nums, int k) {
int count = 0, left = 0;
while (left < nums.length) {
if (nums[left] == k) count++;
int res = k - nums[left];
int i = left + 1;
while (i < nums.length) {
res = res - nums[i];
if (res == 0) {
count++;
}
i++;
}
left++;
}
return count;
}
}

LCR 153二叉树中和为目标值的路径

https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/description/

1
2
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
输出:[[5,4,11,2],[5,8,4,5]]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public List<List<Integer>> pathTarget(TreeNode root, int target) {
List<List<Integer>> result = new ArrayList<>();
if (root!=null){
bt(root,target,new ArrayList<>(),result);
}
return result;
}
private void bt(TreeNode root, int target, List<Integer> list,List<List<Integer>> result) {
list.add(root.val);
if (root.left==null && root.right==null){
if (list.stream().mapToInt(Integer::intValue).sum()==target){
result.add(new ArrayList<>(list));//不能直接add(list)
}
}else {
if (root.left!=null){
bt(root.left,target,list,result);
}
if (root.right!=null){
bt(root.right,target,list,result);
}
}
list.remove(list.size()-1);
}
}

1038从二叉搜索树到更大和树

https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/description/

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
int sum =0;
public TreeNode bstToGst(TreeNode root) {
if (root != null) {
bstToGst(root.right);
sum += root.val;
root.val = sum;
bstToGst(root.left);
}
return root;
}
}
  • Copyrights © 2020-2024 Lin