$.fn.datebox.defaults.formatter = function(date){ var y = date.getFullYear(); var m = date.getMonth()+1; var d = date.getDate(); return y+'-'+m+'-'+d; }
#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "update t_user set name=%s where id = 1" param = ("lt") cursor.execute(sql,param)
cursor.close() conn.commit() conn.close()
同样的执行完脚本,然后查看修改是否发生效果
修改成功,cool~~
查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "select * from t_user where id = 1" cursor.execute(sql) for rows in cursor.fetchall(): for row in rows: print row
cursor.close() conn.commit() conn.close()
执行脚本,控制台成功输出如下结果:
删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#-*- coding:utf-8 -*- """ author:leetao """ import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor() sql = "delete from t_user where id = 1" cursor.execute(sql)
In English, we have a concept called root, which can be followed by some other words to form another longer word - let’s call this word successor. For example, the root an, followed by other, which can form another word another.
Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.
You need to output the sentence after the replacement.
Example 1:
1 2 3
Input: dict = ["cat", "bat", "rat"] sentence = "the cattle was rattled by the battery" Output: "the cat was rat by the bat"
classSolution(object): defreplaceWords(self, roots, sentence): _trie = lambda: collections.defaultdict(_trie) trie = _trie() END = True for root in roots: cur = trie for letter in root: cur = cur[letter] cur[END] = root
defreplace(word): cur = trie for letter in word: if letter notin cur: break cur = cur[letter] if END in cur: return cur[END] return word
defaddWord(self, word): if word: cur = self.root for c in word: if c notin cur.children: cur.children[c] = TrieNode() cur = cur.children[c] cur.word = True
defgetRoot(self, word): ans = "" cur = self.root for c in word: if c in cur.children: ans += c cur = cur.children[c] if cur.word == True: # found smallest root! return ans else: break return word
trie = Trie() for word in roots: trie.addWord(word)
ans = [] for word in sentence.split(): ans.append(trie.getRoot(word)) return' '.join(ans)
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.
Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
Example 1:
1 2
Input: nums = [1,2,2,4] Output: [2,3]
Note:
The given array size will in the range [2, 10000].
The given array’s numbers won’t have any order.
Solution
初始化一个长度为 len(nums) + 1的列表 A,然后遍历列表 nums,统计该列表各个元素出现的个数并记录在 A 中,然后统计列表 A 元素值为 2 的索引和 0 索引即为我们所求的值
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution(object): deffindErrorNums(self, nums): """ :type nums: List[int] :rtype: List[int] """ len_num = len(nums) count = [0] * (len(nums)+1) for num in nums: count[num] += 1 for i inrange(1,len(nums)+1): if count[i] == 2: a = i if count[i] == 0: b = i return [a,b]
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example, Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.
Note: You may assume both s and t have the same length.
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
classSolution(object): defcountPrimes(self, n): """ :type n: int :rtype: int """ if n < 3: return0 primes = [True] * n for i inrange(2, int(n**0.5)+1): j = i k = j while j*k < n: primes[j * k ] = False k += 1 count_primes = 0 // [1] for i inrange(2, n): if primes[i]: count_primes += 1 return count_primes
从 [1] 到最后的代码可以改为:
1
returnsum(primes) - 2 // 0,1需要去掉
有个感觉很不错的写法:
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: # @param {integer} n # @return {integer} defcountPrimes(self, n): if n < 3: return0 primes = [True] * n primes[0] = primes[1] = False for i inrange(2, int(n ** 0.5) + 1): if primes[i]: primes[i * i: n: i] = [False] * len(primes[i * i: n: i]) returnsum(primes)
classSolution(object): defintersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ iflen(nums2) > len(nums1): nums2, nums1 = nums1, nums2 nmap = {} for i in nums2: if i notin nmap: nmap[i] = 1 else: nmap[i] += 1 ans = [] for i in nums1: if i in nmap and nmap[i] > 0: nmap[i] -= 1 ans.append(i) return ans
如果不用字典的话,遍历其中一个列表 A 的各个元素,如果这个元素存在于另外一个列表 B 中,将其保存到结果集后移除该元素在列表 B 中第一次出现的位置,最后输出结果集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution(object): defintersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ res = [] for num in nums2: if num in nums1: res.append(num) index = nums1.index(num) del nums1[index] return res