[LeetCode&Python] Problem 136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1] Output: 1
Example 2:
Input: [4,1,2,1,2] Output: 4
class Solution: def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() n=len(nums) i=0 while i!=n: if i==n-1 or nums[i]!=nums[i+1]: return nums[i] i+=2
相關推薦
[LeetCode&Python] Problem 136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorith
[LeetCode&Python] Problem 693. Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: In
[LeetCode&Python] Problem 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3
[LeetCode&Python] Problem 806. Number of Lines To Write String
abcde rst leetcode letter leet The nbsp abcd let We are to write the letters of a given string S, from left to right into lines. Each lin
[LeetCode&Python] Problem 771: Jewels and Stones
leetcode diff The letter cas light sid sel nbsp You‘re given strings J representing the types of stones that are jewels, and S representi
[LeetCode&Python] Problem 867. Transpose Matrix
lee solution diagonal for note tput leet 1-1 example Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix
[LeetCode&Python] Problem 557. Reverse Words in a String III
elf ext white turn output pac you small ini Given a string, you need to reverse the order of characters in each word within a sentence wh
[LeetCode&Python] Problem 700. Search in a Binary Search Tree
ould ted ant elif div serial turn therefore roo Given the root node of a binary search tree (BST) and a value. You need to find the node
[LeetCode&Python] Problem 821. Shortest Distance to a Character
lis love array lower python har ger str ret Given a string S and a character C, return an array of integers representing the shortest dis
[LeetCode&Python] Problem 682. Baseball Game
num leet clas sco rem wing perm not and You‘re now a baseball game point recorder. Given a list of strings, each string can be one of the
[LeetCode&Python] Problem 258. Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Example: Input: 38 Output: 2 Explan
[LeetCode&Python] Problem 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example:
[LeetCode&Python] Problem 748. Shortest Completing Word
Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to c
[LeetCode&Python] Problem 389. Find the Difference
Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string
[LeetCode&Python] Problem 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the
[LeetCode&Python] Problem 448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements
[LeetCode&Python] Problem 696. Count Binary Substrings
solution equal sel times ati ive i+1 cau substring Give a string s, count the number of non-empty (contiguous) substrings that have the
[LeetCode&Python] Problem 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9
[LeetCode&Python] Problem 653. Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Examp
[LeetCode&Python] Problem 637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 /