File tree Expand file tree Collapse file tree
83. Remove Duplicates from Sorted List Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * 1、int型长度是32位,创建32位长度数组存储元素中1出现的次数
4+ * 2、数字出现K次,则取模k,最后将结果相加则为只出现一次的值
5+ *
6+ */
7+ public class Solution {
8+ public int singleNumber (int [] nums ) {
9+ int result = 0 ;
10+ int [] bit = new int [32 ];
11+ for (int i = 0 ; i < 32 ; i ++){
12+ for (int j = 0 ; j < nums .length ; j ++){ //求出每个数第i位上的1的次数之和
13+ bit [i ] += nums [j ] >> i & 0x01 ;
14+ }
15+ result += (bit [i ] % 3 ) << i ; //求出第i位上的余数
16+ }
17+
18+ return result ;
19+ }
20+ }
21+
22+ /**
23+ * Ref
24+ 其实只要理解什么是XOR就好。
25+ A XOR B = (A+B)%进制。
26+ 如果是10进制,7 XOR 8 = (7+8)%10 = 5
27+ 一种思想就是把二进制XOR改成三进制XOR就好
28+
29+ 所谓single number,其实就是bit统计
30+ 你可以按照 bit by bit 统计
31+ single number II,你可以把bit加到一起
32+ 所有number第一bit的sum
33+ 所有number第二bit的sum...
34+ 然后sum%3 (bit by bit) 就是结果...
35+ */
Original file line number Diff line number Diff line change 1+ /**
2+ *
3+ * 1、如果有重复的,则当前节点指向next的next节点,如果没有,则指向next
4+ *
5+ */
6+ public class DeleteDuplicates {
7+ public ListNode deleteDuplicates (ListNode head ) {
8+ ListNode current = head ;
9+ while (current != null && current .next != null ){
10+ if (current .next .val == current .val ){
11+ current .next = current .next .next ;
12+ }else {
13+ current = current .next ;
14+ }
15+ }
16+ return head ;
17+ }
18+ }
You can’t perform that action at this time.
0 commit comments