Skip to content

Instantly share code, notes, and snippets.

@freelze
freelze / 561_Array Partition.cpp
Last active September 21, 2017 09:27
Runtime: 109 ms , Your runtime beats 4.36 % of cpp submissions.
#include <algorithm>
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
vector<int>::size_type sz = nums.size();
sort(nums.begin(),nums.end());
int total = 0;
for(int i=0;i<sz;i+=2)
{
total+=nums[i];
@freelze
freelze / 461_Hamming Distance.cpp
Last active September 21, 2017 09:26
Runtime: 6 ms , Your runtime beats 2.52 % of cpp submissions.
class Solution {
public:
int hammingDistance(int x, int y) {
const int MAX = 10000;
bool two_x[MAX] = {0}, two_y[MAX] = {0};
int i = MAX-1, j = MAX-1;
int point = 0;
while(x>=2)
{
@freelze
freelze / 485_Max Consecutive Ones.cpp
Last active September 21, 2017 09:25
Runtime: 46 ms , Your runtime beats 15.21 % of cpp submissions.
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int findMaxConsecutiveOnes(vector<int>& nums)
{
int count = 0, max = 0;
@freelze
freelze / 495_Teemo Attacking.cpp
Last active September 21, 2017 09:24
Runtime: 59 ms , Your runtime beats 76.14 % of cpp submissions.(2017/9/21)
class Solution {
public:
int findPoisonedDuration(vector<int>& ts, int dur) {
unsigned int temp=0;
for(int i=0;i < (int)ts.size()-1 ; i++){
if( (int)dur-ts[i+1]+ts[i] > 0)
temp+=(int)dur-ts[i+1]+ts[i];
}
return (int)dur*ts.size()-temp;
}
@freelze
freelze / 657_Judge Route Circle.cpp
Created September 27, 2017 17:09
Runtime: 19 ms , Your runtime beats 41.75 % of cpp submissions.
class Solution
{
public:
bool judgeCircle(string moves)
{
int ud = 0 , lr = 0;
for(int i = 0; i < moves.length(); i++)
{
switch(moves[i])
{
@freelze
freelze / 343_Integer Break.cpp
Created September 28, 2017 12:03
Runtime: 3 ms , Your runtime beats 2.01 % of cpp submissions.
class Solution {
public:
int integerBreak(int n) {
if(n == 2 || n == 3) return n-1;
if(n == 4) return 4;
int num = n/3;
int sum = 1;
if(n%3 == 1) sum = 4, --num;
else if(n%3 == 2) sum = 2;
while(num--)
@freelze
freelze / 344_Reverse String.cpp
Created September 28, 2017 12:05
Runtime: 9 ms , Your runtime beats 23.85 % of cpp submissions.
class Solution {
public:
string reverseString(string s) {
string temp; for(string::reverse_iterator rit=s.rbegin();rit!=s.rend();rit++) temp.push_back(*rit);
return temp;
}
};
@freelze
freelze / 650_2 Keys Keyboard.cpp
Last active December 6, 2017 12:51
Runtime: 3 ms , Your runtime beats 61.34 % of cpp submissions.
/*
Question: https://leetcode.com/problems/2-keys-keyboard/description/
*/
class Solution {
public:
int minSteps(int n) {
int n_temp = n;
if(n == 1 ) return 0;
int result = 0;
while(n%2 == 0)
#
# Scrape Yahoo Stock , output to Excel
# 請先新建一個Excel,命名為: stock_price_data.xlsx, 並增加一個sheet, 名稱改為TW2330,TW3711
# 26行請改成自己Excel的路徑
from bs4 import BeautifulSoup
import requests
import time
import os
import openpyxl
@freelze
freelze / Yahoo_WeatherAPI_crawler.py
Last active June 13, 2018 06:47
export to excel
# 請先新建一個Excel,命名為:Temperature.xlsx, 並增加sheets以配合38行 並命名為 "桃園","內壢","中壢","高雄","台東"
# 36行請改成自己Excel的路徑
import requests
import urllib.parse
import time
import openpyxl
import os
def weather_yahooAPI(Fcity,workbook):
res=requests.get("https://query.yahooapis.com/v1/public/yql?q=SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%20IN(%22"+urllib.parse.quote(Fcity)+"%22)%20AND%20country%20%3D%20%22Taiwan%22&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys")
data=res.json()