当前位置:网站首页>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
边栏推荐
- NLopt--非线性优化--原理介绍及使用方法
- Node environment configuration
- Rider打开Unity脚本后没有提示
- [JVM] G1 garbage collector
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- Launch of Rural Revitalization public welfare fund and release of public welfare bank for intangible cultural heritage protection of ancient tea tree
- 戴森设计大奖,以可持续化设计改变世界
- Shell script functions
- Compare the maximum computing power of the Cenozoic top ant s19xp and the existing s19pro in bitland
- log4j
猜你喜欢
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
Appium automation test foundation - ADB shell command
华南产业集团发力数字经济,城链科技发布会成功召开
KOREANO ESSENTIAL打造气质职场范
Yixian e - commerce publie un rapport trimestriel: adhérer à la R & D et à l’investissement de la marque, réaliser un développement durable et de haute qualité
“昆明城市咖啡地圖”活動再度開啟
Shell script functions
调试方法和技巧详解
L'activité "Kunming City coffee map" a rouvert
机械臂速成小指南(四):机械臂关键部件之减速机
随机推荐
逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展
‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
ModuleNotFoundError: No module named ‘_swigfaiss‘
“昆明城市咖啡地图”活动再度开启
Jinbei LT6 is powerful in the year of the tiger, making waves
Rider does not prompt after opening unity script
机器人系统动力学——惯性参数
G 代码解释|最重要的 G 代码命令列表
MySQL advanced SQL statement of database (1)
C语言实现扫雷游戏,附详解及完整代码
调试方法和技巧详解
[AGC] build service 3- authentication service example
keras ‘InputLayer‘ object is not iterable
unable to convert expression into double array
[C language quick start] let you know C language and get started with zero basics ③
MySQL index, transaction and storage engine of database (1)
Action bright: take good care of children's eyes together -- a summary of the field investigation on the implementation of action bright in Guangxi
NFS shared services
Memorize the text and remember the words. Read the text and remember the words. Read the article and remember the words; 40 articles with 3500 words; 71 articles broke through the words in the middle
Network based BGP