Skip to content

Instantly share code, notes, and snippets.

@lsuper
lsuper / Leetcode.md
Last active August 29, 2015 14:13
Coding Questions and Solutions

Two Sum

Problem

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2

@lsuper
lsuper / gist:9382966
Created March 6, 2014 05:06
Single Number II
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
a1 = 0
a2 = 0
a3 = 0
for a in A:
a2 |= a1&a
a1 ^= a
@lsuper
lsuper / q1.py
Created February 22, 2014 04:19 — forked from tomviner/q1.py
# -*- coding: utf-8 -*-
"""
1) A string is a palindrome if it reads the same from left-to-right as it does right-to-left.
e.g “nolemonnomelon”, “racecar” & “neveroddoreven” are all palindromes.
A string is an anagram of another string if it consists of exactly the same characters but in another order.
e.g The string “carrace” is an anagram of “racecar”.
Write a function `def is_anagram_of_palindrome(str):`
such that it returns True if the string str is an anagram of a palindrome, and otherwise returns False.
You may assume that str contains only lowercase characters a-z and is not empty.
e.g Given str = “carrace” the function should return True as it is an anagram of the palindrome “racecar”.
@lsuper
lsuper / gist:5539378
Last active December 17, 2015 02:58
metadata example: {"id":"temp_sensor2","temp":"19.2328680618"} 我先在直接在postToWrapper中,把id覆盖了 post json example: {"timestamp":1368005426708,"id":"test_VirtualSensor","temp":"19"}
public boolean postToWrapper(Map<String, String> metadata){
String serverUrl = "http://cmu-sensor-network.herokuapp.com/";
String path = "sensors_save";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl + path);
try {
JSONObject topicObject = new JSONObject();
for (Map.Entry<String, String> entry : metadata.entrySet()) {
String field = entry.getKey();