Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0b3047

Browse files
author
junl
committed
新增部分C算法
1 parent 47be1f6 commit a0b3047

File tree

20 files changed

+558
-213
lines changed

20 files changed

+558
-213
lines changed

‎alg-cpp.xcodeproj/project.pbxproj‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
3A5C8BB022E05D0E00354740 /* MyCircularDeque.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A5C8BAF22E05D0E00354740 /* MyCircularDeque.cpp */; };
2121
3A5C8BBB22E06F6900354740 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A5C8BBA22E06F6900354740 /* main.cpp */; };
2222
3A5C8BD222E1A7AE00354740 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A5C8BD122E1A7AE00354740 /* main.cpp */; };
23+
3A6C5209246C115D00594297 /* Tree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A6C5208246C115D00594297 /* Tree.cpp */; };
2324
3A717D6C22DEBC4E002DA2C2 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A717D6B22DEBC4E002DA2C2 /* main.cpp */; };
2425
3A717D7922DEBC6E002DA2C2 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3A717D7822DEBC6E002DA2C2 /* main.cpp */; };
2526
3A717D8722DEBE7B002DA2C2 /* base.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A717D8622DEBE7B002DA2C2 /* base.h */; };
@@ -362,6 +363,8 @@
362363
3A65DBC822F813F10026166D /* FindNumbersWithSum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FindNumbersWithSum.h; sourceTree = "<group>"; };
363364
3A65DBCB22F819060026166D /* FindContinuousSequence.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FindContinuousSequence.h; sourceTree = "<group>"; };
364365
3A65DBCE22F823AD0026166D /* GetUglyNumber_Solution.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetUglyNumber_Solution.h; sourceTree = "<group>"; };
366+
3A6C5207246C115D00594297 /* Tree.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Tree.h; sourceTree = "<group>"; };
367+
3A6C5208246C115D00594297 /* Tree.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Tree.cpp; sourceTree = "<group>"; };
365368
3A6CF75722E554EC004B8775 /* bSearchTree.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = bSearchTree.h; sourceTree = "<group>"; };
366369
3A6CF75A22E5A626004B8775 /* heap.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = heap.h; sourceTree = "<group>"; };
367370
3A6CF75D22E5BEE0004B8775 /* heapSort.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = heapSort.h; sourceTree = "<group>"; };
@@ -385,6 +388,7 @@
385388
3A717DAD22DF159B002DA2C2 /* hasCycle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = hasCycle.h; sourceTree = "<group>"; };
386389
3A717DB022DF213F002DA2C2 /* detectCycle.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = detectCycle.h; sourceTree = "<group>"; };
387390
3A717DB322DF391C002DA2C2 /* reverseList.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = reverseList.h; sourceTree = "<group>"; };
391+
3AA8ED85241E717100179925 /* Expression.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Expression.h; sourceTree = "<group>"; };
388392
3AAD2E6D22E00ADB006078A4 /* middleNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = middleNode.h; sourceTree = "<group>"; };
389393
3AAD2E6E22E00ADB006078A4 /* removeNthFromEnd.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = removeNthFromEnd.h; sourceTree = "<group>"; };
390394
3AAD2E6F22E00ADB006078A4 /* isPalindrome.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = isPalindrome.h; sourceTree = "<group>"; };
@@ -958,6 +962,7 @@
958962
3A5C8BA422E05ABF00354740 /* leetcode */,
959963
3A5C8B9622E0109200354740 /* main.cpp */,
960964
3A5C8B9B22E010A200354740 /* stack.h */,
965+
3AA8ED85241E717100179925 /* Expression.h */,
961966
3A5C8B9E22E0166900354740 /* sampleBrowser.h */,
962967
3A5C8BA122E045E900354740 /* queue.h */,
963968
3A5C8BA222E045E900354740 /* queue.cpp */,
@@ -1183,6 +1188,8 @@
11831188
3A091A2B22EFE31E00EFA79C /* leetcode */,
11841189
3AE1B00322E3174D00DF4545 /* main.cpp */,
11851190
3AE1B00822E326AE00DF4545 /* btree.h */,
1191+
3A6C5207246C115D00594297 /* Tree.h */,
1192+
3A6C5208246C115D00594297 /* Tree.cpp */,
11861193
3A6CF75722E554EC004B8775 /* bSearchTree.h */,
11871194
3A6CF75A22E5A626004B8775 /* heap.h */,
11881195
);
@@ -1712,6 +1719,7 @@
17121719
buildActionMask = 2147483647;
17131720
files = (
17141721
3AE1B00422E3174D00DF4545 /* main.cpp in Sources */,
1722+
3A6C5209246C115D00594297 /* Tree.cpp in Sources */,
17151723
);
17161724
runOnlyForDeploymentPostprocessing = 0;
17171725
};
72.4 KB
Binary file not shown.
1.75 KB
Binary file not shown.

