list = [1,1,3,4,6,3,7] 1. for s in list: if list.count(s) >1: list.remove(s) 2. list2=[] for s in list: if s not in list2: list2.append(s) print list2 3. b = {} b = b.fromkeys(list) print b.keys() 4.set(list)
通常涉及到去重操作最好使用set,但是考虑到某些时候可能遇到不允许使用set的情况,那就自己实现一下: l = [2, 4, 5, 6, 1, 3, 4, 5] def f(l, b=0, c=1): a = len(l) if a > 1: l.sort() if l[b] == l[c]: l.pop(b) a = len(l) # 删除一个元素之后,列表的长度变了,同时,下标b和c对应的值也变了,所以重新对a赋值,而b和c不变 else: b, c = c, c+1 if a > c:
方法1:使用set函数 s=set(list),然后再list(s) 方法2:append def delList(L): L1 = [] for i in L: if i not in L1: L1.append(i) return L1 print(delList([1,2,2,3,3,4,5])) print(delList([1,8,8,3,9,3,3,3,3,3,6,3])) 方法3:count,remove def delList(L): for i in L: if
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Ex
删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个. 您在真实的面试中是否遇到过这个题? Yes 样例 给出 1->1->2->null,返回 1->2->null 给出 1->1->2->3->3->null,返回 1->2->3->null class Solution { public: /* * @param head: head is the head of the linked li
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前置节点来排除第一个节点就是重复元素的特例. 同样是使用快慢针解决问题.不再详细叙述. 代码如下: class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode ret = new ListNode(0); re
1.x的平方根 java (1)直接使用函数 class Solution { public int mySqrt(int x) { int rs = 0; rs = (int)Math.sqrt(x); return rs; } } (2)二分法 对于一个非负数n,它的平方根不会小于大于(n/2+1). 在[0, n/2+1]这个范围内可以进行二分搜索,求出n的平方根. class Solution { public int mySqrt(int x) { long left=1,right=
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有重复出现的数字. LeetCode82. Remove Duplicates from Sorted List II中等 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1->2->5 示例 2: 输入: 1->1->1->