当前位置:网站首页>Leetcode question brushing (I) -- double pointer (go Implementation)
Leetcode question brushing (I) -- double pointer (go Implementation)
2022-06-30 10:21:00 【Fallacious】
leetcode Question brushing and sorting series
leetcode Brush problem ( One ) — Two pointer thought
leetcode Brush problem ( Two ) — Sequencing ideas
leetcode Brush problem ( 3、 ... and ) — Dichotomous thinking
leetcode Brush problem ( Four ) — Greedy thoughts
leetcode Brush question type ( One )— Double pointer type
In an ordered array or linked list , The two pointers move left and right ( Dual pointers are divided into dual pointers in the same direction and dual pointers in the opposite direction ), Find some combinations that meet certain constraints .
Classic title
167 The sum of two numbers of an ordered array
Given a has been according to Non decreasing order Array of integers for numbers , Please find out two numbers from the array, and the sum of them is equal to the target number target .
func twoSum(numbers []int, target int) []int {
i := 0
j := len(numbers) - 1
a := make([]int, 2)
for i < j {
if numbers[i]+numbers[j] == target {
a[0] = i + 1
a[1] = j + 1
break
} else {
if numbers[i]+numbers[j] > target {
// And greater than the target value , Indicates that the right value should be reduced
j--
} else {
i++
}
}
}
return a
}
633 Sum of squares
Given a nonnegative integer c , You have to decide if there are two integers a and b, bring a2 + b2 = c .
func judgeSquareSum(c int) bool {
i := 0
j := (int)(math.Sqrt(float64(c))) //0^2+j^2=c, therefore j The biggest is c The square root of , Pruning reduces time complexity
for i <= j {
m := i*i + j*j
if m == c {
return true
} else {
if m < c {
i++
} else {
j--
}
}
}
return false
}
345 Reverse vowels in a string
Give you a string s , Invert only all vowels in the string , And return the result string .
func reverseVowels(s string) string {
// Add all vowels to map
dict := make(map[uint8]int)
dict['a'] = 1
dict['e'] = 1
dict['o'] = 1
dict['u'] = 1
dict['i'] = 1
dict['A'] = 1
dict['E'] = 1
dict['O'] = 1
dict['U'] = 1
dict['I'] = 1
// Convert the string to byte Array
arr := []byte(s)
length := len(arr)
i := 0
j := length - 1
for true {
for i < length {
if _, ok := dict[arr[i]]; ok {
// The left pointer finds the vowel letter
break
}
i++
}
for j > 0 {
if _, ok := dict[arr[j]]; ok {
// The right pointer finds the vowel letter
break
}
j--
}
if i >= j {
// After the left and right pointers meet, the exchange ends
break
}
// Swap left and right pointer vowels
tmp := arr[i]
arr[i] = arr[j]
arr[j] = tmp
i++
j--
}
return string(arr)
}
There is also the classic quick sort ~
If there is any wrong , Please point out ~
Reference from :
http://www.cyc2018.xyz/
https://leetcode-cn.com/problems
边栏推荐
- How to seize the opportunity of NFT's "chaos"?
- MIT-6874-Deep Learning in the Life Sciences Week6
- Quick completion guide for mechanical arm (V): end effector
- Rider does not prompt after opening unity script
- Shell script multi loop experiment
- 跳跃表介绍
- L'activité "Kunming City coffee map" a rouvert
- 机械臂速成小指南(四):机械臂关键部件之减速机
- G code explanation | list of the most important G code commands
- The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
猜你喜欢

MIT-6874-Deep Learning in the Life Sciences Week5

2022第六季完美童模 合肥赛区 初赛圆满落幕

Didn't receive robot state (joint angles) with recent timestamp within 1 seconds

Why can't you rob scientists of NFT

逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展

How does the diode work?

Get through the supply chain Shenzhen gift show helps cross-border e-commerce find ways to break the situation

MIT-6874-Deep Learning in the Life Sciences Week5

7. development of mobile login function

Dyson design award, changing the world with sustainable design
随机推荐
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
1033 To Fill or Not to Fill
Use and description of event delegation
[AGC] build service 3- authentication service example
孙安民作品《莲花净心》数字藏品上线长城数艺
【JVM】G1垃圾回收器簡述
JUL简介
KOREANO ESSENTIAL打造气质职场范
背课文记单词,读课文记单词,读文章记单词;40篇文章搞定3500词;71篇文章突破中考单词;15篇文章贯通四级词汇;15篇文章贯通六级词汇
Node environment configuration
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion
机器人系统动力学——惯性参数
新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
Detailed explanation of commissioning methods and techniques
UAV project tracking record 83 -- PCB diagram completion
Some domestic image sources
Nlopt -- Nonlinear Optimization -- principle introduction and application method
South China Industrial Group launched digital economy and successfully held the city chain technology conference
"Kunming City coffee map" was opened again, and coffee brought the city closer
事件流的说明》