記錄一些自己覺得需要注意的小點。
目前主要練習c++ c#。
和閨女約了一起做leetcode,慢慢地開始長進。
我沒有閨女,這麼可愛的女孩紙當然是別人家的寶貝女兒啦,閨女是我朋友的外號。
- 1103 分糖果 II
//c++
class Solution {
public:
vector<int> distributeCandies(int candies, int num_people) {
vector <int> ans(num_people,0);
int candiesToPeople = 0;
int peopleNo = 0;
while(candies - candiesToPeople>0){
candies -= ++candiesToPeople;
ans[peopleNo++%num_people] += candiesToPeople;
}
ans[peopleNo%num_people] += candies;
return ans;
}
};
主要是複習一下 i++,++i 的區別與用法。
- 168 Excel表列名稱
//c++
class Solution {
public:
string convertToTitle(int n) {
string ans="";
while(n>0){
if(n%26>0)
{
ans = char(n%26 -1 + 'A') + ans;
n = n / 26;
}
else
{
ans = 'Z' + ans;
n=(n-26)/26;
}
}
return ans;
}
};
一般情況下用的是 string += char把char加到string的末尾,但是這樣本體就需要做一個字符串翻轉,而string類裏面已經重載了+,可以直接用char + string構成新的string字符串。
- 171 Excel表列序號
//c++
class Solution {
public:
int titleToNumber(string s) {
int res = 0;
for(char c : s){
res = res * 26 + (c - 'A' + 1);
}
return res;
}
};
比起168,同樣作爲簡單題的171似乎沒有什麼值得注意的。
- 172 階乘後的零
//c++
class Solution {
public:
int trailingZeroes(int n) {
/*int res = 0;
while(n){
res += n/5;
n /= 5;
}
return res;*/
return n>0?n/5+trailingZeroes(n/5):0;
}
};
在評論中看到了一個用遞歸調用寫的,看起倆非常的精簡,記錄一下。
- 189 旋轉數組
//c++
class Solution {
public:
void rotate(vector<int>& nums, int k) {
k = k % nums.size();
if(k<=0) return;
int mSize = nums.size();
vector<int> newNums(mSize,0);
for(int i=0; i<mSize; i++){
newNums[(i+k)%mSize] = nums[i];
}
nums = newNums;
}
};
這個題自己寫的就很暴力,浪費空間,並不太好,記錄一下非常經典的三次翻轉的做法hhhhhh
n=7 k=3
原始數組 : 1 2 3 4 5 6 7
反轉所有數字後 : 7 6 5 4 3 2 1
反轉前 k 個數字後 : 5 6 7 4 3 2 1
反轉後 n-k 個數字後 : 5 6 7 1 2 3 4
- 121 買賣股票的最佳時機
//c++
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size()<=0) return 0;
int minPrice = prices[0];
int res = 0;
for(int curPrice : prices)
{
if(curPrice < minPrice)
{
minPrice = curPrice;
continue;
}
if(curPrice - minPrice > res){
res = curPrice - minPrice;
}
}
/*for(int i = 0; i<prices.size(); ++i)
{
for(int j = i; j<prices.size(); ++j)
{
if(prices[j]-prices[i]>res)
res = prices[j]-prices[i];
}
}*/
return res;
}
};
- 543 二叉樹的直徑
//c++
class Solution {
public:
int depthOfTree(TreeNode * root){
if(!root) return 0;
return 1+max(depthOfTree(root->left),depthOfTree(root->right));
}
int diameterOfBinaryTree(TreeNode* root) {
if(!root) return 0;
int res = depthOfTree(root->left)+depthOfTree(root->right);
res = max(diameterOfBinaryTree(root->left),res);
res = max(diameterOfBinaryTree(root->right),res);
return res;
}
};
一個暴力的遍歷。
- 204 計數質數
//c++
class Solution {
public:
/*bool isPrimes(int & n) {
if( n == 2 ) return true;
if(n>2 && n%2!=0){
int i = 2;
int cap = sqrt(n)+1;
for(i = 2; i< cap; i++)
{
if( n % i == 0)
break;
}
if(i == cap ) return true;
}
return false;
}
int countPrimes(int n) {
int cnt =0 ;
for(int i=1;i<n;i++){
if(isPrimes(i)){
cnt ++ ;
}
}
return cnt;
}*/
int countPrimes(int n)
{
vector <bool> isPrimes(n+1,true);
int cnt = 0;
for(int i = 2; i < n; i++)
if(isPrimes[i])
{
cnt++;
for(int j = 2 * i; j < n; j += i)
isPrimes[j] = false;
}
return cnt;
}
};
碼一下厄拉多塞篩法。還可以用bitmap來做。
- 1013 將數組分成和相等的三個部分
//c++
class Solution {
public:
bool canThreePartsEqualSum(vector<int>& A) {
int i=0,j=0;
int sum=0;
int sumDividedBy3=0;
int yushu=32766;
int sum1=0;
int sum2=0;
int sum3=0;
for(i=0;i<A.size();++i) {
sum=sum+A[i];
}
yushu=sum%3;
//sum=sum/3;
sumDividedBy3 = sum/3;
if(yushu!=0) return false;
for(i=0;i<A.size()-2;++i) {
sum1=sum1+A[i];
if(sumDividedBy3 == sum1)
break;
}
for(j=i+1;j<A.size()-1;++j) {
sum2=sum2+A[j];
if(sumDividedBy3 == sum2)
break;
}
sum3=sum-sum1-sum2;
if(sum1==sum2 && sum2==sum3)
{ return true;}
return false;
}
};
總覺得這個版本好像不是我寫那個,嘛,不管了,就先馬克一下hhhh
- 202 快樂數
//c++
class Solution {
public:
int squareSum(int n){
int res = 0;
while(n>0){
res += (n%10)*(n%10);
n /= 10;
}
return res;
}
bool isHappy(int n) {
int fast = n;
int slow = n;
do{
slow = squareSum(slow);
if(slow == 1) return true;
fast = squareSum(fast);
fast = squareSum(fast);
}while(slow!=fast);
return false;
}
};
這道題做起來是有技巧的,上面那個類似於雙指針的想法是因爲所有不快樂數的數位平方和計算,最後都會進入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循環中。當然這道題的評論區有些天才用100次循環之類的,反正數字快樂不快樂,寫代碼的人快樂就行。
- 204 計數質數
class Solution {
public:
bool isPrimes(int & n) {
if( n == 2 ) return true;
//if(n>2 && n%2!=0){
if(n>2 ){
int i = 2;
int cap = sqrt(n)+1;
for(i = 2; i< cap; i++)
{
if( n % i == 0)
break;
}
if(i == cap ) return true;
}
return false;
}
int countPrimes(int n) {
int cnt =0 ;
for(int i=1;i<n;i++){
if(isPrimes(i)){
cnt ++ ;
}
}
return cnt;
}
/*int countPrimes(int n)
{
vector <bool> isPrimes(n+1,true);
int cnt = 0;
for(int i = 2; i < n; i++)
if(isPrimes[i])
{
cnt++;
for(int j = 2 * i; j < n; j += i)
isPrimes[j] = false;
}
return cnt;
}*/
};
/*class Solution {
public:
int countPrimes(int n) {
int ans=0;
int count=0;
int i=0,j=0;
if(n <= 2) return 0;
else
{
++ans;
for(i=3;i<n;++i)
{
if(i%2 == 0) continue;
for(j=1;j*j<=i;j=j+2) {
if(i%j == 0) {
++count;
}
if(count>1) break;
}
if(count == 1)
{
++ans;
}
count = 0;
}
return ans;
}
}
};*/
再學習一下=厄拉多塞篩法。
- 144 二叉樹的前序遍歷
//c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
/*void preTraversal(vector <int> & res, TreeNode * root)
{
if(!root) return;
res.push_back(root->val);
preTraversal(res,root->left);
preTraversal(res,root->right);
}
vector<int> preorderTraversal(TreeNode* root) {
vector <int> res;
preTraversal(res,root);
return res;
}*/
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode *>nodes;
vector<int>res;
while(root||!nodes.empty())
{
while(root)
{
nodes.push(root->right);
res.push_back(root->val);
root=root->left;
}
root=nodes.top();
nodes.pop();
}
return res;
}
};
迭代法和遞歸法都寫了一遍。
- 111 二叉樹的最小深度
//c++
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left && root->right) return 1+minDepth(root->right);
if(!root->right && root->left) return 1+minDepth(root->left);
return 1+min(minDepth(root->left),minDepth(root->right));
}
};
//c#
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int MinDepth(TreeNode root) {
if(root == null) return 0;
int ans = 0;
Queue<TreeNode> curLevelNode = new Queue<TreeNode>();
curLevelNode.Enqueue(root);
while(curLevelNode.Count>0){
ans += 1;
int curLevelNodeCount = curLevelNode.Count;
for(int i=0; i<curLevelNodeCount; i++){
TreeNode mNode = curLevelNode.Dequeue();
if(mNode.right== null && mNode.left == null) return ans;
if(mNode.left!=null) curLevelNode.Enqueue(mNode.left);
if(mNode.right!=null) curLevelNode.Enqueue(mNode.right);
}
}
return ans;
}
}
- 101 對稱二叉樹
//c# 迭代遞歸都寫了一下
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
/*public bool AreTwoTreeSymmetric(TreeNode root0, TreeNode root1){
if(root0 == null && root1 == null) return true;
if(root0 == null || root1 == null) return false;
if(root0.val == root1.val){
return AreTwoTreeSymmetric(root0.right, root1.left) && AreTwoTreeSymmetric(root0.left, root1.right);
}
return false;
}
public bool IsSymmetric(TreeNode root) {
return AreTwoTreeSymmetric(root, root);
}*/
public bool IsSymmetric(TreeNode root){
if(root == null) return true;
Queue <TreeNode> NodeQueue = new Queue <TreeNode>();
Queue <TreeNode> SymmetricNodeQueue = new Queue <TreeNode>();
NodeQueue.Enqueue(root);
SymmetricNodeQueue.Enqueue(root);
while(NodeQueue.Count>0){
TreeNode node0 = NodeQueue.Dequeue();
TreeNode node1 = SymmetricNodeQueue.Dequeue();
if(node0 == null && node1 == null) continue;
if(node0 == null || node1 == null) return false;
if(node0.val != node1.val) return false;
NodeQueue.Enqueue(node0.right);
NodeQueue.Enqueue(node0.left);
SymmetricNodeQueue.Enqueue(node1.left);
SymmetricNodeQueue.Enqueue(node1.right);
}
return true;
}
}
- 217 存在重複元素
//c++
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_map <int, bool> TraversedNums;
for(int n : nums)
{
if(TraversedNums.find(n)!=TraversedNums.end()) return true;
TraversedNums[n]=true;
}
return false;
}
};
//c#
public class Solution {
public bool ContainsDuplicate(int[] nums) {
/*Hashtable TraversedNum = new Hashtable();
foreach(int n in nums){
if(TraversedNum.ContainsKey(n)) return true;
TraversedNum.Add(n,true);
}
return false;*/
HashSet <int> TraversedNum = new HashSet <int>();
foreach(int n in nums){
if(!TraversedNum.Add(n)) return true;
}
return false;
}
}
代碼倒是看起來非常短,主要是學習一下c#的hashtable和hashset。
- 219 存在重複元素II
public class Solution {
public bool ContainsNearbyDuplicate(int[] nums, int k) {
Hashtable TraversedNum = new Hashtable();
for(int i=0; i<nums.Length; i++){
if(TraversedNum.ContainsKey(nums[i]) ){
int backIndex = (int)TraversedNum[nums[i]];
if(i - backIndex <= k)
return true;
TraversedNum.Remove(nums[i]);
}
TraversedNum.Add(nums[i],i);
}
return false;
}
}
- 409 最長迴文串
//c#
public class Solution {
public int LongestPalindrome(string s) {
int [] letterCount = new int[52];
foreach(char c in s){
if(c>='A'&&c<='Z')
letterCount[c-'A']++;
if(c>='a'&&c<='z')
letterCount[c-'a'+26]++;
}
int res = 0;
bool isOdd = false;
foreach(int n in letterCount){
if(n%2==0) res+=n;
else{
isOdd = true;
res += n-1;
}
}
if(isOdd) res += 1;
return res;
}
}
- 40 最小的k個數
//c#
public class Solution {
public void HeapAjust(int[] arr, int parent){
int leftChild = parent*2+1;
if(leftChild >= arr.Length) return;
int maxChild = leftChild;
int rightChild = leftChild+1;
if(rightChild < arr.Length)
maxChild = arr[leftChild] > arr[rightChild] ? leftChild : rightChild;
if(arr[maxChild] > arr[parent]){
int tmp = arr[maxChild];
arr[maxChild] = arr[parent];
arr[parent] = tmp;
HeapAjust(arr, maxChild);
}
}
public void BuildMaxHeap(int[]arr){
for (int i = arr.Length /2-1; i >=0; i--)
HeapAjust(arr, i);
}
public int[] GetLeastNumbers(int[] arr, int k) {
if(arr.Length <= k) return arr;
int [] res = new int[k];
if(k<=0) return res;
Array.Copy(arr,0, res,0, k);
BuildMaxHeap(res);
Array.Reverse(res);
for(int i=k; i<arr.Length; i++){
if(arr[i] < res[k-1]){
int tmp = arr[i];
arr[i] = res[k-1];
res[k-1] = tmp;
BuildMaxHeap(res);
Array.Reverse(res);
}
}
return res;
}
}
藉着排序的題目想練習一下用c#寫堆,寫了一個基於堆的獲取最大k個數的方法。但是實際上這個題在劍指offer上有更普遍的解法,而且,優先隊列就是堆了,就滿足需求了,不需要自己寫那麼多orz
- 28 實現strStr()
//c++
class Solution {
public:
vector<int> findnext(string str)
{
vector<int>next(1,-1);
int nextIndex;
for (int i = 1; i < str.length();i++){
nextIndex = next[i - 1];
while (nextIndex>-1)
{
if (str[nextIndex] == str[i - 1])
{
next.push_back(nextIndex + 1);
break;
}
else nextIndex = next[nextIndex];
}
if (nextIndex==-1) next.push_back(0);
}
return next;
}
int strStr(string haystack, string needle) {
int needleLength = needle.size();
if(needleLength<=0) return 0;
vector <int> next = findnext(needle);
int needleIndex = 0;
int haystackLength = haystack.size();
for(int i=0;i<haystackLength;i++){
while(needleIndex>0 && haystack[i]!=needle[needleIndex])
{
needleIndex = next[needleIndex];
}
if(haystack[i] == needle[needleIndex]){
needleIndex ++;
}
if(needleIndex >= needleLength)
{
return i-needleLength+1;
}
}
return -1;
}
};
題目本身不難,主要是想自己練習一下KMP算法。再多注意一下next數組的各種算法。
- 220 存在重複元素 III
//c++
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
multiset <long long> WindowSet;
for( long long i = 0; i<nums.size(); ++i){
auto minNum = WindowSet.lower_bound((long long )nums[i]-(long long )t);
if(minNum != WindowSet.end() && *minNum <=(long long ) nums[i]+(long long )t) return true;
WindowSet.insert(nums[i]);
if(WindowSet.size()>k){
WindowSet.erase(nums[i-k]);
}
}
return false;
}
};
//c#
public class Solution {
public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
Hashtable TraversedNum = new Hashtable();
for(int i=0; i<nums.Length; i++){
int maxNum = nums[i] + t;
int minNum = nums[i] - t;
for(int j = minNum; j<= maxNum;j++)
{
if(TraversedNum.ContainsKey(j))
{
List <int> backIndex = (List <int>)TraversedNum[j];
foreach(int n in backIndex)
{
if(i - n <= k)
return true;
}
}
}
List <int> curNumIndexs = new List <int>();
curNumIndexs = TraversedNum[nums[i]];
curNumIndexs.Add(i);
TraversedNum.Add(nums[i],curNumIndexs);
}
return false;
}
}
滑動窗口的思想,滑動窗口內維持一個性能較好的排序。
- 面試題 17.16 按摩師
//c++
class Solution {
public:
int massage(vector<int>& nums) {
int ans = 0;
int appiontmentNum = nums.size();
vector<int> dp(appiontmentNum+2);
dp[0] = 0;
dp[1] = 0;
for(int i = 2 ; i <appiontmentNum+2; i++ ){
int curMax = dp[i-1];
for(int j = i-2; j >= 0 ; j--){
curMax = dp[j] + nums[i-2] >= curMax ? dp[j] + nums[i-2]: curMax;
}
dp[i] = curMax;
}
return dp[appiontmentNum+1];
}
};
//c#
public class Solution {
public int Massage(int[] nums) {
int [] dp = new int [nums.Length+2];
dp[0] = 0;
dp[1] = 0;
for(int i =2 ; i<nums.Length+2; i++){
int curMax = dp[i-1];
for(int j = i-2; j>=0 ; j--){
curMax = dp[j] + nums[i-2] > curMax ? dp[j] + nums[i-2] : curMax;
}
dp[i] = curMax;
}
return dp[nums.Length+1];
}
}
比較簡單的動態規劃,不過我寫的本質上和大家寫的還是不一樣的。我寫的O(n^2),答案的思路是O(n),還是可以修改的。不過這個題AC也不影響。
- 213 打家劫舍 II
//c++
class Solution {
public:
int dp(vector<int>& nums) {
int ans = 0;
int appiontmentNum = nums.size();
vector<int> dp(appiontmentNum+2);
dp[0] = 0;
dp[1] = 0;
for(int i = 2 ; i <appiontmentNum+2; i++ ){
int curMax = dp[i-1];
for(int j = i-2; j >= 0 ; j--){
curMax = dp[j] + nums[i-2] >= curMax ? dp[j] + nums[i-2]: curMax;
}
dp[i] = curMax;
}
return dp[appiontmentNum+1];
}
int rob(vector<int>& nums) {
int res = 0;
if(nums.size()==1) res = nums[0];
if(nums.size()>1)
{
vector<int> subArray1, subArray2;
subArray1.assign(nums.begin(),nums.end()-1);
subArray2.assign(nums.begin()+1,nums.end());
res = max(dp(subArray1), dp(subArray2));
}
return res;
}
};
//c#
public class Solution {
public int dp(int[] nums) {
int [] dp = new int [nums.Length+2];
dp[0] = 0;
dp[1] = 0;
for(int i =2 ; i<nums.Length+2; i++){
int curMax = dp[i-1];
for(int j = i-2; j>=0 ; j--){
curMax = dp[j] + nums[i-2] > curMax ? dp[j] + nums[i-2] : curMax;
}
dp[i] = curMax;
}
return dp[nums.Length+1];
}
public int Rob(int[] nums) {
int res = 0;
if(nums.Length==1) res = nums[0];
if(nums.Length > 1){
int [] sub1 = new int [nums.Length-1];
Array.Copy(nums, 0, sub1, 0 , nums.Length-1);
int [] sub2 = new int [nums.Length-1];
Array.Copy(nums, 1, sub2, 0 , nums.Length-1);
res = Math.Max(dp(sub1), dp(sub2));
}
return res;
}
}
偷懶把昨天的函數拿來調用了。
- 999 車的可用捕獲量
//c++
class Solution {
public:
int numRookCaptures(vector<vector<char>>& board) {
int boardSize = board.size();
int rookX = 0, rookY = 0;
for(int i = 0; i< boardSize; ++i)
{
for(int j = 0 ; j<boardSize; ++j)
{
if(board[i][j] == 'R')
{
rookX = i;
rookY = j;
break;
}
}
if(rookX!=0) break;
}
int res = 0;
for(int i= rookX+1 ; i < boardSize; i++)
{
if(board[i][rookY]=='B') break;
if(board[i][rookY]=='p')
{
res += 1;
break;
}
}
for(int i= rookY+1 ; i < boardSize; i++)
{
if(board[rookX][i]=='B') break;
if(board[rookX][i]=='p')
{
res += 1;
break;
}
}
for(int i= rookX-1 ; i >=0 ; i--)
{
if(board[i][rookY]=='B') break;
if(board[i][rookY]=='p')
{
res += 1;
break;
}
}
for(int i= rookY-1 ; i >=0 ; i--)
{
if(board[rookX][i]=='B') break;
if(board[rookX][i]=='p')
{
res += 1;
break;
}
}
return res;
}
};
//c#
public class Solution {
public int NumRookCaptures(char[][] board) {
int rx = 0, ry = 0;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(board[i][j]=='R')
{
rx = i;
ry = j;
break;
}
}
if(rx!=0) break;
}
int []dx = new int [4] {0,0,1,-1};
int []dy = new int [4] {1,-1,0,0};
int res = 0;
for(int i=0;i<4;i++)
{
int curX = rx;
int curY = ry;
while(curX>=0&&curX<8&&curY>=0&&curY<8)
{
if(board[curX][curY]=='B') break;
if(board[curX][curY]=='p'){
res += 1;
break;
}
curX += dx[i];
curY += dy[i];
}
}
return res;
}
}
四個for循環寫着確實是蠢了,在c#中練習了用數組的寫法。
- 914 卡牌分組
//c#
public class Solution {
public int Gcd(int a,int b)
{
if (0 == b) return a;
else return Gcd(b, a % b);
}
public int GcdN(int[] digits,int length)
{
if (1 == length) return digits[0];
else return Gcd(digits[length - 1], GcdN(digits, length - 1));
}
public bool HasGroupsSizeX(int[] deck) {
Dictionary<int, int> TraversedNum = new Dictionary<int, int>();
foreach(int n in deck)
{
if (!TraversedNum.ContainsKey(n))
{
TraversedNum.Add(n, 1);
}
else
{
TraversedNum[n] ++;
}
}
if(TraversedNum.Count > 0){
int[] NumCount = TraversedNum.Values.ToArray();
int x = GcdN(NumCount,NumCount.Length);
if(x<=1) return false;
}
return true;
}
}
嗐,開始的時候沒有意識到是GCD。
- 223 矩形面積
//c#
public class Solution {
public int ComputeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int x1 = Math.Max(A,E);
int x2 = Math.Min(C,G);
int y1 = Math.Max(B,F);
int y2 = Math.Min(D,H);
int rec1 = (D-B)*(C-A);
int rec2 = (H-F)*(G-E);
if(x1<x2&&y1<y2) return rec1-Math.Abs((x1-x2)*(y1-y2))+rec2;
return rec1+rec2;
}
}
- 面試題62 圓圈中最後剩下的數字
//c#
public class Solution {
public int DeleteMforN(int n,int m){
if (n==1)
return 0;
int index = DeleteMforN(n-1,m);
return (index+m)%n;
}
public int LastRemaining(int n, int m) {
return DeleteMforN(n,m);
}
/*public int LastRemaining(int n, int m) {
List<int> nums = new List<int>();
for(int i = 0; i<n;i++)
{
nums.Add(i);
}
int curIndex = 0;
while(n>1)
{
curIndex = (m + curIndex - 1) % n;
nums.RemoveAt(curIndex);
n--;
}
return nums[0];
}
}*/
劍指offer的題不過思路已經忘了,開始的時候模擬操作用的動態數組ArrayList運行超時,換成List就可以通過了。List比ArrayList性能要更好,以後多用List。
- 234 迴文鏈表
//c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (head == nullptr ) return true;
ListNode * slow = head;
ListNode * fast = head;
while(fast!= nullptr && fast->next!=nullptr){
slow = slow->next;
fast = fast->next->next;
}
ListNode * cur = slow;
ListNode * pre = nullptr;
while(cur!=nullptr){
ListNode * curNext = cur->next;
cur->next = pre;
pre = cur;
cur = curNext;
}
cur = pre;
while(head && cur )
{
if(cur->val != head->val) return false;
cur = cur->next;
head = head->next;
}
return true;
}
};
練習了快慢指針+反轉鏈表