Skip to content

Commit bf01a1c

Browse files
committed
more algorithm and data structor
1 parent f9be12b commit bf01a1c

47 files changed

Lines changed: 893 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.idea/compiler.xml

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BinaryGap/BinaryGap.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

BinaryGap/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>BinaryGap</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
3+
*
4+
* For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
5+
*
6+
* Write a function:
7+
*
8+
* class Solution { public int solution(int N); }
9+
*
10+
* that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
11+
*
12+
* For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
13+
*
14+
* Write an efficient algorithm for the following assumptions:
15+
*
16+
* N is an integer within the range [1..2,147,483,647].
17+
*/
18+
19+
20+
public class BinaryGap {
21+
22+
public static int solution(int N){
23+
String binaryStr = transBinary(N);
24+
System.out.println("Decimal Number:" + N);
25+
System.out.println("Binary String:" + binaryStr + "\n");
26+
27+
char[] chArr = binaryStr.toCharArray();
28+
int i=0;
29+
int flag = 0;
30+
int count = 0;
31+
int maxCount = 0;
32+
while(i<chArr.length){
33+
// System.out.print(chArr[i]);
34+
int value = chArr[i] -48;
35+
if(1 == value && 0 == flag){
36+
flag = 1;
37+
i++;
38+
continue;
39+
}
40+
if(0 == value && 1 == flag ){
41+
count ++;
42+
i++;
43+
continue;
44+
}
45+
if(1 == value && 1 == flag){
46+
if( count > maxCount){
47+
maxCount = count;
48+
}
49+
count = 0;
50+
}
51+
i++;
52+
}
53+
// for(int i=0;i<chArr.length;i++){
54+
// if(1 == chArr[i] ){
55+
//
56+
// }
57+
// }
58+
return maxCount;
59+
}
60+
61+
public static String transBinary(int N){
62+
return Integer.toBinaryString(N);
63+
}
64+
65+
66+
public static void main(String args[]){
67+
System.out.print(solution(2147483647));
68+
/**
69+
* 1
70+
* 5
71+
* 6
72+
* 9
73+
* 16
74+
* 529
75+
* 20
76+
* 15
77+
* 32
78+
* 1041
79+
* 1089
80+
* 1091
81+
* 51712
82+
* 1610612737
83+
* 2147483647
84+
*/
85+
}
86+
87+
88+
}
1.47 KB
Binary file not shown.

BinaryTree/BinaryTree.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

BinaryTree/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>BinaryTree</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>

CAndCPlusPlus/.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "clang - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}/${fileBasenameNoExtension}",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "lldb",
18+
"preLaunchTask": "C/C++: clang build active file"
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)