Leetcode题解(26)

80. Remove Duplicates from Sorted Array II

题目

分析:简单的操作,代码如下:

 1 class Solution {
 2 public:
 3     int removeDuplicates(vector<int>& nums) {
 4         int n = nums.size();
 5         if(0==n)
 6             return 0;
 7
 8         int i=0;
 9         int temp;
10         int res = n;
11         vector<int> result;
12         int count;
13         for(i=0;i<n;)
14         {
15             temp=nums[i];
16             count=1;
17             i++;
18             while(i<n&&nums[i] == temp)
19             {
20                 count++;
21                 i++;
22             }
23
24             if(count>2)
25             {
26                 res = res-(count-2);
27                 result.push_back(temp);
28                 result.push_back(temp);
29
30             }
31             else
32                 while(count--)
33                 {
34                     result.push_back(temp);
35                 }
36
37         }
38         nums = result;
39         return res;
40
41     }
42 };

---------------------------------------------------------------------------------分割线-----------------------------------------------------------------

81. Search in Rotated Sorted Array II

题目

分析:题目和33题很相识,代码如下:

 1 class Solution {
 2 public:
 3     bool search(vector<int>& nums, int target) {
 4         int n=nums.size();
 5         vector<int> A=nums;
 6          if(0 == n) return false;
 7         int left = 0;
 8         int right = n - 1;
 9         while(left <= right)
10         {
11             int midle = (left + right) >> 1;
12             if(A[midle] == target) return true;
13             if(A[left] == A[midle] && A[midle] == A[right])
14             {
15                ++left;
16                --right;
17             }
18             else if(A[left] <= A[midle])
19             {
20                 if(A[left] <= target && target  < A[midle])
21                 {
22                     right = midle - 1;
23                 }
24                 else
25                 left = midle + 1;
26             }
27             else {
28                 if(A[midle] < target && target <= A[right])
29                     left = midle + 1;
30                 else
31                     right = midle - 1;
32             }
33         }
34         return false;
35     }
36 };

--------------------------------------------------------------------------------分割线-----------------------------------------------------------------

82. Remove Duplicates from Sorted List II

题目

分析:这道题主要是考察指针操作,为了方便,在处理之前,对链表添加一个头节点,以便处理起来更加方便,代码如下

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* deleteDuplicates(ListNode* head) {
12         if(NULL == head)
13             return NULL;
14
15         ListNode *pre,*current;
16         ListNode *pHead = new ListNode(0);
17         pHead->next = head;//添加头节点
18         pre = pHead;
19         current = head;
20         int key;
21         bool flag = false;
22         while(current!= NULL)
23         {
24             key = current->val;
25             current = current->next;
26             while( current != NULL && current->val == key)
27             {
28                 flag = true;
29                 current = current->next;
30             }
31             if(flag)
32             {
33                 pre->next = current;
34                 flag = false;
35             }
36             else
37             pre = pre->next;
38         }
39
40         return pHead->next;
41
42     }
43 };

-------------------------------------------------------------------------分割线-------------------------------------------------------------------------

83. Remove Duplicates from Sorted List

分析:这一题和82题类似,代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution
10 {
11 public:
12     ListNode *deleteDuplicates(ListNode *head)
13     {
14         if(head==NULL || head->next==NULL) return head;
15         ListNode *helper = new ListNode(-100000);
16         ListNode *ret=head;
17         while(ret)
18         {
19             ListNode *next=ret->next;
20             if(ret->val!=helper->val)
21             {
22                 helper->next=ret;
23                 helper=ret;//将helper指新链表的尾结点
24                 helper->next=NULL;//尾指向空,因为后面的结点有可能被删去了,它不知道下一个指向谁
25             }
26             else delete ret;
27             ret=next;
28         }
29         return head;
30     }
31 };
时间: 2024-11-04 16:31:33

Leetcode题解(26)的相关文章

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] co