Skip to content

Commit 5fdacde

Browse files
author
Bai jialiang
committed
2sum
0 parents  commit 5fdacde

8 files changed

+196
-0
lines changed

.ycm_extra_conf.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Generated by YCM Generator at 2016-12-09 15:35:30.893900
2+
3+
# This file is NOT licensed under the GPLv3, which is the license for the rest
4+
# of YouCompleteMe.
5+
#
6+
# Here's the license text for this file:
7+
#
8+
# This is free and unencumbered software released into the public domain.
9+
#
10+
# Anyone is free to copy, modify, publish, use, compile, sell, or
11+
# distribute this software, either in source code form or as a compiled
12+
# binary, for any purpose, commercial or non-commercial, and by any
13+
# means.
14+
#
15+
# In jurisdictions that recognize copyright laws, the author or authors
16+
# of this software dedicate any and all copyright interest in the
17+
# software to the public domain. We make this dedication for the benefit
18+
# of the public at large and to the detriment of our heirs and
19+
# successors. We intend this dedication to be an overt act of
20+
# relinquishment in perpetuity of all present and future rights to this
21+
# software under copyright law.
22+
#
23+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26+
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29+
# OTHER DEALINGS IN THE SOFTWARE.
30+
#
31+
# For more information, please refer to <http://unlicense.org/>
32+
33+
import os
34+
import ycm_core
35+
36+
flags = [
37+
'-x',
38+
'c++',
39+
'-DBY_THREAD',
40+
'-DCPPTEST',
41+
'-Wall',
42+
'-std=c++14',
43+
]
44+
45+
46+
# Set this to the absolute path to the folder (NOT the file!) containing the
47+
# compile_commands.json file to use that instead of 'flags'. See here for
48+
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
49+
#
50+
# You can get CMake to generate this file for you by adding:
51+
# set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
52+
# to your CMakeLists.txt file.
53+
#
54+
# Most projects will NOT need to set this to anything; you can just change the
55+
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
56+
compilation_database_folder = ''
57+
58+
if os.path.exists( compilation_database_folder ):
59+
database = ycm_core.CompilationDatabase( compilation_database_folder )
60+
else:
61+
database = None
62+
63+
SOURCE_EXTENSIONS = [ '.C', '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
64+
65+
def DirectoryOfThisScript():
66+
return os.path.dirname( os.path.abspath( __file__ ) )
67+
68+
69+
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
70+
if not working_directory:
71+
return list( flags )
72+
new_flags = []
73+
make_next_absolute = False
74+
path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
75+
for flag in flags:
76+
new_flag = flag
77+
78+
if make_next_absolute:
79+
make_next_absolute = False
80+
if not flag.startswith( '/' ):
81+
new_flag = os.path.join( working_directory, flag )
82+
83+
for path_flag in path_flags:
84+
if flag == path_flag:
85+
make_next_absolute = True
86+
break
87+
88+
if flag.startswith( path_flag ):
89+
path = flag[ len( path_flag ): ]
90+
new_flag = path_flag + os.path.join( working_directory, path )
91+
break
92+
93+
if new_flag:
94+
new_flags.append( new_flag )
95+
return new_flags
96+
97+
98+
def IsHeaderFile( filename ):
99+
extension = os.path.splitext( filename )[ 1 ]
100+
return extension in [ '.H', '.h', '.hxx', '.hpp', '.hh' ]
101+
102+
103+
def GetCompilationInfoForFile( filename ):
104+
# The compilation_commands.json file generated by CMake does not have entries
105+
# for header files. So we do our best by asking the db for flags for a
106+
# corresponding source file, if any. If one exists, the flags for that file
107+
# should be good enough.
108+
if IsHeaderFile( filename ):
109+
basename = os.path.splitext( filename )[ 0 ]
110+
for extension in SOURCE_EXTENSIONS:
111+
replacement_file = basename + extension
112+
if os.path.exists( replacement_file ):
113+
compilation_info = database.GetCompilationInfoForFile(
114+
replacement_file )
115+
if compilation_info.compiler_flags_:
116+
return compilation_info
117+
return None
118+
return database.GetCompilationInfoForFile( filename )
119+
120+
121+
def FlagsForFile( filename, **kwargs ):
122+
if database:
123+
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
124+
# python list, but a "list-like" StringVec object
125+
compilation_info = GetCompilationInfoForFile( filename )
126+
if not compilation_info:
127+
return None
128+
129+
final_flags = MakeRelativePathsInFlagsAbsolute(
130+
compilation_info.compiler_flags_,
131+
compilation_info.compiler_working_dir_ )
132+
133+
else:
134+
relative_to = DirectoryOfThisScript()
135+
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
136+
137+
return {
138+
'flags': final_flags,
139+
'do_cache': True
140+
}
141+

.ycm_extra_conf.pyc

2.61 KB
Binary file not shown.

2sum/2sum.cc

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_map>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
vector<int> twoSum(vector<int>& nums, int target)
9+
{
10+
unordered_map<int,int> record;
11+
vector<int> result;
12+
for( int i = 0; i < nums.size(); i++)
13+
{
14+
int numberToFind = target - nums[i];
15+
if(record.find(numberToFind) != record.end())
16+
{
17+
result.push_back(record[numberToFind]);
18+
result.push_back(i);
19+
return result;
20+
}
21+
record[nums[i]]=i;
22+
}
23+
return result;
24+
}
25+
};
26+
27+
int main(int argc, char *argv[])
28+
{
29+
Solution s;
30+
vector<int> test = {2,7,11,15};
31+
int target = 9;
32+
33+
for(int i : s.twoSum(test, target))
34+
cout << i << endl;
35+
36+
return 0;
37+
}

2sum/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_executable(2sum 2sum.cc)

CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
project(helloworld)
2+
cmake_minimum_required(VERSION 2.8)
3+
4+
set(CMAKE_VERBOSE_MAKEFILE on)
5+
set(CMAKE_CXX_FLAGS "-Wall -static-libstdc++ -DCPPTEST -DBY_THREAD -std=c++14")
6+
#cmake -DCMAKE_BUILD_TYPE=debug ../
7+
set(CMAKE_CXX_FLAGS_DEBUG "-g3")
8+
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
9+
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
10+
11+
add_subdirectory(2sum)
12+
13+
14+
15+
16+
17+

GPATH

16 KB
Binary file not shown.

GRTAGS

16 KB
Binary file not shown.

GTAGS

16 KB
Binary file not shown.

0 commit comments

Comments
 (0)