A stupid way to beat 99.57% cpp submissions


  • 0
    T
    class Solution {
    public:
    	vector<int> twoSum(vector<int>& nums, int target) {
    		int validNums[10000];
    		memset(validNums, -1, sizeof(int)* 10000);
    		int nvalidNums[10000];
    		memset(nvalidNums, -1, sizeof(int)* 10000);
    		vector<int> result;
    
    		int searchFor;
    		int size = nums.size();
    		for (int i = 0; i<size; ++i) {
    			searchFor = target - nums[i];
    			if (searchFor >= 0) {
    				
    				if (validNums[searchFor] >= 0) {
    					result.push_back(validNums[searchFor]);
    					result.push_back(i);
    					return result;
    				}
    			}
    			else {
    				if (nvalidNums[-searchFor] >= 0) {
    					result.push_back(i);
    					result.push_back(nvalidNums[-searchFor]);
    					return result;
    				}
    			}
    			if (nums[i] >= 0) {
    				validNums[nums[i]] = i;
    			}
    			else {
    				nvalidNums[-nums[i]] = i;
    			}
    
    		}
    		return result;
    	}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.