Commit c1aef7e0 authored by misterbooo's avatar misterbooo
Browse files

Remove Code

parent 5871d9af
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;
}
}
@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/remove-linked-list-elements/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
/// Time Complexity: O(n)
/// Space Complexity: O(1)
public class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode cur = dummyHead;
while(cur.next != null){
if(cur.next.val == val ){
ListNode delNode = cur.next;
cur.next = delNode.next;
}
else
cur = cur.next;
}
return dummyHead.next;
}
public static void main(String[] args) {
int[] arr = {1, 2, 6, 3, 4, 5, 6};
int val = 6;
ListNode head = new ListNode(arr);
System.out.println(head);
(new Solution()).removeElements(head, val);
System.out.println(head);
}
}
public class Solution2 {
public ListNode removeElements(ListNode head, int val) {
if(head == null)
return head;
ListNode node = removeElements(head.next, val);
head.next = node;
return head.val == val ? node : head;
}
public static void main(String[] args) {
int[] arr = {1, 2, 6, 3, 4, 5, 6};
int val = 6;
ListNode head = new ListNode(arr);
System.out.println(head);
(new Solution()).removeElements(head, val);
System.out.println(head);
}
}
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_0279)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main3.cpp)
add_executable(cpp_0279 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <queue>
#include <stdexcept>
using namespace std;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
if(n == 0)
return 0;
queue<pair<int, int>> q;
q.push(make_pair(n, 0));
vector<bool> visited(n + 1, false);
visited[n] = true;
while(!q.empty()){
int num = q.front().first;
int step = q.front().second;
q.pop();
for(int i = 1; num - i * i >= 0; i ++){
int a = num - i * i;
if(!visited[a]){
if(a == 0) return step + 1;
q.push(make_pair(a, step + 1));
visited[a] = true;
}
}
}
throw invalid_argument("No Solution.");
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
vector<int> mem(n + 1, -1);
return numSquares(n, mem);
}
private:
int numSquares(int n, vector<int>& mem){
if(n == 0)
return 0;
if(mem[n] != -1)
return mem[n];
int res = INT_MAX;
for(int i = 1; n - i * i >= 0; i ++ )
res = min(res, 1 + numSquares(n - i * i, mem));
return mem[n] = res;
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int numSquares(int n) {
vector<int> mem(n + 1, INT_MAX);
mem[0] = 0;
for(int i = 1; i <= n ; i ++)
for(int j = 1 ; i - j * j >= 0 ; j ++)
mem[i] = min(mem[i], 1 + mem[i - j * j]);
return mem[n];
}
};
int main() {
cout << Solution().numSquares(12) << endl;
cout << Solution().numSquares(13) << endl;
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.LinkedList;
import javafx.util.Pair;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution1 {
public int numSquares(int n) {
if(n == 0)
return 0;
LinkedList<Pair<Integer, Integer>> queue = new LinkedList<Pair<Integer, Integer>>();
queue.addLast(new Pair<Integer, Integer>(n, 0));
boolean[] visited = new boolean[n+1];
visited[n] = true;
while(!queue.isEmpty()){
Pair<Integer, Integer> front = queue.removeFirst();
int num = front.getKey();
int step = front.getValue();
if(num == 0)
return step;
for(int i = 1 ; num - i*i >= 0 ; i ++){
int a = num - i*i;
if(!visited[a]){
if(a == 0) return step + 1;
queue.addLast(new Pair(num - i * i, step + 1));
visited[num - i * i] = true;
}
}
}
throw new IllegalStateException("No Solution.");
}
public static void main(String[] args) {
System.out.println((new Solution1()).numSquares(12));
System.out.println((new Solution1()).numSquares(13));
}
}
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.Arrays;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution2 {
public int numSquares(int n) {
int[] mem = new int[n+1];
Arrays.fill(mem, -1);
return numSquares(n, mem);
}
private int numSquares(int n, int[] mem){
if(n == 0)
return 0;
if(mem[n] != -1)
return mem[n];
int res = Integer.MAX_VALUE;
for(int i = 1; n - i * i >= 0; i ++ )
res = Math.min(res, 1 + numSquares(n - i * i, mem));
return mem[n] = res;
}
public static void main(String[] args) {
System.out.println((new Solution2()).numSquares(12));
System.out.println((new Solution2()).numSquares(13));
}
}
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.Arrays;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public class Solution3 {
public int numSquares(int n) {
int[] mem = new int[n+1];
Arrays.fill(mem, Integer.MAX_VALUE);
mem[0] = 0;
for(int i = 1; i <= n ; i ++)
for(int j = 1 ; i - j * j >= 0 ; j ++)
mem[i] = Math.min(mem[i], 1 + mem[i - j * j]);
return mem[n];
}
public static void main(String[] args) {
System.out.println((new Solution3()).numSquares(12));
System.out.println((new Solution3()).numSquares(13));
}
}
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
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