‎alg-cpp.xcodeproj/xcuserdata/junlongj.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,13 @@
5757
shouldBeEnabled = "Yes"
5858
ignoreCount = "0"
5959
continueAfterRunningActions = "No"
60-
filePath = "array/coding-interviews/PrintMinNumber.h"
61-
timestampString = "599927151.914993"
60+
filePath = "array/coding-interviews/reOrderArray.h"
61+
timestampString = "611072803.998991"
6262
startingColumnNumber = "9223372036854775807"
6363
endingColumnNumber = "9223372036854775807"
64-
startingLineNumber = "78"
65-
endingLineNumber = "78"
66-
landmarkName = "test_PrintMinNumber()"
67-
landmarkType = "9">
68-
</BreakpointContent>
69-
</BreakpointProxy>
70-
<BreakpointProxy
71-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
72-
<BreakpointContent
73-
shouldBeEnabled = "Yes"
74-
ignoreCount = "0"
75-
continueAfterRunningActions = "No"
76-
filePath = "array/coding-interviews/PrintMinNumber.h"
77-
timestampString = "599927157.866888"
78-
startingColumnNumber = "9223372036854775807"
79-
endingColumnNumber = "9223372036854775807"
80-
startingLineNumber = "71"
81-
endingLineNumber = "71"
82-
landmarkName = "PrintMinNumber2(vector&lt;int&gt; numbers)"
64+
startingLineNumber = "76"
65+
endingLineNumber = "76"
66+
landmarkName = "test_reOrderArray()"
8367
landmarkType = "9">
8468
</BreakpointContent>
8569
</BreakpointProxy>

‎alg-cpp/main.cpp‎

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,6 @@
99
#include <iostream>
1010
#include <vector>
1111
#include <string>
12-
#include <string>
13-
#include <stack>
14-
using namespace std;
15-
std::string FilterRepeatString(std::string &s){
16-
if (s.empty()) return s;
17-
s+='%';
18-
std::string result;
19-
int i =0;
20-
int j =0;
21-
while (j<s.length()) {
22-
if (s[i] == s[j]) {
23-
j++;
24-
}else{
25-
int count = j-i;
26-
if (count == 1) {
27-
result+=s[i];
28-
}else{
29-
result+=s[i];
30-
result += std::to_string(count);
31-
}
32-
i=j;
33-
j++;
34-
}
35-
}
36-
return result;
37-
}
38-
class Solution {
39-
public:
40-
static bool Compare(const vector<int> &l, const vector<int> &r){
41-
return l[0] > r[0];
42-
}
43-
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
44-
vector<vector<int>> res;
45-
if (people.empty() || people[0].size() != 2){
46-
return res;
47-
}
48-
sort(people.begin(), people.end(), Compare);
49-
50-
for (int i = 1; i < people.size(); i++){
51-
for(int j=0;j<i;j++){
52-
if (people[i][0] == people[j][0]){
53-
if (people[i][1] < people[j][1]){
54-
int cnt = people[i][1];
55-
vector<int> r = people[i];
56-
people.erase(people.begin() + i);
57-
people.insert(people.begin() + cnt, r);
58-
break;
59-
}
60-
}else{
61-
int cnt = people[i][1];
62-
vector<int> r = people[i];
63-
people.erase(people.begin() + i);
64-
people.insert(people.begin() + cnt, r);
65-
break;
66-
}
67-
}
68-
}
69-
return people;
70-
}
71-
};
7212
int main(int argc, const char * argv[]) {
73-
// insert code here...
74-
std::string s{"aabccccaab"};
75-
std::cout << FilterRepeatString(s) << std::endl;
7613
return 0;
7714
}

‎array/coding-interviews/reOrderArray.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@
1919
*/
2020

