当前位置:网站首页>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
边栏推荐
- Enter the world of helium (hNT) hotspot servers to bring you different benefits
- Detailed explanation of SolidWorks mass characteristics (inertia tensor, moment of inertia, inertia spindle)
- 陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军
- Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home
- 戴森设计大奖,以可持续化设计改变世界
- log4j
- 采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.
- Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
- MIT-6874-Deep Learning in the Life Sciences Week5
- MySQL index, transaction and storage engine of database (3)
猜你喜欢

SolidWorks质量特性详解(惯性张量、转动惯量、惯性主轴)

新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动

开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军

Installation and use

MIT-6874-Deep Learning in the Life Sciences Week6

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

9. cache optimization

Detailed explanation of commissioning methods and techniques

Rider打开Unity脚本后没有提示
随机推荐
unable to convert expression into double array
Input limit input
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
Use and description of event delegation
The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
ModuleNotFoundError: No module named ‘_swigfaiss‘
log4j
Article content cannot be copied
“昆明城市咖啡地图”活动再度开启
Brève description du collecteur d'ordures G1
Appium automation test foundation - ADB shell command
《锦绣中华》中老年公益文旅游-走进佛山敬老院
Robot system dynamics - inertia parameters
ModuleNotFoundError: No module named ‘_ swigfaiss‘
Rider打开Unity脚本后没有提示
【JVM】CMS简述
力扣 428. 序列化和反序列化 N 叉树 DFS
Splendid China: public welfare tourism for the middle-aged and the elderly - entering Foshan nursing home
log4j