Commit 921cc799 authored by MisterBigbooo's avatar MisterBigbooo
Browse files

添加仓库代码

parent 4e5b4643
cmake_minimum_required(VERSION 3.5)
project(cpp_0219)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(cpp_0219 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/contains-duplicate-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
/// Using Hash Set
/// Time Complexity: O(n)
/// Space Complexity: O(k)
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
if(nums.size() <= 1)
return false;
if(k <= 0)
return false;
unordered_set<int> record;
for(int i = 0 ; i < nums.size() ; i ++){
if(record.find(nums[i]) != record.end())
return true;
record.insert(nums[i]);
// 保持record中最多有k个元素
// 因为在下一次循环中会添加一个新元素,使得总共考虑k+1个元素
if(record.size() == k + 1)
record.erase(nums[i - k]);
}
return false;
}
};
void printBool(bool b){
cout << (b ? "True" : "False") << endl;
}
int main() {
int nums[] = {1, 2, 1};
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
int k = 1;
printBool(Solution().containsNearbyDuplicate(vec, k));
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/contains-duplicate-ii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import java.util.HashSet;
/// Using Hash Set
/// Time Complexity: O(n)
/// Space Complexity: O(k)
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums == null || nums.length <= 1)
return false;
if(k <= 0)
return false;
HashSet<Integer> record = new HashSet<Integer>();
for(int i = 0 ; i < nums.length; i ++){
if(record.contains(nums[i]))
return true;
record.add(nums[i]);
if(record.size() == k + 1)
record.remove(nums[i-k]);
}
return false;
}
private static void printBool(boolean b){
System.out.println(b ? "True" : "False");
}
public static void main(String[] args) {
int[] nums = {1, 2, 1};
int k = 1;
printBool((new Solution()).containsNearbyDuplicate(nums, k));
}
}
cmake_minimum_required(VERSION 3.5)
project(cpp_0237)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(cpp_0237 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include <iostream>
#include <cassert>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// LinkedList Test Helper Functions
ListNode* createLinkedList(int arr[], int n){
if(n == 0)
return NULL;
ListNode* head = new ListNode(arr[0]);
ListNode* curNode = head;
for(int i = 1 ; i < n ; i ++){
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}
return head;
}
void printLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode != NULL){
cout << curNode->val << " -> ";
curNode = curNode->next;
}
cout << "NULL" << endl;
return;
}
void deleteLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode != NULL){
ListNode* delNode = curNode;
curNode = curNode->next;
delete delNode;
}
return;
}
ListNode* findNode(ListNode* head, int x){
ListNode* curNode = head;
while(curNode != NULL){
if(curNode->val == x)
return curNode;
curNode = curNode->next;
}
return NULL;
}
// 时间复杂度: O(1)
// 空间复杂度: O(1)
class Solution {
public:
void deleteNode(ListNode* node) {
assert(node != NULL && node->next != NULL);
node->val = node->next->val;
ListNode* delNode = node->next;
node->next = delNode->next;
delete delNode;
return;
}
};
int main() {
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr)/sizeof(int);
ListNode* head = createLinkedList(arr, n);
printLinkedList(head);
ListNode* node2 = findNode(head, 2);
Solution().deleteNode(node2);
printLinkedList(head);
deleteLinkedList(head);
return 0;
}
\ No newline at end of file
public class ListNode {
public int val;
public ListNode next = null;
public ListNode(int x) {
val = x;
}
public ListNode (int[] arr){
if(arr == null || arr.length == 0)
throw new IllegalArgumentException("arr can not be empty");
this.val = arr[0];
ListNode curNode = this;
for(int i = 1 ; i < arr.length ; i ++){
curNode.next = new ListNode(arr[i]);
curNode = curNode.next;
}
}
ListNode findNode(int x){
ListNode curNode = this;
while(curNode != null){
if(curNode.val == x)
return curNode;
curNode = curNode.next;
}
return null;
}
@Override
public String toString(){
StringBuilder s = new StringBuilder("");
ListNode curNode = this;
while(curNode != null){
s.append(Integer.toString(curNode.val));
s.append(" -> ");
curNode = curNode.next;
}
s.append("NULL");
return s.toString();
}
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/delete-node-in-a-linked-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
///
/// 时间复杂度: O(1)
/// 空间复杂度: O(1)
public class Solution {
public void deleteNode(ListNode node) {
if(node == null || node.next == null)
throw new IllegalArgumentException("node should be valid and can not be the tail node.");
node.val = node.next.val;
node.next = node.next.next;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
ListNode head = new ListNode(arr);
System.out.println(head);
ListNode node2 = head.findNode(2);
(new Solution()).deleteNode(node2);
System.out.println(head);
}
}
cmake_minimum_required(VERSION 3.5)
project(cpp_0283)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(cpp_0283 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
#include <iostream>
#include <vector>
using namespace std;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
vector<int> nonZeroElements;
// put all the non zero elements into a new vector
for(int i = 0 ; i < nums.size() ; i ++)
if(nums[i])
nonZeroElements.push_back(nums[i]);
// make nums[0...nonZeroElements.size()) all non zero elements
for(int i = 0 ; i < nonZeroElements.size() ; i ++)
nums[i] = nonZeroElements[i];
// make nums[nonZeroElements.size()...nums.size()) all zero elements
for(int i = nonZeroElements.size() ; i < nums.size() ; i ++)
nums[i] = 0;
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
int arr[] = {0, 1, 0, 3, 12};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
Solution().moveZeroes(vec);
printVec(vec);
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
#include <iostream>
#include <vector>
using namespace std;
// Time Complexity: O(n)
// Space Complexity: O(1)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.size() ; i ++ )
if(nums[i])
nums[k++] = nums[i];
// make the nums[k...end) zeros
for(int i = k ; i < nums.size() ; i ++)
nums[i] = 0;
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
int arr[] = {0, 1, 0, 3, 12};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
Solution().moveZeroes(vec);
printVec(vec);
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
#include <iostream>
#include <vector>
using namespace std;
// Time Complexity: O(n)
// Space Complexity: O(1)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.size() ; i ++)
if(nums[i])
swap(nums[k++] , nums[i]);
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
int arr[] = {0, 1, 0, 3, 12};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
Solution().moveZeroes(vec);
printVec(vec);
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
#include <iostream>
#include <vector>
using namespace std;
// Time Complexity: O(n)
// Space Complexity: O(1)
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.size() ; i ++ )
if(nums[i])
// avoid self swapping
if(k != i)
swap(nums[k++], nums[i]);
else
k ++;
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
int arr[] = {0, 1, 0, 3, 12};
vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
Solution().moveZeroes(vec);
printVec(vec);
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
import java.util.*;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution1 {
public void moveZeroes(int[] nums) {
ArrayList<Integer> nonZeroElements = new ArrayList<Integer>();
// put all the non zero elements into a new vector
for (int i = 0; i < nums.length; i++)
if (nums[i] != 0)
nonZeroElements.add(nums[i]);
// make nums[0...nonZeroElements.size()) all non zero elements
for (int i = 0; i < nonZeroElements.size(); i++)
nums[i] = nonZeroElements.get(i);
// make nums[nonZeroElements.size()...nums.size()) all zero elements
for (int i = nonZeroElements.size(); i < nums.length; i++)
nums[i] = 0;
}
private static void printArr(int[] arr){
for(int i = 0 ; i < arr.length ; i ++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int[] arr = {0, 1, 0, 3, 12};
(new Solution1()).moveZeroes(arr);
printArr(arr);
}
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
import java.util.*;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution2 {
public void moveZeroes(int[] nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.length ; i ++)
if(nums[i] != 0)
nums[k++] = nums[i];
// make the nums[k...end) zeros
for(int i = k ; i < nums.length ; i ++)
nums[i] = 0;
}
private static void printArr(int[] arr){
for(int i = 0 ; i < arr.length ; i ++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int[] arr = {0, 1, 0, 3, 12};
(new Solution2()).moveZeroes(arr);
printArr(arr);
}
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
import java.util.*;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution3 {
public void moveZeroes(int[] nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.length ; i ++)
if(nums[i] != 0)
swap(nums, k++, i);
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
private static void printArr(int[] arr){
for(int i = 0 ; i < arr.length ; i ++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int[] arr = {0, 1, 0, 3, 12};
(new Solution3()).moveZeroes(arr);
printArr(arr);
}
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/move-zeroes/description/
/// Author : liuyubobobo
/// Time : 2017-02-09
import java.util.*;
// Time Complexity: O(n)
// Space Complexity: O(n)
class Solution4 {
public void moveZeroes(int[] nums) {
int k = 0; // keep nums[0...k) are all zero elements
for(int i = 0 ; i < nums.length ; i ++)
if(nums[i] != 0)
if(k != i)
swap(nums, k ++, i);
else
k ++;
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
private static void printArr(int[] arr){
for(int i = 0 ; i < arr.length ; i ++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String args[]){
int[] arr = {0, 1, 0, 3, 12};
(new Solution4()).moveZeroes(arr);
printArr(arr);
}
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project(cpp_0328)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main2.cpp)
add_executable(cpp_0328 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/odd-even-linked-list/description/
/// Author : liuyubobobo
/// Time : 2018-10-01
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Split the Linked List into two and then merge
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head == NULL || head->next == NULL || head->next->next == NULL)
return head;
ListNode* dummyHead1 = new ListNode(-1);
ListNode* dummyHead2 = new ListNode(-1);
ListNode* p1 = dummyHead1;
ListNode* p2 = dummyHead2;
ListNode* p = head;
for(int i = 0; p; i ++)
if(i % 2 == 0){
p1->next = p;
p = p->next;
p1 = p1->next;
p1->next = NULL;
}
else{
p2->next = p;
p = p->next;
p2 = p2->next;
p2->next = NULL;
}
p1->next = dummyHead2->next;
ListNode* ret = dummyHead1->next;
delete dummyHead1;
delete dummyHead2;
return ret;
}
};
int main() {
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/odd-even-linked-list/description/
/// Author : liuyubobobo
/// Time : 2018-10-01
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Split the Linked List into two and then merge
/// Keep one in original Linked List
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head == NULL || head->next == NULL || head->next->next == NULL)
return head;
ListNode* dummyHead2 = new ListNode(-1);
ListNode* p2 = dummyHead2;
ListNode* p = head;
while(p->next){
p2->next = p->next;
if(p->next->next == NULL){
p->next = NULL;
break;
}
p->next = p->next->next;
p = p->next;
p2 = p2->next;
p2->next = NULL;
}
p->next = dummyHead2->next;
delete dummyHead2;
return head;
}
};
int main() {
return 0;
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project(cpp_0344)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(cpp_0344 ${SOURCE_FILES})
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment