Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/azl397985856/leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Jun 28, 2019
2 parents 176f37f + 7f799b8 commit 90d9424
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Binary file added assets/problems/191.number-of-1-bits.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 50 additions & 1 deletion problems/191.number-of-1-bits.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ In Java, the compiler represents the signed integers using 2's complement notati

## 代码

语言支持:JS, C++

JavaScript Code:

```js
/*
* @lc app=leetcode id=191 lang=javascript
Expand Down Expand Up @@ -132,4 +136,49 @@ var hammingWeight = function(n) {
return count;
};

```
```
C++ code:
```c++
class Solution {
public:
int hammingWeight(uint32_t v) {
auto count = 0;
while (v != 0) {
v &= (v - 1);
++count;
}
return count;
}
};
```
## 扩展
可以使用位操作来达到目的。例如8位的整数21:
![number-of-1-bits](../assets/problems/191.number-of-1-bits.png)
C++ Code:
```c++
const uint32_t ODD_BIT_MASK = 0xAAAAAAAA;
const uint32_t EVEN_BIT_MASK = 0x55555555;
const uint32_t ODD_2BIT_MASK = 0xCCCCCCCC;
const uint32_t EVEN_2BIT_MASK = 0x33333333;
const uint32_t ODD_4BIT_MASK = 0xF0F0F0F0;
const uint32_t EVEN_4BIT_MASK = 0x0F0F0F0F;
const uint32_t ODD_8BIT_MASK = 0xFF00FF00;
const uint32_t EVEN_8BIT_MASK = 0x00FF00FF;
const uint32_t ODD_16BIT_MASK = 0xFFFF0000;
const uint32_t EVEN_16BIT_MASK = 0x0000FFFF;
class Solution {
public:
int hammingWeight(uint32_t v) {
v = (v & EVEN_BIT_MASK) + ((v & ODD_BIT_MASK) >> 1);
v = (v & EVEN_2BIT_MASK) + ((v & ODD_2BIT_MASK) >> 2);
v = (v & EVEN_4BIT_MASK) + ((v & ODD_4BIT_MASK) >> 4);
v = (v & EVEN_8BIT_MASK) + ((v & ODD_8BIT_MASK) >> 8);
return (v & EVEN_16BIT_MASK) + ((v & ODD_16BIT_MASK) >> 16);
}
};
```
52 changes: 52 additions & 0 deletions problems/92.reverse-linked-list-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Output: 1->4->3->2->5->NULL

## 代码

语言支持:JS, C++

JavaScript Code:

```js
/*
* @lc app=leetcode id=92 lang=javascript
Expand Down Expand Up @@ -142,3 +146,51 @@ var reverseBetween = function(head, m, n) {
};

```
C++ Code:
```c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int s, int e) {
if (s == e) return head;
ListNode* prev = nullptr;
auto cur = head;
for (int i = 1; i < s; ++i) {
prev = cur;
cur = cur->next;
}
// 此时各指针指向:
// x -> x -> x -> x -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> x -> x -> x ->
// ^head ^prev ^cur
ListNode* p = nullptr;
auto c = cur;
auto tail = c;
ListNode* n = nullptr;
for (int i = s; i <= e; ++i) {
n = c->next;
c->next = p;
p = c;
c = n;
}
// 此时各指针指向:
// x -> x -> x -> x 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 x -> x -> x ->
// ^head ^prev ^p ^cur ^c
// ^tail
if (prev != nullptr) { // 若指向前一个节点的指针不为空,则说明s在链表中间(不是头节点)
prev->next = p;
cur->next = c;
return head;
} else {
if (tail != nullptr) tail->next = c;
return p;
}
}
};
```

0 comments on commit 90d9424

Please sign in to comment.