C++:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == nullptr || head->next == nullptr){
return false;
}
ListNode* slow = head;
ListNode* fast = head->next;
while(fast!=slow){
if(fast == nullptr || fast->next == nullptr){
return false;
}
slow = slow->next;
fast = fast->next->next;
}
return true;
}
};
GO:
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
if head == nil || head.Next == nil {
return false
}
slow, fast := head, head.Next
for fast != slow {
if fast == nil || fast.Next == nil {
return false
}
slow = slow.Next
fast = fast.Next.Next
}
return true
}
C#:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public bool HasCycle(ListNode head) {
// // 哈希表
HashSet<ListNode> seen = new HashSet<ListNode>();
while (head != null) {
if (!seen.Contains(head)) seen.Add(head);
else return true;
head = head.next;
}
return false;
// // 快慢指针
// if (head == null || head.next == null) return false;
// ListNode slow = head;
// ListNode fast = head.next;
// while (slow != fast) {
// if (fast == null || fast.next == null) return false;
// slow = slow.next;
// fast = fast.next.next;
// }
// return true;
}
}
class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
sort(people.begin(),people.end());
int i = 0;
int j = people.size()-1;
int ans =0;
while(i<=j){
ans++;
if(people[i]+people[j]<=limit){
i++;
}
j--;
}
return ans;
}
};
class Solution {
public int numRescueBoats(int[] people, int limit) {
int ans = 0;
// 从小到大排序,右边是最重的人
Arrays.sort(people);
int left = 0, right = people.length - 1;
while (left <= right) {
// 看右边的重量是否足够
if (people[right] == limit) {
ans++;
right--;
} else if (people[right] + people[left] <= limit) {
// 再看最右边的人与最左边的人总体重量是否超过limit
ans++;
right--;
left++;
} else {
// 还是最右边的人单着
ans++;
right--;
}
}
return ans;
}
}
作者:tong-zhu
链接:https://leetcode-cn.com/problems/boats-to-save-people/solution/tong-ge-lai-shua-ti-la-pai-xu-tan-xin-sh-6paq/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
func numRescueBoats(people []int, limit int) int {
sort.Ints(people)
ans :=0
i, j :=0, 0, len(people) - 1
for i <= j {
if people[i] + people[j] <= limit {
i++
} else{
j--
}
ans++
}
return ans
}
作者:ivan1
链接:https://leetcode-cn.com/problems/boats-to-save-people/solution/tan-xin-suan-fa-jie-jue-jiu-sheng-ting-wen-ti-by-i/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public class Solution {
public int NumRescueBoats(int[] people, int limit) {
int ans = 0;
Array.Sort(people);
int light = 0, heavy = people.Length - 1;
while (light <= heavy) {
if (people[light] + people[heavy] <= limit) {
++light;
}
--heavy;
++ans;
}
return ans;
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/boats-to-save-people/solution/jiu-sheng-ting-by-leetcode-solution-0nsp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
void reverseString(vector<char>& s) {
int n = s.size();
for (int left = 0, right = n - 1; left < right; ++left, --right) {
swap(s[left], s[right]);
}
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-string/solution/fan-zhuan-zi-fu-chuan-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
func reverseString(s []byte) {
for left, right := 0, len(s)-1; left < right; left++ {
s[left], s[right] = s[right], s[left]
right--
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-string/solution/fan-zhuan-zi-fu-chuan-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public class Solution {
public void ReverseString(char[] s) {
//对撞双指针解法
//声明赋值最左和最右的双指针
int left=0,righ=s.Length-1;
//声明一个临时储存值,用于数组元素调换位置
char temp =' ';
//当双指针对撞到一起,就证明已经都遍历过了,停止循环
while(left<righ){
//左右元素互相调换位置
temp=s[left];
s[left]=s[righ];
s[righ]=temp;
left++;
righ--;
}
}
}
作者:tang-pao-fan-m
链接:https://leetcode-cn.com/problems/reverse-string/solution/344-fan-zhuan-zi-fu-chuan-jian-dan-ti-zh-8zmx/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。