numbers

How do I include negative decimal numbers in this regular expression?

蹲街弑〆低调 提交于 2019-12-27 10:37:48
问题 How do I match negative numbers as well by this regular expression? This regex works fine with positive values, but I want it to also allow negative values e.g. -10, -125.5 etc. ^[0-9]\d*(\.\d+)?$ Thanks 回答1: You should add an optional hyphen at the beginning by adding -? ( ? is a quantifier meaning one or zero occurrences ): ^-?[0-9]\d*(\.\d+)?$ I verified it in Rubular with these values: 10.00 -10.00 and both matched as expected. 回答2: Some Regular expression examples: Positive Integers : ^

leetcode水题(一)

*爱你&永不变心* 提交于 2019-12-26 03:37:46
Two Sum 1 public int[] twoSum(int[] numbers,int target){ Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i=0;i<numbers.length;i++){ int num = numbers[i]; if(map.containsKey(target-num)){ return new int[]{map.get(num)+1,i+1}; } map.put(num, i); } throw new IllegalArgumentException("No two sum solution"); } 首先,暴力解法也就是遍历数组中的每个数字,在数组中寻找target-当前数字,显然时间复杂度比较高,这是一个O(n²)的算法,而在上述代码中我们使用了一个map以O(n)的空间换时间,由于map的put和contains都是O(1)复杂度的操作,算法的时间复杂度为O(n)。 Two Sum 2 题目2和题目1类似,只是在这里input是一个有序的数组,当然我们可以采用题目1的解法,只是貌似没有利用上有序这个条件。 那么谈到有序数组,最常用的算法包括二分查找和双指针法。 public int[] twoSum2(int[] numbers

Python variable increment while-loop

纵饮孤独 提交于 2019-12-25 18:59:28
问题 I'm trying to make the variable number increase if user guessed the right number! And continue increase the increased one if it is guessed by another user. But it's seem my syntax is wrong. So i really need your help. Bellow is my code: #!/usr/bin/python # Filename: while.py number = 23 running = True while running: guess = int(raw_input('Enter an integer : ')) if guess == number: print 'Congratulations, you guessed it. The number is now increase' number += 1 # Increase this so the next user

String Header for data Matlab

北慕城南 提交于 2019-12-25 18:36:07
问题 I found "Adding a header to a matrix in Matlab" question which was very close to what I need done. I was hoping to keep the data in the cells, since my headers are large and i'm running this multiple times I dont want to use file i.o. since it adds tons of time. this is what i have... header = ( 'Quarter', 'monthly amount remaining', 'annual amountremaining'); data = 1 30000 150000 2 20000 130000 and i can't seem to get this out = Quarter monthly am annual am 1 30000 150000 2 20000 130000 It

how can I remove special character from phone number in swift? [duplicate]

余生颓废 提交于 2019-12-25 18:34:05
问题 This question already has answers here : remove \\u{e2} characters from string (3 answers) Closed 10 months ago . When I copy number phone from phone app I get results like this \u{e2}05xxxxxx49\u{e2} I need to remove this character \u{e2} from the left and right of the number to get a result like this 05xxxxxx49 var str = number str = str!.replacingOccurrences(of: "\u{e2}", with: "") print(str!) 回答1: thanks for all finally I know how is it 👇👇👇👇 var number = self.myTFForNumber.text self

divisors of divisors of a number

走远了吗. 提交于 2019-12-25 18:29:15
问题 If we try to find all the divisors D(array) of a number N, then the divisors of each d in D is also automatically calculated in the calculation of D. Is there a way that we can find all the divisors of a number N by finding the divisors of all its divisors, and then finally summing them up. Obviously, there is a way but I want a way which doesn't have repetitive calculation. I think this can be done by recursion, but I am not getting how to proceed. I am providing my implementation of

Guessing a number, true or false? Python

安稳与你 提交于 2019-12-25 17:26:22
问题 I want to assign something a value and then try and get someone to guess that value. I have tried something along the lines of this but I can't seem to get it to work...: foo = 1 guessfoo = input('Guess my number: ') if foo == guessfoo: print('Well done, You guessed it!') if foo != guessfoo: print('haha, fail.') Why doesn't this work? And how should I be doing it? I am a beginner at this, please help! 回答1: With python version 3 : input() returns a 'str' (string) object. A string compares to

how to find only numbers in a array by using regexp in php?

吃可爱长大的小学妹 提交于 2019-12-25 17:05:40
问题 i am using $front->getRequest()->getParams() to get the url params. They look like this Zend_Debug::dump($front->getRequest()->getParams()); array(4) { ["id"] => string(7) "3532231" ["module"] => string(7) "test" ["controller"] => string(6) "index" ["action"] => string(5) "index" } i am interested in running this through preg_match_all to get back only the id numbers by using some regexp similar to ([\s0-9])+ for some reason i cant isolate that number. There is the possibility that there will

Java - Disable scientific notation in JTable cells

自古美人都是妖i 提交于 2019-12-25 17:00:33
问题 I have a JTable where a series of Float are displayed. For small values the cell displays the number in scientific notation. Is there a way to disable that? 回答1: You should read the following page to get an insight about why it is happening: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender Then you should pass your own NumberFormat instance (somehow) in to your JTable , and then all numbers will be formatted properly. I think it's a good read to read through the

Maximum length of JTextField and only accepts numbers?

孤街醉人 提交于 2019-12-25 16:50:55
问题 I want the user to input a maximum of 8 numbers as it is a field for Mobile number. This is my JTextField. txtMobile = new JTextField(); txtMobile.setColumns(10); txtMobile.setBounds(235, 345, 145, 25); add(txtMobile); While we're at it, how do I check for invalid characters like >> '^%$* in a JTextField ? 1)Maximum Length 2)Accepts only numbers 3)Check for invalid characters 4)Check if it's a valid email address Please help :D 回答1: You could use a JFormattedField , check out How to Use