2121
namespace codinginterviews {
22+
/**
23+
* Note: The returned array must be malloced, assume caller calls free().
24+
*/
25+
int* exchange(int* nums, int numsSize, int* returnSize){
26+
if (nums == NULL || numsSize <= 0) return NULL;
27+
28+
if (returnSize){
29+
*returnSize = numsSize;
30+
}
31+
int i, j;
32+
i = j = 0;
33+
for(i = 0; i < numsSize; i++){
34+
if ((nums[i] & 0x1) == 0)
35+
break;
36+
}
37+
if (i == numsSize)
38+
return nums;
39+
j = i;
40+
for(; j < numsSize; j++){
41+
if(nums[j] & 0x1){
42+
int tmp = nums[j];
43+
nums[j] = nums[i];
44+
nums[i++] = tmp;
45+
}
46+
}
47+
return nums;
48+
}
2249
void reOrderArray(std::vector<int> &array) {
2350
if (array.empty()) {
2451
return;
@@ -43,6 +70,12 @@ namespace codinginterviews {
4370
std::vector<int> a{0,1,2,3,4,5,6};
4471
reOrderArray(a);
4572
a;
73+
74+
int nums[] = {};
75+
int *nums2 = exchange(nums, 0, NULL);
76+
for (int i=0; i<7; i++) {
77+
printf("%d\t",nums2[i]);
78+
}
4679
}
4780
}
4881

‎hash.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# 哈希算法
23

34
定义: 将任意长度的二进制值串映射为固定长度的二进制串,这个映射的规则就是哈希算法。哈希算法要满足几个要求:

‎linkedList/coding-interviews/ReverseList.h‎

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,45 @@
1717
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
1818
*/
1919
namespace codinginterviews {
20-
ListNode* ReverseList(ListNode* pHead) {
21-
if (!pHead) {
22-
return nullptr;
23-
}
24-
if (!pHead->next) {
25-
return pHead;
20+
#pragma mark - 循环实现
21+
ListNode* ReverseList(ListNode* head) {
22+
if (head == NULL || head->next == NULL) return head;
23+
24+
ListNode *pNode, *cNode, *nNode;
25+
pNode = NULL;
26+
cNode = head;
27+
28+
while(cNode != NULL){
29+
nNode = cNode->next;
30+
cNode->next = pNode;
31+
pNode = cNode;
32+
cNode = nNode;
2633
}
27-
ListNode *pre,*ct,*next;
28-
pre = pHead;
29-
ct = pre->next;
30-
pre->next = nullptr;
31-
while (ct) {
32-
next = ct->next;
33-
ct->next = pre;
34-
pre = ct;
35-
ct = next;
34+
return pNode;
35+
}
36+
#pragma mark - 递归实现
37+
ListNode* ReverseListRecursion(ListNode* head){
38+
if (head == NULL || head->next == NULL) return head;
39+
40+
ListNode *next = head->next;
41+
head->next = NULL;
42+
ListNode *pNode = ReverseListRecursion(next);
43+
ListNode *newHead = pNode;
44+
while(pNode->next != NULL){
45+
pNode = pNode->next;
3646
}
37-
return pre;
47+
pNode->next = head;
48+
return newHead;
3849
}
3950

4051
void test_ReverseList(){
4152
std::cout << "test_ReverseList staring.......... " << std::endl;
4253
ListNode *root = creatLists({1,2,3,4,5})->next;
4354
root = ReverseList(root);
4455
root->print();
56+
root = ReverseListRecursion(root);
57+
root->print();
58+
std::cout << "*******************" << std::endl;
4559
}
4660
}
4761

‎linkedList/leetcode/easy/mergeTwoLists.h‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace leetcode {
2424
链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
2525
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2626
*/
27+
#pragma mark - 迭代实现
2728
ListNode* mergeTwoLists(ListNode *l1, ListNode*l2){
2829
ListNode *head = new ListNode(0);
2930
ListNode *ct = head;
@@ -46,7 +47,7 @@ namespace leetcode {
4647

4748
ListNode *mergeTwoLists01(ListNode* head1, ListNode* head2){
4849
ListNode *p1 = head1, *p2=head2;
49-
staticListNode dummy(0);
50+
ListNode dummy(0);
5051

5152
dummy.next = p1;
5253
ListNode *prev = &dummy;
@@ -68,7 +69,21 @@ namespace leetcode {
6869

6970
return dummy.next;
7071
}
72+
#pragma mark - 递归实现
73+
ListNode* mergeTwoListsRecursion( ListNode* l1, ListNode* l2){
74+
if (l1 == NULL) return l2;
75+
if (l2 == NULL) return l1;
7176

77+
ListNode *pHead = NULL;
78+
if (l1->val < l2->val){
79+
pHead = l1;
80+
pHead->next = mergeTwoLists(l1->next, l2);
81+
}else{
82+
pHead = l2;
83+
pHead->next = mergeTwoLists(l1, l2->next);
84+
}
85+
return pHead;
86+
}
7287

7388
void test_mergeTwoLists(){
7489
singlyLinkedList ll,l2;

‎other/coding-interviews/Power.h‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,39 @@
1818

1919
namespace codinginterviews {
2020
//注意exponent的取值,可正可负
21-
double Power(double base, int exponent) {
22-
if (exponent == 0) {
23-
return 1;
21+
double myPowWithSignedExponent(double x, int n);
22+
int equal(double x, double y);
23+
double myPow(double x, int n){
24+
if (equal(x, 0) && n < 0){
25+
return 0;//输入无效
2426
}
25-
bool neg = false;
26-
if (exponent < 0 ){
27-
exponent = -exponent;
28-
neg = true;
27+
//指数存在正数和负数
28+
signed int absN = n > 0 ? -n : n;
29+
double res = myPowWithSignedExponent(x, absN);
30+
if (n < 0){
31+
res = 1.0 / res;
2932
}
30-
double value = 1;
31-
for (int i=1; i<=exponent; i++) {
32-
value *= base;
33+
return res;
34+
}
35+
int equal(double x, double y){
36+
if (x - y <= 0.0000001 && x - y >= -0.0000001){
37+
return 1;
3338
}
34-
return neg ? 1/value : value;
39+
return 0;
40+
}
41+
double myPowWithSignedExponent(double x, int n){
42+
if (n == 0) return 1;
43+
if (n == -1) return x;
44+
double val = myPowWithSignedExponent(x, n >> 1);
45+
val *= val;
46+
if (n % 2 == -1)
47+
val *= x;
48+
return val;
3549
}
3650

3751
void test_Power(){
3852
std::cout << "Power starting......." <<std::endl;
39-
std::cout << Power(2, 3) << std::endl;
53+
std::cout << myPow(2, 3) << std::endl;
4054
}
4155
}
4256

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /